flask
我在两个模型之间建立多个关系时遇到问题。 这些是我现在拥有的两个型号:
class Product(db.Model): tablename='product' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) image_id = db.Column(db.Integer, db.ForeignKey('image.id')) image = db.relatihip('Image',uselist=False,backref=db.backref('product')) class Image(db.Model): __tablename__='address' id = db.Column(db.Integer, primary_key=True) normal = db.Column(db.String(200)) product_id = db.Column(db.Integer, db.ForeignKey('product.id')) product = db.relatihip('Product', backref='product_images')产品应该与封面图像一对一,以及与其他图像库一对一的产品。 但是,外键存在循环依赖关系。
我想只在两张桌子里这样做。 有没有其他方法来实现这两种关系?
此时代码抛出:
AmbiguousForeignKeysErrorI am having trouble setting up multiple relatihips between two models. These are my two models as I have them now:
class Product(db.Model): tablename='product' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) image_id = db.Column(db.Integer, db.ForeignKey('image.id')) image = db.relatihip('Image',uselist=False,backref=db.backref('product')) class Image(db.Model): __tablename__='address' id = db.Column(db.Integer, primary_key=True) normal = db.Column(db.String(200)) product_id = db.Column(db.Integer, db.ForeignKey('product.id')) product = db.relatihip('Product', backref='product_images')Product should have a one-to-one with a cover image, and a one to many with a gallery of other images. However, there is a circular dependency with the foreign keys.
I would like to do this in only two tables. Is there another way to implement these two relatihips?
At this point code above throws:
AmbiguousForeignKeysError最满意答案
这里有两个循环依赖:
外键相互依赖于每个表的存在。 必须在依赖表已存在之后创建其中一个fks。 在一个上设置use_alter=True和name='some_name以解决此问题。 这些关系都需要在插入后解析其目标的primary_key,但是相互依赖于已经提交的两者。 在一个上设置post_update=True以解决此问题。请参阅以下文档:
CircularDependencyError 依赖外键 依赖关系这是一个展示解决方案的工作示例。
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, Table from declarative import declarative_base from import sessionmaker, relatihip engine = create_engine('sqlite:///:memory:', echo=True) Session = sessionmaker(bind=engine) session = Session() Base = declarative_base(bind=engine) class Product(Base): __tablename__ = 'product' id = Column(Integer, primary_key=True) name = Column(String, nullable=False) # cover image foreign key # use_alter=True along with name='' adds this foreign key after Image has been created to avoid circular dependency cover_id = Column(Integer, ForeignKey('image.id', use_alter=True, name='fk_product_cover_id')) # cover image one-to-one relatihip # set post_update=True to avoid circular dependency during cover = relatihip('Image', foreign_keys=cover_id, post_update=True) class Image(Base): __tablename__ = 'image' id = Column(Integer, primary_key=True) path = Column(String, nullable=False) product_id = Column(Integer, ForeignKey(Product.id)) # product gallery many-to-one product = relatihip(Product, foreign_keys=product_id, backref='images') # nothing special was need in Image, all circular dependencies were solved in Product _all() # create some images i1 = Image(path='img1') i2 = Image(path='img2') i = Image(path='img') i4 = Image(path='img4') # create a product with those images, one of which will also be the cover p1 = Product(name='sample', images=[i1, i2, i, i4], cover=i2) session.add(p1) () print 'cover:', path # prints one cover image path print 'images:', [i.path for i in p1.images] # prints 4 gallery image paths print 'image product:', p1.images[0]. # prints product name from image perspectiveThere are two circular dependencies here:
The foreign keys are mutually dependent on the existence of each table. One of the fks must be created after the dependent table already exists. Set use_alter=True and name='some_name on one to resolve this. The relatihips both need to resolve the primary_key of their target after insert, but are mutually dependent on both having already been commited. Set post_update=True on one to resolve this.See the following documentation:
CircularDependencyError Dependent foreign keys Dependent relatihipsHere is a working example demtrating the solution.
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, Table from declarative import declarative_base from import sessionmaker, relatihip engine = create_engine('sqlite:///:memory:', echo=True) Session = sessionmaker(bind=engine) session = Session() Base = declarative_base(bind=engine) class Product(Base): __tablename__ = 'product' id = Column(Integer, primary_key=True) name = Column(String, nullable=False) # cover image foreign key # use_alter=True along with name='' adds this foreign key after Image has been created to avoid circular dependency cover_id = Column(Integer, ForeignKey('image.id', use_alter=True, name='fk_product_cover_id')) # cover image one-to-one relatihip # set post_update=True to avoid circular dependency during cover = relatihip('Image', foreign_keys=cover_id, post_update=True) class Image(Base): __tablename__ = 'image' id = Column(Integer, primary_key=True) path = Column(String, nullable=False) product_id = Column(Integer, ForeignKey(Product.id)) # product gallery many-to-one product = relatihip(Product, foreign_keys=product_id, backref='images') # nothing special was need in Image, all circular dependencies were solved in Product _all() # create some images i1 = Image(path='img1') i2 = Image(path='img2') i = Image(path='img') i4 = Image(path='img4') # create a product with those images, one of which will also be the cover p1 = Product(name='sample', images=[i1, i2, i, i4], cover=i2) session.add(p1) () print 'cover:', path # prints one cover image path print 'images:', [i.path for i in p1.images] # prints 4 gallery image paths print 'image product:', p1.images[0]. # prints product name from image perspective#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上一篇:compare()和compareTo()之间有什么区别?(What is the difference between compare() and compareTo()?)
下一篇:剪映电脑版超简单使用教程Mac
推荐阅读
留言与评论(共有 19 条评论) |
本站网友 远洋一方三期 | 12分钟前 发表 |
外键存在循环依赖关系 | |
本站网友 导致肥胖的原因 | 1分钟前 发表 |
backref='images') # nothing special was need in Image | |
本站网友 古来圣贤皆寂寞 | 6分钟前 发表 |
在一个上设置post_update=True以解决此问题 | |
本站网友 icu病房一天多少钱 | 2分钟前 发表 |
images=[i1 | |
本站网友 取代 | 17分钟前 发表 |
请参阅以下文档: CircularDependencyError 依赖外键 依赖关系 这是一个展示解决方案的工作示例 | |
本站网友 烽火战国战功 | 15分钟前 发表 |
i | |
本站网友 沉珂婚纱照 | 13分钟前 发表 |
有没有其他方法来实现这两种关系? 此时代码抛出: AmbiguousForeignKeysError I am having trouble setting up multiple relatihips between two models. These are my two models as I have them now | |
本站网友 视点31 | 15分钟前 发表 |
primary_key=True) normal = db.Column(db.String(200)) product_id = db.Column(db.Integer | |
本站网友 天目湖好玩吗 | 3分钟前 发表 |
p1.images[0]. # prints product name from image perspective | |
本站网友 山萸肉的功效与作用 | 3分钟前 发表 |
class Product(db.Model) | |
本站网友 隐忍 | 23分钟前 发表 |
[i.path for i in p1.images] # prints 4 gallery image paths print 'image product | |
本站网友 鼓楼大街二手房 | 13分钟前 发表 |
memory | |
本站网友 莱芜男科 | 15分钟前 发表 |
' | |
本站网友 股权激励计划 | 30分钟前 发表 |
[i.path for i in p1.images] # prints 4 gallery image paths print 'image product | |
本站网友 赵嘉 | 2分钟前 发表 |
Column | |
本站网友 雷帕 | 10分钟前 发表 |
uselist=False | |
本站网友 利泰瀑布游览区 | 9分钟前 发表 |
Table from declarative import declarative_base from import sessionmaker | |
本站网友 烤肉宛 | 21分钟前 发表 |
__tablename__ = 'product' id = Column(Integer |