SQLAlchemy's automap_base creates bad collections for one-to-many tables

百般思念 提交于 2020-01-15 09:21:26

问题


I am using Flask-SQLAlchemy to recreate tables in an existing SQL Server database. The usual declarative base wasn't working out so I am trying to use the automap base.

My DB has a table called 'orders' and I am trying to query the data:

db = SQLAlchemy(app)
db.Model.metadata.reflect(bind=db.engine)

Base = automap_base()
Base.prepare(db.engine, reflect=True)

db.session.query(Base.classes.orders).all()

However, an ArgumentError is thrown:

ArgumentError: orders.orders and back-reference orders.orders_collection are both of the same direction symbol('ONETOMANY'). Did you mean to set remote_side on the many-to-one side ?

The SQLAlchemy doc tells me that the latter orders.orders_collection is automatically created. What could I do to make sure the creation of this collection doesn't lead to this ArgumentError?

来源:https://stackoverflow.com/questions/41064015/sqlalchemys-automap-base-creates-bad-collections-for-one-to-many-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!