简体   繁体   English

SQLALCHEMY:没有唯一约束匹配给定键的引用表

[英]SQLALCHEMY: There is no unique constraint matching given keys for referenced table

I'm trying to create a relationship between my tables seller and item where each seller can sell any number of items but they can't sell the same item twice. 我正在尝试在我的表卖家和商品之间建立一种关系,其中每个卖家可以出售任意数量的商品,但他们不能两次出售同一商品。 Here's what I have: 这是我所拥有的:

sells = db.Table('sells',
     db.Column('seller_email', db.String(), db.ForeignKey('seller.email'), primary_key=True),
     db.Column('item_id', db.Integer, ForeignKey('item.id'), primary_key=True)
)

class Item(db.Model):
     __tablename__ = 'item'

     id = db.Column(db.Integer, primary_key=True)
     coverPhoto = db.Column(db.String())
     price = db.Column(db.Integer)
     condition = db.Column(db.Integer)
     title = db.Column(db.String())

     def __init__(self, title, coverPhoto, price, condition):
         self.coverPhoto = coverPhoto
         self.price = price
         self.condition = condition
         self.title = title

     def __repr__(self):
         return '<id {}>'.format(self.id)

class Seller(db.Model):
     __tablename__ = 'seller'

     email = db.Column(db.String(), primary_key=True)
     password = db.Column(db.String())
     firstName = db.Column(db.String())
     lastName = db.Column(db.String())
     location = db.Column(db.String())

     def __init__(self, email, password, firstName, lastName, location):
         self.email = email
         self.password = password
         self.firstName = firstName
         self.lastName = lastName
         self.location = location

     def __repr__(self):
         return "<Seller {email='%s'}>" % (self.email)

And I get the following error: 我得到以下错误:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) there is no unique constraint matching given keys for referenced table "seller"

[SQL: '\nCREATE TABLE sells (\n\tseller_email VARCHAR NOT NULL, \n\titem_id INTEGER NOT NULL, \n\tPRIMARY KEY (seller_email, item_id), \n\tFOREIGN KEY(item_id) REFERENCES item (id), \n\tFOREIGN KEY(seller_email) REFERENCES seller (email)\n)\n\n']

Bother seller.email and item.id are primary keys so shouldn't they inherently be unique? Seller.email和item.id都是主键,所以它们本来就不是唯一的吗?

You're creating the table sells using db.Table , a SQLAlchemy Core function. 您正在使用SQLAlchemy Core函数db.Table创建sells表。 Nothing wrong with that. 没有错。 Then you create your other tables by inheriting from db.Model using the SQLAlchemy ORM's declarative syntax. 然后,您可以使用SQLAlchemy ORM的声明性语法从db.Model继承来创建其他表。 (If you're not familiar with the difference between SQLAlchemy Core and ORM, the tutorial is a good place to start.) (如果您不熟悉SQLAlchemy Core和ORM之间的区别,那么本教程是一个很好的起点。)

You've got a couple of potential issues here: 您在这里有几个潜在的问题:

  1. You're using db.Model and the SQLAlchemy ORM's declarative syntax. 您正在使用db.Model和SQLAlchemy ORM的声明性语法。 When you do this, your model subclasses don't need an __init__ function. 执行此操作时,模型子类不需要__init__函数。 In fact, using an __init__ likely will cause problems (possibly even the root cause here) as it will interfere with the monkey-patching that SQLAlchemy does to make the Declarative syntax so convenient... 实际上,使用__init__可能会引起问题(甚至可能是根本原因),因为它会干扰猴子的补丁,而SQLAlchemy确实使猴子补丁使Declarative语法如此方便...

  2. I suspect that the root cause here might actually be that you use SQLAlchemy Core to create a table with a foreign key reference to a SQLAlchemy ORM-managed table. 我怀疑这里的根本原因可能实际上是您使用SQLAlchemy Core创建的表具有对SQLAlchemy ORM管理的表的外键引用。 Normally, when you do db.metadata.create_all() , SQLAlchemy will collect all the table/model mappings, look at the dependencies and figure out the correct ordering for emitting the CREATE TABLE / ADD CONSTRAINT commands to the database. 通常,当您执行db.metadata.create_all() ,SQLAlchemy将收集所有表/模型映射,查看依赖关系并找出向数据库发出CREATE TABLE / ADD CONSTRAINT命令的正确顺序。 This under-the-covers dependency resolving is what allows the app programmer to define a table that includes a foreign key to a table that is defined later. 这种幕后的依赖关系解析使应用程序程序员可以定义一个表,该表包括到稍后定义的表的外键。 When you have a Core-based Table object that references a ORM db.Model -based object, it might prevent the dependency resolving from working correctly during table creation. 当您有一个基于Core的Table对象引用一个基于ORM db.Model的对象时,它可能会阻止依赖关系解析在表创建期间正常工作。 I'm not 100% confident this is the issue, but it's worth experimenting with making all your tables either Table objects, or db.Model subclasses. 我不是100%确信这是问题所在,但是值得尝试使所有表成为Table对象或db.Model子类。 If you're not sure, I'd suggest the latter. 如果您不确定,我建议使用后者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sqlalchemy模式:没有唯一约束匹配给定表的键 - sqlalchemy schema: there is no unique constraint matching given keys for referenced table Python - sqlalchemy.exc.ProgrammingError: (psycopg2.errors.InvalidForeignKey) 没有唯一约束匹配引用表的给定键 - Python - sqlalchemy.exc.ProgrammingError: (psycopg2.errors.InvalidForeignKey) there is no unique constraint matching given keys for referenced table sqlalchemy.exc.ProgrammingError: (psycopg2.errors.InvalidForeignKey) 引用表“dicom”的给定键没有唯一约束匹配 - sqlalchemy.exc.ProgrammingError: (psycopg2.errors.InvalidForeignKey) there is no unique constraint matching given keys for referenced table "dicom" 没有与引用表“用户”的给定键匹配的唯一约束(PostgreSQL) - No unique constraint matching given keys for referenced table “users” (PostgreSQL) django 迁移错误:“没有唯一约束匹配给定键的引用表 - django error on migration: "There is no unique constraint matching given keys for referenced table Django错误地宣称“没有给引用表的键匹配唯一约束” - Django falsely claims “There is no unique constraint matching given keys for referenced table” django 中没有与给定键匹配的唯一约束 - There is no unique constraint matching given keys for referenced table in django 错误:“没有唯一约束匹配引用表的给定键”,尽管包含主键 - Error: "there is no unique constraint matching given keys for referenced table" although primary key is included django.db.utils.ProgrammingError:没有唯一约束匹配引用表的给定键 - django.db.utils.ProgrammingError: there is no unique constraint matching given keys for referenced table Postgres / SQLAlchemy:没有匹配的唯一约束 - Postgres / SQLAlchemy: no matching unique constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM