用于在数据库(sqlite)模型之间创建关系的选项。烧瓶

时间:2019-07-02 07:05:06

标签: python flask flask-sqlalchemy

我在DB(sqlite)OperationContragent的两个模型之间创建连接:

class Operation(db.Model):
    __tablename__ = "operation"
    id = db.Column(db.Integer, primary_key=True)    
    date_operation = db.Column(db.DateTime, index=True, nullable=True)    
    contragent_id = db.Column(db.Integer, db.ForeignKey('contragent.id'))# add db.ForeignKey('contragent.id')

    def __repr__(self):
        return '<Operation {}>'.format(self.code)


class Contragent(db.Model):
    __tablename__ = "contragent"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(360))    
    operations = db.relationship('Operation', backref='operation', lazy='dynamic')# add this line

    def __repr__(self):
        return '<Contragent {}>'.format(self.name)

更改模型后,创建新的数据库迁移并 我在数据库中应用更改,同时出现错误:

(venv) C:\Users\User\testapp>flask db upgrade
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade d0ac0532c134 -> 285707aa6265,
Operation table
ERROR [root] Error: No support for ALTER of constraints in SQLite dialect

run_migrations_online ()文件的env.py方法中,我添加了render_as_batch = True,但错误仍然存​​在:

with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            process_revision_directives=process_revision_directives,
            render_as_batch=True, # add this line
            **current_app.extensions['migrate'].configure_args
        )

此后,我尝试设置变量render_as_batch的值:

config.get_main_option('sqlalchemy.url').startswith('sqlite:///')
...
with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            process_revision_directives=process_revision_directives,
            render_as_batch=config.get_main_option('sqlalchemy.url').startswith('sqlite:///'), # add this line
            **current_app.extensions['migrate'].configure_args
        )
....

错误仍然发生! 为什么即使在将变量render_as_batch设置为建议值后仍会发生错误,还有哪些选项可以更改数据库模型呢?

0 个答案:

没有答案