Python 如何在 SQLAlchemy 中对“UNIQUE”约束进行建模?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14355499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to model a `UNIQUE` constraint in SQLAlchemy?
提问by ereOn
I am writing a Flask/SQLAlchemy application in which I have users and groups.
我正在编写一个有用户和组的 Flask/SQLAlchemy 应用程序。
Users can belong to several groups, and they have a unique number within each group. Asking about how to model the databaseI was advised to use the following table structure for my many-to-many relationship:
用户可以属于多个组,并且他们在每个组中都有一个唯一的编号。在询问如何对数据库建模时,我被建议对我的多对多关系使用以下表结构:
TABLE UserGroups
GroupID
UserID
UserNumber
PRIMARY KEY (GroupID, UserID)
UNIQUE (GroupID, UserNumber)
FOREIGN KEY (GroupID)
REFERENCES Groups (GroupID)
FOREIGN KEY (UserID)
REFERENCES Users (UserID)
Now I know how to create a regular many-to-many relationship with SQLAlchemy, but I don't know how to represent the UNIQUEconstraint with the additional UserNumberfield.
现在我知道如何使用 SQLAlchemy 创建常规的多对多关系,但我不知道如何UNIQUE用附加UserNumber字段表示约束。
I don't have a lot of experience with database design, ORMs and SQLAlchemy, so this may be obvious, but I can't find a way to express it.
我在数据库设计、ORM 和 SQLAlchemy 方面没有很多经验,所以这可能很明显,但我找不到表达方式。
On of the things I don't get is: using a regular many-to-many relationship, my Userclass has a list-likeattribute groupswhich contains all the groups he belongs to, but this completely hides the UserGroupsjoining-table and I don't know how to access the UserNumberfield.
我不明白的事情是:使用常规的多对多关系,我的User班级有一个类似列表的属性groups,其中包含他所属的所有组,但这完全隐藏了UserGroups连接表,我不不知道如何访问该UserNumber字段。
This is all a bit blur to me. Do you have any good example or explanations on how-to do such a thing with SQLAlchemy ?
这对我来说有点模糊。关于如何使用 SQLAlchemy 做这样的事情,你有什么好的例子或解释吗?
采纳答案by schlamar
The first part of the question (about creating a unique constraint with multiple columns) is already answered by cleg.
问题的第一部分(关于创建具有多列的唯一约束)已由 cleg 回答。
However, the default many-to-many approach doesn't work if you want to have additionally columns in the mapping table. Instead, you should use the Association Object Pattern. Additionally, you can simplify the access between user and group with an association_proxy.
但是,如果您想在映射表中添加额外的列,则默认的多对多方法不起作用。相反,您应该使用关联对象模式。此外,您可以使用association_proxy简化用户和组之间的访问。
The proxied_association.pyfrom the SQLAlchemy examplesshould be a good place to start.
在proxied_association.py从SQLAlchemy的例子应该是一个很好的起点。
回答by cleg
Use UniqueConstraintin your model. In detailed it's described in this question: sqlalchemy unique across multiple columns
UniqueConstraint在您的模型中使用。在这个问题中详细描述了它:sqlalchemy unique over multiple columns
As for many-to-many relations, SQLAlchemy have pretty good tutorial.
至于多对多关系,SQLAlchemy 有很好的教程。
P.S. Sorry, I've missed with second part of answer (it's more complex then I've thought, so see answer from @schlamar), but first part is still correct.
PS 抱歉,我错过了答案的第二部分(它比我想象的更复杂,所以请参阅@schlamar 的答案),但第一部分仍然是正确的。

