Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Please support latest SQLAlchemy or pin it #2996

Closed
samuelcolvin opened this issue Mar 26, 2021 · 5 comments
Closed

Please support latest SQLAlchemy or pin it #2996

samuelcolvin opened this issue Mar 26, 2021 · 5 comments
Labels
question Question or problem question-migrate

Comments

@samuelcolvin
Copy link
Sponsor Collaborator

samuelcolvin commented Mar 26, 2021

Hi @tiangolo, fastapi tests are currently failing and therefore causing pydantic tests to fail.

See pydantic/pydantic#2584, fastapi is not compatible with the v1.4 of SQLAlchemy which was released earlier in March, I've had to pin to SQLAlchemy==1.3.23

Please could we fix fastapi (the incompatibility looks like it might be trivial) or pin the dependency?

Once master of fastapi is fixed, we'll need to remember to also remove the hack from pydantic.

@samuelcolvin samuelcolvin added the question Question or problem label Mar 26, 2021
@Kludex
Copy link
Sponsor Collaborator

Kludex commented Mar 26, 2021

@samuelcolvin This comes from encode/databases. @PrettyWood created a PR there: encode/databases#299

If this is not solved, @tiangolo will probably fix this in the next weeks.

@PrettyWood
Copy link
Contributor

https://github.com/encode/databases/releases/tag/0.4.3 is out! @samuelcolvin No need for your fix anymore!

@gjask
Copy link

gjask commented Apr 12, 2021

Also it seems to me, that SQLAlchemy 1.4 has started to support async for its ORM. It would be great to have it available in fastapi.

https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html#synopsis-orm

@ghost
Copy link

ghost commented Jun 23, 2021

Is the use of databases still necessary/desirable? I tried to replace the current async/core-version with pure sqlalchemy, to be more in line with the sqlalchemy documentation, and it kind of works even though I probably understand only 10% of what I'm doing and wouldn't notice if this was horribly bad as long as it won't crash and burn.

meta = MetaData()

t1 = Table(
    "t1", meta, Column("id", Integer, primary_key=True), Column("name", String)
)

engine = create_async_engine(
        "sqlite+aiosqlite:///./sqlite.db",
        echo=True, connect_args={"check_same_thread": False}
    )
app = FastAPI()

@app.on_event("startup")
async def startup():
    async with engine.begin() as conn:
        await conn.run_sync(meta.create_all)
        
@app.on_event("shutdown")
async def shutdown():
    await engine.dispose()
        

@app.get("/")
async def root():
    return {"message": "Hello World!"}
    
@app.get("/read")
async def get_read():
    async with engine.connect() as conn:
        result = await conn.execute(t1.select())
        return result.fetchall()
    
@app.get("/insert")
async def get_insert():
    async with engine.connect() as conn:
        result = await conn.execute(t1.insert(), [{"name": "some name 1"}, {"name": "some name 2"}])
        await conn.commit()
    return {"message": "Done"}

@rinckd
Copy link

rinckd commented Jun 26, 2021

Michael Kennedy goes over async SQLAlchemy with FastAPI in one of his latest courses: https://github.com/talkpython/web-applications-with-fastapi-course/tree/main/code/ch8-async-databases

It's definitely interesting to play around with, but the SQLAlchemy asyncio extension is still considered beta software until the 2.0 release.
image

Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #9030 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

6 participants