我使用ponyorm,我想在两个字段的组合中添加独特的约束。
这是一个示例(ponyeditor链接:)。
我有两个表格,父母和 child 。父母可以有很多孩子,但没有两个孩子。这意味着我想对子表的夫妇(父母,名称)添加独特的约束。
这很简单,但是我不知道python语法要做。
I use PonyORM, and I want to add a unique constraint on a combination of two fields.
Here's an example (PonyEditor link : https://editor.ponyorm.com/user/lial_slasher/DoubleConstraint).
I have two tables, Parent and Child. A parent can have many childs, but not two with the same name. Which means I want to add a unique constraint on the couple (parent, name) of the Child table.
It's pretty straightforward, but I can't figure out the python syntax to do it.
发布评论
评论(1)
您可以使用
Composite_key
,根据文档,在SQL中的几列上等同于unique
。示例:
在这里,
name
Child
类中的属性将与parent
一起唯一具体名称。这等于以下SQL查询:You can use
composite_key
which according to the document, is equivalent toUNIQUE
on several columns in SQL.example:
Here, the
name
attribute in theChild
class will be unique together with theparent
which means each parent can have exactly one child with a specific name. This is equal to the following SQL query: