TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

SQLModel – SQL Databases in FastAPI

224 点作者 dmart超过 3 年前

14 条评论

tiangolo超过 3 年前
Hey all! Author here!<p>Thanks for sharing!<p>This is the biggest thing I&#x27;ve built since FastAPI and Typer...<p>SQLModel is a library for interacting with SQL DBs, based on Python type hints.<p>Each model is both a Pydantic and SQLAlchemy model, and it&#x27;s all optimized for FastAPI.<p>GitHub here: <a href="https:&#x2F;&#x2F;github.com&#x2F;tiangolo&#x2F;sqlmodel" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tiangolo&#x2F;sqlmodel</a><p>More info in this Twitter thread: <a href="https:&#x2F;&#x2F;twitter.com&#x2F;tiangolo&#x2F;status&#x2F;1430252646968004612" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;tiangolo&#x2F;status&#x2F;1430252646968004612</a><p>And the docs are here: <a href="https:&#x2F;&#x2F;sqlmodel.tiangolo.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;sqlmodel.tiangolo.com&#x2F;</a>
评论 #28296150 未加载
评论 #28297569 未加载
评论 #28295823 未加载
评论 #28300101 未加载
评论 #28295674 未加载
评论 #28296821 未加载
评论 #28301456 未加载
nitred超过 3 年前
I faced this exact problem a few days ago i.e. having to build one model for SQLAlchemy and an identical Pydantic model to validate the data (outside of the FastAPI context)! Will be using it the next I need it.<p>@tiangolo I would like to use this for production at work, however I&#x27;m a bit hesitant about using the 0.0.x versions. Would you recommend me to wait until version 0.1.0 or 1.0.0?
评论 #28299996 未加载
faizshah超过 3 年前
Would this support pydantic dynamic models? <a href="https:&#x2F;&#x2F;pydantic-docs.helpmanual.io&#x2F;usage&#x2F;models&#x2F;#dynamic-model-creation" rel="nofollow">https:&#x2F;&#x2F;pydantic-docs.helpmanual.io&#x2F;usage&#x2F;models&#x2F;#dynamic-mo...</a><p>Or rather can I pass an existing pydantic class to SQLModel somehow?
评论 #28300013 未加载
afrnz超过 3 年前
Well done Sébastian - I have been waiting for this. Having seperate SQLAlchemy and pydantic models always seemed like a spot where the whole FastAPI developer experience wasn&#x27;t ideal imo. Looking forward to try this out :)
sandGorgon超过 3 年前
here&#x27;s my attempt at unifying SQLAlchemy and Pydantic. I use the new dataclasses support in both libraries to do this.<p><a href="https:&#x2F;&#x2F;gist.github.com&#x2F;sandys&#x2F;671b8b86ba913e6436d4cb22d04b135f" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;sandys&#x2F;671b8b86ba913e6436d4cb22d04b1...</a><p>This example trys to play with some quirky table structures - Mixins, Surrogate primary keys, index, etc<p>Both async and sync modes.<p>I like what @tiangolo has done with SQLModel, but i suspect that a lot of people who are already using sqlalchemy in production will prefer to unify via dataclasses versus switching the DX to a new library.<p>And this is possible today.
emptysea超过 3 年前
I&#x27;m curious how the type hints for the `__init__` method are derived from the properties in the class without a type checking plugin<p>Nothing stuck out to me when perusing through the code<p>Edit: seems pylance can figure it out but mypy can&#x27;t, maybe pylance is special casing something?<p><pre><code> from typing import Optional from sqlmodel import Field, SQLModel class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str secret_name: str age: Optional[int] = None reveal_type(Hero.__init__) # pylance: Type of &quot;Hero.__init__&quot; is &quot;(self: Hero, *, id: int | None = Field(default=None, primary_key=True), name: str, secret_name: str, age: int | None = None) -&gt; None&quot; # mypy: main.py:18:13: note: Revealed type is &quot;def (__pydantic_self__: sqlmodel.main.SQLModel, **data: Any)&quot;</code></pre>
评论 #28300033 未加载
评论 #28297965 未加载
评论 #28296863 未加载
woile超过 3 年前
Super nice! Always delivering quality tools. This also seems to play good with starlette as well. And I see async and migrations are coming soon. Alembic is good but a bit cumbersome to use, I hope to see some improvements there as well
评论 #28300047 未加载
gabereiser超过 3 年前
While I&#x27;m a fan of both pydantic and SQLAlchemy, I feel like support for this should be more clear in pydantic. We have the `.from_orm` method and we have sqlalchemy model metadata. This project wraps them nicely but I shouldn&#x27;t have to have separate glue code for this. Until we have cleaner pydantic&#x2F;sqlalchemy integration for FastAPI, the OP&#x27;s project will do just nicely. Great job. I fully expect FastAPI with it&#x27;s reliance on pydantic to help push better support for ORM models though. Anyway, enough ranting and back to coding.
hangonhn超过 3 年前
Does it work outside of FastAPI or is it tied to it? We sort of shoved SQLAlchemy into Starlette (which FastAPI also uses) and it would be great if this library also works with Starlette. Thanks!
评论 #28295595 未加载
etimberg超过 3 年前
Does this have async query support? I’ve been using ormar lately with FastAPI and found it a pleasure to work with. I even built a number of PG specific extensions easily for things like JSONB and native UUID fields
评论 #28300051 未加载
easton超过 3 年前
I was literally an hour ago trying to figure out how to get my complicated Pydantic models to convert correctly to the SQLAlchemy models I made. Thanks tiangolo!
评论 #28300058 未加载
agordhandas超过 3 年前
Been a fan of FastAPI! Does this support async?
评论 #28296602 未加载
jkrubin超过 3 年前
If tiangolo is involved it will be very polished by 0.1.0
评论 #28300059 未加载
gigatexal超过 3 年前
That it uses SQLAlchemy bring the scenes is a deal breaker for me.
评论 #28296449 未加载
评论 #28296433 未加载