SQLModel 示例:多列唯一约束
在以下示例中,我们在 SQLModel 中对多列(seller 和 url)创建唯一约束。
sqlmodel_unique_constraint.py
from sqlmodel import SQLModel, Field, UniqueConstraint
class Offer(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
seller: str = Field(description="Name of the seller")
url: str = Field(description="URL to the product page for this seller")
__table_args__ = (
UniqueConstraint('seller', 'url', name='unique_seller_url'),
)你可以随意选择 UniqueConstraint 的 name 参数。它用于在数据库中命名约束。
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow