How to deal with column type needed situation? #1517
-
First Check
Commit to Help
Example Codefrom sqlmodel import SQLModel, UniqueConstraint, select
class BookCollection(SQLModel, table=True):
user_nid: int
book_nid: int
UniqueConstraint(
BookCollection.user_nid, # Argument of type "int" cannot be assigned to parameter "columns" of type "str | Column[Any]" in function "__init__"
BookCollection.book_nid, # Argument of type "int" cannot be assigned to parameter "columns" of type "str | Column[Any]" in function "__init__"
name="uidx_book_collection_user_nid_book_nid",
)
# Cannot access member "not_in" for type "int"
select(BookCollection).where(BookCollection.book_nid.not_in([1, 2, 3])) DescriptionSome APIs of sqlalchemy may still need a column type. Without that, a type checker will complain. Operating SystemLinux, macOS Operating System DetailsNo response SQLModel Version0.0.4 Python Version3.8.6 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
from sqlmodel import SQLModel, select, Column class BookCollection(SQLModel, table=True): Define the table's UniqueConstraint using SQLAlchemy's Column objectsuidx_book_collection = UniqueConstraint( Use a list to specify the column names for the "in_" conditionbook_nids: List[int] = [1, 2, 3] print(uidx_book_collection) |
Beta Was this translation helpful? Give feedback.
-
In both cases you should use from sqlmodel import (
Field,
SQLModel,
UniqueConstraint,
col,
select,
)
class BookCollection(SQLModel, table=True):
id: int = Field(primary_key=True)
user_nid: int
book_nid: int
UniqueConstraint(
col(BookCollection.user_nid),
col(BookCollection.book_nid),
name="uidx_book_collection_user_nid_book_nid",
)
select(BookCollection).where(col(BookCollection.book_nid).not_in([1, 2, 3])) |
Beta Was this translation helpful? Give feedback.
In both cases you should use
col()
(from sqlmodel import col
):