-
First Check
Commit to Help
Example Codefrom typing import Optional
import datetime
from sqlmodel import SQLModel, Field
class User(SQLModel):
id: int = Field(primary_key=True)
name: str = Field(unique=True)
lastname: str = Field(unique=True)Description
Operating SystemWindows Operating System DetailsNo response SQLModel Version0.0.3 Python Version3.8.6 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments
-
|
Do this from typing import Optional
import datetime
from sqlalchemy import UniqueConstraint
from sqlmodel import SQLModel, Field
class User(SQLModel):
__table_args__ = (UniqueConstraint("name", "lastname"), )
id: int = Field(primary_key=True)
name: str = Field(unique=True)
lastname: str = Field(unique=True)UPD YuriiMotov: you probably don't need from typing import Optional
import datetime
from sqlalchemy import UniqueConstraint
from sqlmodel import SQLModel, Field
class User(SQLModel, table=True):
__table_args__ = (UniqueConstraint("name", "lastname"), )
id: int = Field(primary_key=True)
name: str
lastname: str |
Beta Was this translation helpful? Give feedback.
-
|
it works! thank you a lot! where did you find the solution? |
Beta Was this translation helpful? Give feedback.
-
|
Basically, the Specifically, for |
Beta Was this translation helpful? Give feedback.
-
|
It seems to be no longer working with 0.0.4 version: @tadejsv can you confirm or not, please? |
Beta Was this translation helpful? Give feedback.
-
@Trophime This one works for |
Beta Was this translation helpful? Give feedback.
-
|
@northtree I think this would result in two independent constraints rather than a single composite constraint. In other words, it would only allow one record with a given first or last name rather than a given first and last name. |
Beta Was this translation helpful? Give feedback.
-
If only one single composite unique constraint needed, you just have to define via |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
|
Whatever the reason for the change may be, I think that |
Beta Was this translation helpful? Give feedback.

Do this
UPD YuriiMotov: you probably don't need
Field(unique=True)anymore. And don't forgetteble=True: