Advanced Tutorial - Documentation #1938
Replies: 9 comments
-
|
I agree that the basic usage tutorial is excellent. We'd like to use SQLModel with the async engine from SQLAlchemy. The advanced page indicates this is possible. I was hoping you would post an example at least, even if the guide is not complete yet, @tiangolo? 🙏🏻 🙏🏻 🙏🏻 EDIT: Also, we were previously using imperative mapping from SQLAlchemy. I'm curious if your advanced guide will include instructions on working with MetaData using SQLModel? |
Beta Was this translation helpful? Give feedback.
-
|
Would be awesome to have just a peek about how to use the |
Beta Was this translation helpful? Give feedback.
-
|
I order to use from typing import Optional, cast
import asyncio
from sqlmodel import Field, Session, SQLModel, create_engine, select
from sqlmodel.ext.asyncio.session import AsyncSession, AsyncEngine
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+aiosqlite:///{sqlite_file_name}"
async_engine = AsyncEngine(create_engine(sqlite_url, echo=True))
engine = create_engine(f"sqlite:///{sqlite_file_name}", echo=True)
def create_db_and_tables():
SQLModel.metadata.create_all(engine)
async 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)
async with AsyncSession(async_engine) as session:
session.add(hero_1)
session.add(hero_2)
session.add(hero_3)
await session.commit()
def run():
with Session(engine) as session:
query = select(Hero).where(Hero.name == "Deadpond")
heroes = session.exec(query)
hero = heroes.first()
for hero in heroes:
print(hero.name)
async def select_heroes():
async with AsyncSession(async_engine) as session:
query = select(Hero).where(Hero.name == "Deadpond")
# heroes = await session.exec(cast(Select[Hero], query))
heroes = await session.exec(query)
hero = heroes.one()
print(hero.name)
# for heroe in heroes:
# print(heroe.name)
async def main():
await create_heroes()
await select_heroes()
if __name__ == "__main__":
# create_db_and_tables()
asyncio.run(main())Everything is OK, But I don’t know if there will be other side effects. |
Beta Was this translation helpful? Give feedback.
-
|
You can find a sample here https://github.com/jonra1993/fastapi-alembic-sqlmodel-async. It can help others trying to create an async setup with SQLModel. |
Beta Was this translation helpful? Give feedback.
-
|
As a convert-from-Flask, I'm struggling to make fastAPI and SQLModel work together in a non-trivial use case, with many models, relationships and business logic abstractions that preclude putting everything into single models, routes and crud files. The core foundation is working well, but I keep slipping off the edges 🤕 An unexpected "feature" of the fastAPI tutorial (users and items) was that the majority of With SQLModel, we get a different example. While conceptually similar, it is subtly structurally different - from directory layout and modularity, to design mindset and completeness. For someone struggling to understand the magic that sifts between the cracks, these differences are a search engine comprehension killer :-( All this is to say that, in the best of worlds, going back and updating the fastAPI tutorial to use SQLModel instead of BaseModel would, in my mind, be a fantastic "advanced" tutorial. With that in place, a "a world class async app" tutorial with a modular, maintainable file structure would be nice, too 👍 |
Beta Was this translation helpful? Give feedback.
-
|
Are there any plans for publishing this documentation? Would love to learn more on how to use |
Beta Was this translation helpful? Give feedback.
-
|
Collating useful guides i've come across: Self referential relationships: #127 (comment) |
Beta Was this translation helpful? Give feedback.
-
|
I see that the What's the current working flow everyone's using for async support with SQLModel |
Beta Was this translation helpful? Give feedback.
-
|
related(for ref) #706 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Just want to ask about advanced tutorial?
Is there a plan to add documentation and what is timeline?
https://sqlmodel.tiangolo.com/advanced/
Tutorial for basic usage is excellent. Thanks to Tiangolo for making it so great and helpful.
Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.7
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions