Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deal with column type needed situation? #95

Open
8 tasks done
Ma233 opened this issue Sep 14, 2021 · 1 comment
Open
8 tasks done

How to deal with column type needed situation? #95

Ma233 opened this issue Sep 14, 2021 · 1 comment
Labels
question Further information is requested

Comments

@Ma233
Copy link

Ma233 commented Sep 14, 2021

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from 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]))

Description

Some APIs of sqlalchemy may still need a column type. Without that, a type checker will complain.
Currently, I'm using type: ignore to skip those.

Operating System

Linux, macOS

Operating System Details

No response

SQLModel Version

0.0.4

Python Version

3.8.6

Additional Context

No response

@Ma233 Ma233 added the question Further information is requested label Sep 14, 2021
@ljluestc
Copy link

ljluestc commented Sep 3, 2023

from sqlmodel import SQLModel, select, Column
from sqlalchemy import UniqueConstraint
from typing import List

class BookCollection(SQLModel, table=True):
user_nid: int
book_nid: int

Define the table's UniqueConstraint using SQLAlchemy's Column objects

uidx_book_collection = UniqueConstraint(
Column("user_nid", Integer), # Specify the column type
Column("book_nid", Integer), # Specify the column type
name="uidx_book_collection_user_nid_book_nid"
)

Use a list to specify the column names for the "in_" condition

book_nids: List[int] = [1, 2, 3]
query = select(BookCollection).where(BookCollection.book_nid.in_(book_nids))

print(uidx_book_collection)
print(query)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants