flask-sqlalchemy

Accessing base class variable [duplicate]

谁都会走 提交于 2019-12-25 04:08:43
问题 This question already has an answer here : Accessing a mixin member variable (column) from the child (1 answer) Closed last year . How do I access id from my child class? class BaseClass: id = 'testing' class MyClass(BaseClass): something = id More specifically, I can't access id from class:User : class BaseModel: id = db.Column(db.Integer, primary_key=True) class User(db.Model, BaseModel): username = db.Column(db.String(35), nullable=False, unique=True) followed = db.relationship( 'User',

Flask-migrate multiple models.py

人盡茶涼 提交于 2019-12-25 02:47:14
问题 I've a question related to Flask-migrate. I'm creating a set of web services with Flask. I've split each web service in his own package in a python application. The application structure look like this: MyApp WS1 models.py WS2 models.py CommonPackage models.py How can I import all the modules and init the db? I've tried to import them all manually but is not working. I know that it works if I import the "app" from WS1 or Ws2 separately, but I would like to do this in a single operation, is

When accessed similarly, why does a SQLAlchemy relationship return differently?

↘锁芯ラ 提交于 2019-12-25 01:01:33
问题 With this simplified model: class User(db.Model): id = db.Column(db.Integer, primary_key=True) # other attributes #user many-to-many size associations (using link tables) sz_shirt_dress_sleeve = db.relationship( 'SizeKeyShirtDressSleeve', secondary=LinkUserSizeShirtDressSleeve, backref=db.backref('users', lazy='dynamic')) strange = {'whyisthisdifferent': db.relationship( 'SizeKeyShirtDressNeck', secondary=LinkUserSizeShirtDressNeck, backref=db.backref('users', lazy='dynamic'))} I would assume

sqlalchemy 1 to many relationship

拥有回忆 提交于 2019-12-24 21:26:20
问题 i tried to implement a simple sqlalchemy table relationship so, I have below model: class User(db.Model): __tablename__ = "users" UserID = db.Column(db.Integer, primary_key=True, autoincrement=True) FirstName = db.Column(db.String(255), nullable=False) LastName = db.Column(db.String(255), nullable=False) #others column Tokens = db.relationship('Token', backref='Token.UserID', primaryjoin='User.UserID==Token.UserID', lazy='dynamic') #bla... other codes and class Token(db.Model): __tablename__

Flask/Sqlaclemy: How to do one to many polymorphic inheritance

这一生的挚爱 提交于 2019-12-24 20:13:25
问题 I am attempting to create a one to many polymorphic relationship in flask/sqlalchemy. The concept is to provide temporary to certain data using an authorizations table. The table will have two fields, authorizable_id and authorizable_type. The authorizable_id refers to the id of the entry it is referring to. It is not a foreign key as it can refer to different tables. The authorizable_type determines which table it is referring to. For example, for a file, it is 'FILE'. I've seen how to do

Format db.Datetime values automatically in Flask-SQLAlchemy

落花浮王杯 提交于 2019-12-24 19:26:37
问题 I'm in the process of migrating hosts, and the new hosts uses a newer version of MySQL which is stricter about the format of datetime values it accepts. I know that I can turn this off, but I'd like to find a solution to properly convert these values to the correct format. Preferably one that can be easily swapped out across the many models I need to update (100 or so). If I can't find a way to update this on the model level, I may have to hunt down the thousands of locations where these

Sqlite3 Disk I/O error encountered after a while, but worked after using copy of db

笑着哭i 提交于 2019-12-24 18:40:30
问题 I'm using an sqlite3 database to record data every second. The interface to it is provided by Flask-SQLAlchemy. This can work fine for a couple of months, but eventually (as the .db file approaches 8 GB), an error prevents any more data from being written to the database: Failed to commit: (sqlite3.OperationalError) disk I/O error The journal file does not seem to be the issue here - if I restart the application and use the pragma journal_mode=TRUNCATE , the journal file is created but the

How can I programmatically set the 'target_metadata' required by Alembic for use with the command API?

自作多情 提交于 2019-12-24 12:00:35
问题 I'm managing database migrations with Alembic and would like to use the default files generated by alembic init without any modifications to env.py (e.g., setting target_metadata) or alembic.ini (e.g., setting sqlalchemy.url) to manage migrations of my database from scripts. For example, I'd like to use a command based script like the following: import os from alembic.config import Config from alembic import command from myapp import db alembic_cfg = Config(os.path.join(os.path.abspath(os

SQLAlchemy union_all and all() returning incorrect number of items

风流意气都作罢 提交于 2019-12-24 11:35:02
问题 For some reason when I'm using SQLAlchemy's union_all and .all() , it's returning the incorrect number of items. As you can see below, I broke each one down to see where the error was. Does anyone have any idea why this would be happening? >>> pn = PostNotification.query.filter_by(notified_id=1) >>> cn = CommentNotification.query.filter_by(notified_id=1) >>> pn.count() 4 >>> cn.count() 2 >>> u = pn.union_all(cn) >>> u.count() 6 >>> all = u.all() >>> len(all) 5 Here are my two models: class

SQLalchemy updating all rows instead of one

别等时光非礼了梦想. 提交于 2019-12-24 07:40:07
问题 This code is supposed to update a post in the database that matches 'post_id'. But it updates every post in the table. The code has been edited so it works correctly. @app.route('/update/<post_id>', methods=['GET', 'POST']) def update(post_id): if 'username' not in session: return redirect(url_for('login')) post = Post.query.get(post_id) form = PostForm(obj=post) if request.method == 'POST': if form.validate() == False: return render_template('update.html', form=form) else: Post.query.filter