flask-sqlalchemy

SQLAlchemy delete doesn't cascade

偶尔善良 提交于 2019-11-26 22:47:53
问题 My User model has a relationship to the Address model. I've specified that the relationship should cascade the delete operation. However, when I query and delete a user, I get an error that the address row is still referenced. How do I delete the user and the addresses? class User(db.Model): id = db.Column(db.Integer, primary_key=True) addresses = db.relationship('Address', cascade='all,delete', backref='user') class Address(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db

flask sqlalchemy query with keyword as variable

时间秒杀一切 提交于 2019-11-26 22:22:51
问题 Let's say I have a model like this: class User(db.Model): id = db.Column(db.Integer, primary_key=True) hometown = db.Column(db.String(140)) university = db.Column(db.String(140)) To get a list of users from New York, this is my query: User.query.filter_by(hometown='New York').all() To get a list of users who go to USC, this is my query: User.query.filter_by(university='USC').all() And to get a list of users from New York, and who go to USC, this is my query: User.query.filter_by(hometown='New

how to use QuerySelectField in flask?

為{幸葍}努か 提交于 2019-11-26 21:22:33
问题 I am trying to have a select field filled with the results of a sqlalchemy request in a flask form. Here are the code : def possible_book(): return Book.query.with_entities(Book.id).all() class AuthorForm(Form): familyname = TextField('familyname', [validators.Required()]) firstname = TextField('firstname', [validators.Required()]) book_list = QuerySelectField('book_list',query_factory=possible_book,get_label='title',allow_blank=True) This is the template : <form action="" method="post" name=

ImportError: No module named flaskext.sqlalchemy

我与影子孤独终老i 提交于 2019-11-26 21:10:54
I am trying to use the Sign in with SteamID snippet from the Flask site. However, I get ImportError: No module named flaskext.sqlalchemy when I try to run it, and PyCharm says Uresolved reference "flaskext" and Uresolved reference "OpenID" . I re-installed Flask-OpenID and Flask-SQLAlchemy to make sure they were there. Why am I getting this error and how do I fix it? That snippet is really old. flaskext is no more (or at least very deprecated). Refer to packages directly rather than through flaskext or flask.ext . from flask_sqlalchemy import SQLAlchemy Flask-SQLAlchemy (and most other

Flask sqlalchemy many-to-many insert data

不羁岁月 提交于 2019-11-26 18:58:26
问题 I am trying to make a many to many relation here in Flask-SQLAlchemy , but it seems that I don't know how to fill the "many to many identifier database" . Could you please help me understand what I am doing wrong and how it is supposed to look? class User(db.Model): __tablename__ = 'users' user_id = db.Column(db.Integer, primary_key=True) user_fistName = db.Column(db.String(64)) user_lastName = db.Column(db.String(64)) user_email = db.Column(db.String(128), unique=True) class Class(db.Model):

How to build a flask application around an already existing database?

非 Y 不嫁゛ 提交于 2019-11-26 18:11:05
I already have an existing Database that has a lot of tables and a lot of data in MySQL . I intend to create a Flask app and use sqlalchemy along with it. Now I asked out on irc and looked around on google and tried the following ideas: First I used sqlacodegen to generate the models from my DB . But then I was confused about it a little and looked some more. And I found this . This looked like an elegant solution. So Second , I rewrote my models.py according to the solution there and now I am even more confused. I am looking for the best approach to build this flask app along with the already

SQLAlchemy ordering by count on a many to many relationship

耗尽温柔 提交于 2019-11-26 16:23:13
问题 This is a simplified example of my current models (I'm using the Flask SQLAlchemy extension) : like = db.Table( 'like', db.Column('uid', db.Integer, db.ForeignKey('users.id')), db.Column('pid', db.Integer, db.ForeignKey('posts.id')) ) class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key = True) username = db.Column(db.String(20)) class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key = True) title = db.Column(db.String(255)) likes

Can SQLAlchemy be used with Google Cloud SQL?

一曲冷凌霜 提交于 2019-11-26 16:07:45
问题 I've looked over Google Cloud SQL's documentation and various searches, but I can't find out whether it is possible to use SQLAlchemy with Google Cloud SQL, and if so, what the connection URI should be. I'm looking to use the Flask-SQLAlchemy extension and need the connection string like so: mysql://username:password@server/db I saw the Django example, but it appears the configuration uses a different style than the connection string. https://developers.google.com/cloud-sql/docs/django Google

Flask-SQLalchemy update a row&#39;s information

时光毁灭记忆、已成空白 提交于 2019-11-26 15:04:49
问题 How can I update a row's information? For example I'd like to alter the name column of the row that has the id 5. 回答1: Retrieve an object using the tutorial shown in the Flask-SQLAlchemy documentation. Once you have the entity that you want to change, change the entity itself. Then, db.session.commit() . For example: admin = User.query.filter_by(username='admin').first() admin.email = 'my_new_email@example.com' db.session.commit() user = User.query.get(5) user.name = 'New Name' db.session

Can&#39;t set attribute on result objects in SQLAlchemy flask

半腔热情 提交于 2019-11-26 14:52:55
问题 I encounter a problem with Flask-SQLAlchemy, I can set the attribute of the objects in place_collections , but when I want to set the attribute for objects in places , an error occurred: Traceback (most recent call last): File "/Users/user/PycharmProjects/website/venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__ return self.wsgi_app(environ, start_response) File "/Users/user/PycharmProjects/website/venv/lib/python3.6/site-packages/werkzeug/contrib/fixers.py", line 152, in