【发布时间】:2020-09-11 15:50:13
【问题描述】:
我在models.py 有 3 个模型,关系是很多产品对很多卖家,一个类别可以有很多产品:
association_table = db.Table('association', db.metadata,
db.Column('products_id', db.Integer, db.ForeignKey('products.id')),
db.Column('sellers_id', db.Integer, db.ForeignKey('sellers.id'))
)
class Product(db.Model):
__tablename__ = 'products'
id = db.Column(db.Integer, primary_key=True)
product_name = db.Column(db.String(60), index=True)
sellers_id = db.relationship('Seller', secondary=association_table)
category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
categories = db.relationship('Category', back_populates='products')
def __repr__(self):
return f'{self.product_name}'
class Seller(db.Model):
__tablename__ = 'sellers'
id = db.Column(db.Integer, primary_key=True)
seller_name = db.Column(db.String(60), index=True)
email = db.Column(db.String(60))
phone = db.Column(db.String(60))
site = db.Column(db.String(60))
def __repr__(self):
return f'{self.seller_name}'
class Category(db.Model):
__table__name = 'categories'
id = db.Column(db.Integer, primary_key=True)
category_name = db.Column(db.String(60), index=True)
products = db.relationship('Product', back_populates='category')
def __repr__(self):
return f'{self.category_name}'
我收到以下错误:
Traceback (most recent call last):
File "/home/andressa/MEGAsync/Code/product-catalog/tests/test_models.py", line 7, in setUp
self.u = User(username='u', password='cat')
File "<string>", line 2, in __init__
File "/home/andressa/anaconda3/envs/product-catalog/lib/python3.6/site-packages/sqlalchemy/orm/instrumentation.py", line 376, in _new_state_if_none
state = self._state_constructor(instance, self)
File "/home/andressa/anaconda3/envs/product-catalog/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 883, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "/home/andressa/anaconda3/envs/product-catalog/lib/python3.6/site-packages/sqlalchemy/orm/instrumentation.py", line 202, in _state_constructor
self.dispatch.first_init(self, self.class_)
File "/home/andressa/anaconda3/envs/product-catalog/lib/python3.6/site-packages/sqlalchemy/event/attr.py", line 322, in __call__
fn(*args, **kw)
File "/home/andressa/anaconda3/envs/product-catalog/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py", line 3392, in _event_on_first_init
configure_mappers()
File "/home/andressa/anaconda3/envs/product-catalog/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py", line 3276, in configure_mappers
raise e
sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'mapped class Category->category'. Original exception was: Could not determine join condition between parent/child tables on relationship Category.product - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
我该如何解决这个错误?
【问题讨论】:
-
这可能只是一个错字,但你有这个:
__table__name = 'categories'而应该是:__tablename__ = 'categories'。看看这是否有助于做出改变。 -
@mechanical_meat 是的,这是一个错字,但错误与其他一些事情有关!刚刚找到答案,会回答我自己的问题哈哈谢谢你的帮助
标签: python python-3.x sqlite flask sqlalchemy