我在DB(sqlite)Operation
和Contragent
的两个模型之间创建连接:
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
设置为建议值后仍会发生错误,还有哪些选项可以更改数据库模型呢?