How to define unique constraint in table columns #1929
-
First Check
Commit to Help
Example Codeclass User(SQLModel, table=True):
user_uuid: UUID = Field(default=uuid4, primary_key=True)
name: str
email: str
password: str
balance: float = Field(default=0.0)
income: float = Field(default=0.0)DescriptionHi, guys! I want to define something like: But Field does not have unique param, like SQLAlchemy Column have: I've searched in Docs, Google and GitHub, but I found nothing about unique constraint. Thanks for your attention! Operating SystemWindows Operating System DetailsNo response SQLModel Version0.0.4 Python Version3.9.4 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 16 comments
-
|
see #65 - it has the how to do it. Basically you specify something like - |
Beta Was this translation helpful? Give feedback.
-
Thanks very much for your help, @obassett! Nevertheless, i have opened a Pull Request to use Unique constraint directly by the sqlmodel whithout using sa_column param. |
Beta Was this translation helpful? Give feedback.
-
|
I had the same question, so just for documentation I put my complete example :) Hope that can help someone. So I had to define a list of product with an integer primary key id and a unique name for the products. The file from sqlmodel import SQLModel
class ProductBase(SQLModel):
name: str
description: str
price: float
available: boolThe file: from typing import Optional
from sqlalchemy import String
from sqlalchemy.sql.schema import Column
from sqlmodel import Field
from app.src.schemas.entities import ProductBase
class Product(ProductBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
# name is unique
name: str = Field(sa_column=Column("name", String, unique=True))Of course the relationship with ProductType and ProductTagLink are defined in another files and schemas :) Hope this example helps :) |
Beta Was this translation helpful? Give feedback.
-
|
Found a solution that I think is more elegant using table_args |
Beta Was this translation helpful? Give feedback.
-
|
Why not simply add |
Beta Was this translation helpful? Give feedback.
-
|
@StefnirKristjansson not sure that the usage of @sgraaf 's solution feels the most elegant one for single field constraints. edit: However, both my statements are pure personal preference, no solid grounds on why they would be more or less elegant :) |
Beta Was this translation helpful? Give feedback.
-
|
@sgraaf : Thank you, that works fine! Where is that "trick" documented? |
Beta Was this translation helpful? Give feedback.
-
|
@Data-Mastery Honestly?... Nowhere! I had to dive (deep) into the source code of SQLModel to find it. While the docs are really good in some aspects (very heavy on examples / guides / tutorials), the lack of a comprehensive API reference is very unfortunate. |
Beta Was this translation helpful? Give feedback.
-
|
This isn't documented as it is a feature from SQLAlchemy, but you can define unique constraints at the model level using "table_args". So the above code would add a constraint to prevent duplicate entries in "employee_id". I've only tested this on a PostgreSQL database, but it should work fine for others. The benefit of doing it this way is you avoid having to override the column definition at the field level. https://docs.sqlalchemy.org/en/14/orm/declarative_tables.html |
Beta Was this translation helpful? Give feedback.
-
|
It looks like the Field column now supports the unique=True/False keyword argument? Merged in #83 I was able to successfully use |
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
-
|
Now But I think it would be nice to document the way how to create unique constraints including unique constraints on multiple columns |
Beta Was this translation helpful? Give feedback.
Now
Field(unique=True)works thanks to @raphaelgibson.But I think it would be nice to document the way how to create unique constraints including unique constraints on multiple columns