Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

SQL databases in Python, designed for simplicity, compatibility, and robustness.

License

NotificationsYou must be signed in to change notification settings

Nimitha-jagadeesha/sqlmodel

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQLModel

SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness.

TestPublishCoveragePackage version


Documentation:https://sqlmodel.tiangolo.com

Source Code:https://github.com/fastapi/sqlmodel


SQLModel is a library for interacting with SQL databases from Python code, with Python objects. It is designed to be intuitive, easy to use, highly compatible, and robust.

SQLModel is based on Python type annotations, and powered byPydantic andSQLAlchemy.

The key features are:

  • Intuitive to write: Great editor support. Completion everywhere. Less time debugging. Designed to be easy to use and learn. Less time reading docs.
  • Easy to use: It has sensible defaults and does a lot of work underneath to simplify the code you write.
  • Compatible: It is designed to be compatible withFastAPI, Pydantic, and SQLAlchemy.
  • Extensible: You have all the power of SQLAlchemy and Pydantic underneath.
  • Short: Minimize code duplication. A single type annotation does a lot of work. No need to duplicate models in SQLAlchemy and Pydantic.

Sponsors

SQL Databases in FastAPI

SQLModel is designed to simplify interacting with SQL databases inFastAPI applications, it was created by the sameauthor. 😁

It combines SQLAlchemy and Pydantic and tries to simplify the code you write as much as possible, allowing you to reduce thecode duplication to a minimum, but while getting thebest developer experience possible.

SQLModel is, in fact, a thin layer on top ofPydantic andSQLAlchemy, carefully designed to be compatible with both.

Requirements

A recent and currently supportedversion of Python.

AsSQLModel is based onPydantic andSQLAlchemy, it requires them. They will be automatically installed when you install SQLModel.

Installation

Make sure you create avirtual environment, activate it, and then install SQLModel, for example with:

$pip install sqlmodel---> 100%Successfully installed sqlmodel

Example

For an introduction to databases, SQL, and everything else, see theSQLModel documentation.

Here's a quick example. ✨

A SQL Table

Imagine you have a SQL table calledhero with:

  • id
  • name
  • secret_name
  • age

And you want it to have this data:

idnamesecret_nameage
1DeadpondDive Wilsonnull
2Spider-BoyPedro Parqueadornull
3Rusty-ManTommy Sharp48

Create a SQLModel Model

Then you could create aSQLModel model like this:

fromtypingimportOptionalfromsqlmodelimportField,SQLModelclassHero(SQLModel,table=True):id:Optional[int]=Field(default=None,primary_key=True)name:strsecret_name:strage:Optional[int]=None

That classHero is aSQLModel model, the equivalent of a SQL table in Python code.

And each of those class attributes is equivalent to eachtable column.

Create Rows

Then you couldcreate each row of the table as aninstance of the model:

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)

This way, you can use conventional Python code withclasses andinstances that representtables androws, and that way communicate with theSQL database.

Editor Support

Everything is designed for you to get the best developer experience possible, with the best editor support.

Includingautocompletion:

Andinline errors:

Write to the Database

You can learn a lot more aboutSQLModel by quickly following thetutorial, but if you need a taste right now of how to put all that together and save to the database, you can do this:

fromtypingimportOptionalfromsqlmodelimportField,Session,SQLModel,create_engineclassHero(SQLModel,table=True):id:Optional[int]=Field(default=None,primary_key=True)name:strsecret_name:strage:Optional[int]=Nonehero_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)engine=create_engine("sqlite:///database.db")SQLModel.metadata.create_all(engine)withSession(engine)assession:session.add(hero_1)session.add(hero_2)session.add(hero_3)session.commit()

That will save aSQLite database with the 3 heroes.

Select from the Database

Then you could write queries to select from that same database, for example with:

fromtypingimportOptionalfromsqlmodelimportField,Session,SQLModel,create_engine,selectclassHero(SQLModel,table=True):id:Optional[int]=Field(default=None,primary_key=True)name:strsecret_name:strage:Optional[int]=Noneengine=create_engine("sqlite:///database.db")withSession(engine)assession:statement=select(Hero).where(Hero.name=="Spider-Boy")hero=session.exec(statement).first()print(hero)

Editor Support Everywhere

SQLModel was carefully designed to give you the best developer experience and editor support,even after selecting data from the database:

SQLAlchemy and Pydantic

That classHero is aSQLModel model.

But at the same time, ✨ it is aSQLAlchemy model ✨. So, you can combine it and use it with other SQLAlchemy models, or you could easily migrate applications with SQLAlchemy toSQLModel.

And at the same time, ✨ it is also aPydantic model ✨. You can use inheritance with it to define all yourdata models while avoiding code duplication. That makes it very easy to use withFastAPI.

License

This project is licensed under the terms of theMIT license.

About

SQL databases in Python, designed for simplicity, compatibility, and robustness.

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python99.9%
  • Other0.1%

[8]ページ先頭

©2009-2025 Movatter.jp