-
First Check
Commit to Help
Example Codefrom typing import Optional
from sqlmodel import Field, Session, SQLModel, create_engine, select, col
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
secret_name: str
age: Optional[int] = None
sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
engine = create_engine(sqlite_url, echo=True)
def create_db_and_tables():
SQLModel.metadata.create_all(engine)
def create_heroes():
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)
with Session(engine) as session:
session.add_all([hero_1, hero_2, hero_3])
session.commit()
def select_heroes():
with Session(engine) as session:
statement = select(Hero).where(col(Hero.age) is None, Hero.name.like('%Deadpond%'))
results = session.exec(statement)
for hero in results:
print(hero) DescriptionTo make the where condition Wanted SolutionTo make the where condition Wanted Codefrom typing import Optional
from sqlmodel import Field, Session, SQLModel, create_engine, select, col
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
secret_name: str
age: Optional[int] = None
sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
engine = create_engine(sqlite_url, echo=True)
def create_db_and_tables():
SQLModel.metadata.create_all(engine)
def create_heroes():
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)
with Session(engine) as session:
session.add_all([hero_1, hero_2, hero_3])
session.commit()
def select_heroes():
with Session(engine) as session:
statement = select(Hero).where(col(Hero.age) is None, Hero.name.like('%Deadpond%'))
results = session.exec(statement)
for hero in results:
print(hero) AlternativesNo response Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.4 Python Version3.8.5 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 17 comments
-
I believe this is concerned entirely with SQLAlchemy, not with SQLModel, and has to do with the required semantics to construct a
From a cursory search, it does not seem that the |
Beta Was this translation helpful? Give feedback.
-
@mmlynarik Along the lines of |
Beta Was this translation helpful? Give feedback.
-
If it worked as expected, then definitely yes:) |
Beta Was this translation helpful? Give feedback.
-
Ok, I am working on a PR implementing this, repo owners please give me some time 👍 |
Beta Was this translation helpful? Give feedback.
-
You can use |
Beta Was this translation helpful? Give feedback.
-
@tc-imba where to import |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
My understanding is this:
But |
Beta Was this translation helpful? Give feedback.
-
For
|
Beta Was this translation helpful? Give feedback.
-
The same goes for the select(Ticket).where(Ticket.stage_id.in_(stage_ids)) This also let mypy and other type using tool cry out loud like |
Beta Was this translation helpful? Give feedback.
-
I guess the issue title could be updated to smth like |
Beta Was this translation helpful? Give feedback.
-
looking for a team without heros like
isn't compatible with E711 but doesn't work |
Beta Was this translation helpful? Give feedback.
-
In the end, write the code that works for you. PEP 8 is just a recommendation, and flake8 is just a tool. If you prefer to ignore a recommendation, PEP 8 has a section about that as well. |
Beta Was this translation helpful? Give feedback.
-
Besides using |
Beta Was this translation helpful? Give feedback.
-
@patrickwasp this issue is still open. It hasn't be merged to the code base yet. I recommend the following code:
|
Beta Was this translation helpful? Give feedback.
-
this is working on me but: Comparison to |
Beta Was this translation helpful? Give feedback.
-
Why aren't there helper functions like |
Beta Was this translation helpful? Give feedback.
You can use
Hero.age.is_(None)