You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar question 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
"""The class definition"""fromsqlmodelimportFieldfromsqlmodelimportIndexfromsqlmodelimportSQLModelclassFoo(SQLModel, table=True): # type: ignore""" Exposure db version """__tablename__='foo'id: int|None=Field(default=None, primary_key=True)
bar_1: strbar_2: strbar_3: str__table_args__= (
Index(
'foo_filter_id',
'bar_1',
'bar_2',
'bar_3'
),
)
"""Notebook imports"""fromecodev_core.db_connectionimportDB_URL, DBfromsqlmodelimportcreate_engineengine=create_engine(DB_URL,echo=True)
importpsycopg2aspsyfrompsycopg2importextrasaspsy_extrasfromapp.db_modelimportFoofromsqlmodelimportSQLModel, select, Sessionconf= {
'host': DB.db_host,
'database': DB.db_name,
'user': DB.db_username,
'password': DB.db_password,
'port': DB.db_port,
}
defdb_cursor():
""" Get database connection adn cursor based on config file """connection=psy.connect(**conf)
returnconnection, connection.cursor(cursor_factory=psy_extras.RealDictCursor)
"""psycopg2 direct call, working (meaning using the index)"""%%timedb_con, db_cur=db_cursor()
db_cur.execute(f"explain SELECT * from foo where bar_1 = 'a' and bar_2='g' and bar_3 = 's'")
db_cur.fetchall()
"""SQLModel not working, meaning not using the index"""%%timewithSession(engine) assession:
toto=session.exec(select(Foo.bar_1,Foo.bar_2,Foo.bar_3).where(
Foo.bar_1=='a',
Foo.bar_2=='g',
Foo.bar_3=='s')).all()
Description
Hi.
I successfully created a composite index (see code snippet), but can't manage to forge a query that actually uses it.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
Hi.
I successfully created a composite index (see code snippet), but can't manage to forge a query that actually uses it.
Some screenshots

index indeed generated via sqlmodel

composite index working in plain sql

composite index working in psycopg2

composite index not working in SQLModel

Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.22
Python Version
3.11
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions