No exemplo a seguir, criamos uma restrição única sobre múltiplas colunas (seller e url) no SQLModel.
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'),
)Você pode escolher o parâmetro name de UniqueConstraint ao seu gosto. Ele é usado para nomear a restrição no banco de dados.
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow