Python SqlAlchemy外键约束
当我插入新记录时,我在理解ForeignKey约束如何与SqlAlchemy一起工作时遇到了一个问题。我有一个父表,它有两个子表,每一个子表都是一对多关系。我的底层数据库是Oracle,下面是我的表的简化模型:Python SqlAlchemy外键约束,python,oracle,sqlalchemy,Python,Oracle,Sqlalchemy,当我插入新记录时,我在理解ForeignKey约束如何与SqlAlchemy一起工作时遇到了一个问题。我有一个父表,它有两个子表,每一个子表都是一对多关系。我的底层数据库是Oracle,下面是我的表的简化模型: class ProductCategory(db.Model): __tablename__ = 'productcategory' product_category_id = Column(Integer, Sequence('productcategory_seq', pri
class ProductCategory(db.Model):
__tablename__ = 'productcategory'
product_category_id = Column(Integer, Sequence('productcategory_seq', primary_key=True)
label = Column(String)
products = relation('Product', backref='product_category')
printers = relation('Printer', backref='product_category')
class Product(db.Model):
__tablename__ = 'product'
product_id = Column(Integer, Sequence('product_seq'), primary_key=True)
product_category_id = Column(Integer, ForeignKey('productcategory.product_category_id')
name = Column(String)
class Printer(db.Model):
__tablename__ = 'printer'
printer_id = Column(Integer, Sequence('printer_seq'),
product_category_id = Column(Integer, ForeignKey('product_category.product_category_id')
name = Column(String)
下面是一个Python代码的简化示例,它引发了一个(cx_Oracle.integrityyror)ORA-02291:integrity约束(EASTLAB.SYS_C0049050)冲突-父键未找到异常
try:
product_category = ProductCategory(label='some_category')
db.session.add(product_category)
# iterate over the products in the category
for product in products:
new_product = Product(
product_category=product_category,
name=product.name
)
db.session.add(new_product)
# iterate over the printers in the category
for printer in printers:
new_printer = Printer(
product_category=product_category,
name=printer.name
)
db.session.add(new_printer)
# commit before exiting context manager
db.session.commit()
except:
db.session.rollback()
在执行db.session.commit()代码时引发异常。我不知道为什么会提出这个例外,我在上面所做的似乎是我在各种在线帖子中看到的一种模式。有趣的是,如果我注释掉添加打印机子项的代码,这就可以了。我很困惑……:)
任何帮助、指点或建议都将不胜感激
提前感谢,,
Doug打印机上的外键
product\u category\u id
引用的是'product\u category.product\u id'
,而不是'productcategory.product\u category\u id'
使用以下适用于我的工具对此进行了测试:
class ProductCategory(Base):
__tablename__ = 'productcategory'
product_category_id = Column(Integer, Sequence('productcategory_seq'), primary_key=True)
label = Column(String)
products = relation('Product', backref='product_category')
printers = relation('Printer', backref='product_category')
class Product(Base):
__tablename__ = 'product'
product_id = Column(Integer, Sequence('product_seq'), primary_key=True)
product_category_id = Column(Integer, ForeignKey('productcategory.product_category_id'))
name = Column(String)
class Printer(Base):
__tablename__ = 'printer'
printer_id = Column(Integer, Sequence('printer_seq'), primary_key=True)
product_category_id = Column(Integer, ForeignKey('productcategory.product_category_id'))
name = Column(String)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
product_category = ProductCategory(label='some_category')
session.add(product_category)
product_names = ["a", "b", "c"]
# iterate over the products in the category
for product in product_names:
new_product = Product(
product_category=product_category,
name=product
)
session.add(new_product)
printer_names = ["a", "b", "c"]
# iterate over the printers in the category
for printer in printer_names:
new_printer = Printer(
product_category=product_category,
name=printer
)
session.add(new_printer)
# commit before exiting context manager
session.commit()
与您的示例略有不同,当您将示例添加到问题中时,您省略了一些结束括号,我稍微改变了它获取
打印机
和产品
样本的方式,打印机
上的外键产品类别id
正在引用'product\u category.product\u category\u id'
而非'productcategory.product\u category\u id'
使用以下适用于我的工具对此进行了测试:
class ProductCategory(Base):
__tablename__ = 'productcategory'
product_category_id = Column(Integer, Sequence('productcategory_seq'), primary_key=True)
label = Column(String)
products = relation('Product', backref='product_category')
printers = relation('Printer', backref='product_category')
class Product(Base):
__tablename__ = 'product'
product_id = Column(Integer, Sequence('product_seq'), primary_key=True)
product_category_id = Column(Integer, ForeignKey('productcategory.product_category_id'))
name = Column(String)
class Printer(Base):
__tablename__ = 'printer'
printer_id = Column(Integer, Sequence('printer_seq'), primary_key=True)
product_category_id = Column(Integer, ForeignKey('productcategory.product_category_id'))
name = Column(String)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
product_category = ProductCategory(label='some_category')
session.add(product_category)
product_names = ["a", "b", "c"]
# iterate over the products in the category
for product in product_names:
new_product = Product(
product_category=product_category,
name=product
)
session.add(new_product)
printer_names = ["a", "b", "c"]
# iterate over the printers in the category
for printer in printer_names:
new_printer = Printer(
product_category=product_category,
name=printer
)
session.add(new_printer)
# commit before exiting context manager
session.commit()
与您的示例略有不同,当您将示例添加到问题中时,您省略了一些结束括号,我稍微改变了它获取打印机
和产品
样本的方式