METADATA 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. Metadata-Version: 2.1
  2. Name: sqlmodel
  3. Version: 0.0.22
  4. Summary: SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness.
  5. Author-Email: =?utf-8?q?Sebasti=C3=A1n_Ram=C3=ADrez?= <tiangolo@gmail.com>
  6. Classifier: Development Status :: 4 - Beta
  7. Classifier: Framework :: AsyncIO
  8. Classifier: Intended Audience :: Developers
  9. Classifier: Intended Audience :: Science/Research
  10. Classifier: Intended Audience :: System Administrators
  11. Classifier: License :: OSI Approved :: MIT License
  12. Classifier: Programming Language :: Python :: 3 :: Only
  13. Classifier: Programming Language :: Python :: 3.7
  14. Classifier: Programming Language :: Python :: 3.8
  15. Classifier: Programming Language :: Python :: 3.9
  16. Classifier: Programming Language :: Python :: 3.10
  17. Classifier: Programming Language :: Python :: 3.11
  18. Classifier: Programming Language :: Python :: 3.12
  19. Classifier: Topic :: Database
  20. Classifier: Topic :: Database :: Database Engines/Servers
  21. Classifier: Topic :: Internet
  22. Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
  23. Classifier: Topic :: Internet :: WWW/HTTP
  24. Classifier: Typing :: Typed
  25. Project-URL: Homepage, https://github.com/fastapi/sqlmodel
  26. Project-URL: Documentation, https://sqlmodel.tiangolo.com
  27. Project-URL: Repository, https://github.com/fastapi/sqlmodel
  28. Project-URL: Issues, https://github.com/fastapi/sqlmodel/issues
  29. Project-URL: Changelog, https://sqlmodel.tiangolo.com/release-notes/
  30. Requires-Python: >=3.7
  31. Requires-Dist: SQLAlchemy<2.1.0,>=2.0.14
  32. Requires-Dist: pydantic<3.0.0,>=1.10.13
  33. Description-Content-Type: text/markdown
  34. <p align="center">
  35. <a href="https://sqlmodel.tiangolo.com"><img src="https://sqlmodel.tiangolo.com/img/logo-margin/logo-margin-vector.svg#only-light" alt="SQLModel"></a>
  36. </p>
  37. <p align="center">
  38. <em>SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness.</em>
  39. </p>
  40. <p align="center">
  41. <a href="https://github.com/fastapi/sqlmodel/actions?query=workflow%3ATest" target="_blank">
  42. <img src="https://github.com/fastapi/sqlmodel/workflows/Test/badge.svg" alt="Test">
  43. </a>
  44. <a href="https://github.com/fastapi/sqlmodel/actions?query=workflow%3APublish" target="_blank">
  45. <img src="https://github.com/fastapi/sqlmodel/workflows/Publish/badge.svg" alt="Publish">
  46. </a>
  47. <a href="https://coverage-badge.samuelcolvin.workers.dev/redirect/fastapi/sqlmodel" target="_blank">
  48. <img src="https://coverage-badge.samuelcolvin.workers.dev/fastapi/sqlmodel.svg" alt="Coverage">
  49. <a href="https://pypi.org/project/sqlmodel" target="_blank">
  50. <img src="https://img.shields.io/pypi/v/sqlmodel?color=%2334D058&label=pypi%20package" alt="Package version">
  51. </a>
  52. </p>
  53. ---
  54. **Documentation**: <a href="https://sqlmodel.tiangolo.com" target="_blank">https://sqlmodel.tiangolo.com</a>
  55. **Source Code**: <a href="https://github.com/fastapi/sqlmodel" target="_blank">https://github.com/fastapi/sqlmodel</a>
  56. ---
  57. SQLModel is a library for interacting with <abbr title='Also called "Relational databases"'>SQL databases</abbr> from Python code, with Python objects. It is designed to be intuitive, easy to use, highly compatible, and robust.
  58. **SQLModel** is based on Python type annotations, and powered by <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic</a> and <a href="https://sqlalchemy.org/" class="external-link" target="_blank">SQLAlchemy</a>.
  59. The key features are:
  60. * **Intuitive to write**: Great editor support. <abbr title="also known as auto-complete, autocompletion, IntelliSense">Completion</abbr> everywhere. Less time debugging. Designed to be easy to use and learn. Less time reading docs.
  61. * **Easy to use**: It has sensible defaults and does a lot of work underneath to simplify the code you write.
  62. * **Compatible**: It is designed to be compatible with **FastAPI**, Pydantic, and SQLAlchemy.
  63. * **Extensible**: You have all the power of SQLAlchemy and Pydantic underneath.
  64. * **Short**: Minimize code duplication. A single type annotation does a lot of work. No need to duplicate models in SQLAlchemy and Pydantic.
  65. ## Sponsors
  66. <!-- sponsors -->
  67. <a href="https://www.govcert.lu" target="_blank" title="This project is being supported by GOVCERT.LU"><img src="https://sqlmodel.tiangolo.com/img/sponsors/govcert.png"></a>
  68. <!-- /sponsors -->
  69. ## SQL Databases in FastAPI
  70. <a href="https://fastapi.tiangolo.com" target="_blank"><img src="https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" style="width: 20%;"></a>
  71. **SQLModel** is designed to simplify interacting with SQL databases in <a href="https://fastapi.tiangolo.com" class="external-link" target="_blank">FastAPI</a> applications, it was created by the same <a href="https://tiangolo.com/" class="external-link" target="_blank">author</a>. 😁
  72. It combines SQLAlchemy and Pydantic and tries to simplify the code you write as much as possible, allowing you to reduce the **code duplication to a minimum**, but while getting the **best developer experience** possible.
  73. **SQLModel** is, in fact, a thin layer on top of **Pydantic** and **SQLAlchemy**, carefully designed to be compatible with both.
  74. ## Requirements
  75. A recent and currently supported <a href="https://www.python.org/downloads/" class="external-link" target="_blank">version of Python</a>.
  76. As **SQLModel** is based on **Pydantic** and **SQLAlchemy**, it requires them. They will be automatically installed when you install SQLModel.
  77. ## Installation
  78. Make sure you create a <a href="https://sqlmodel.tiangolo.com/virtual-environments/" class="external-link" target="_blank">virtual environment</a>, activate it, and then install SQLModel, for example with:
  79. <div class="termy">
  80. ```console
  81. $ pip install sqlmodel
  82. ---> 100%
  83. Successfully installed sqlmodel
  84. ```
  85. </div>
  86. ## Example
  87. For an introduction to databases, SQL, and everything else, see the <a href="https://sqlmodel.tiangolo.com/databases/" target="_blank">SQLModel documentation</a>.
  88. Here's a quick example. ✨
  89. ### A SQL Table
  90. Imagine you have a SQL table called `hero` with:
  91. * `id`
  92. * `name`
  93. * `secret_name`
  94. * `age`
  95. And you want it to have this data:
  96. | id | name | secret_name | age |
  97. -----|------|-------------|------|
  98. | 1 | Deadpond | Dive Wilson | null |
  99. | 2 | Spider-Boy | Pedro Parqueador | null |
  100. | 3 | Rusty-Man | Tommy Sharp | 48 |
  101. ### Create a SQLModel Model
  102. Then you could create a **SQLModel** model like this:
  103. ```Python
  104. from typing import Optional
  105. from sqlmodel import Field, SQLModel
  106. class Hero(SQLModel, table=True):
  107. id: Optional[int] = Field(default=None, primary_key=True)
  108. name: str
  109. secret_name: str
  110. age: Optional[int] = None
  111. ```
  112. That class `Hero` is a **SQLModel** model, the equivalent of a SQL table in Python code.
  113. And each of those class attributes is equivalent to each **table column**.
  114. ### Create Rows
  115. Then you could **create each row** of the table as an **instance** of the model:
  116. ```Python
  117. hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
  118. hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
  119. hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)
  120. ```
  121. This way, you can use conventional Python code with **classes** and **instances** that represent **tables** and **rows**, and that way communicate with the **SQL database**.
  122. ### Editor Support
  123. Everything is designed for you to get the best developer experience possible, with the best editor support.
  124. Including **autocompletion**:
  125. <img class="shadow" src="https://sqlmodel.tiangolo.com/img/index/autocompletion01.png">
  126. And **inline errors**:
  127. <img class="shadow" src="https://sqlmodel.tiangolo.com/img/index/inline-errors01.png">
  128. ### Write to the Database
  129. You can learn a lot more about **SQLModel** by quickly following the **tutorial**, but if you need a taste right now of how to put all that together and save to the database, you can do this:
  130. ```Python hl_lines="18 21 23-27"
  131. from typing import Optional
  132. from sqlmodel import Field, Session, SQLModel, create_engine
  133. class Hero(SQLModel, table=True):
  134. id: Optional[int] = Field(default=None, primary_key=True)
  135. name: str
  136. secret_name: str
  137. age: Optional[int] = None
  138. hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
  139. hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
  140. hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)
  141. engine = create_engine("sqlite:///database.db")
  142. SQLModel.metadata.create_all(engine)
  143. with Session(engine) as session:
  144. session.add(hero_1)
  145. session.add(hero_2)
  146. session.add(hero_3)
  147. session.commit()
  148. ```
  149. That will save a **SQLite** database with the 3 heroes.
  150. ### Select from the Database
  151. Then you could write queries to select from that same database, for example with:
  152. ```Python hl_lines="15-18"
  153. from typing import Optional
  154. from sqlmodel import Field, Session, SQLModel, create_engine, select
  155. class Hero(SQLModel, table=True):
  156. id: Optional[int] = Field(default=None, primary_key=True)
  157. name: str
  158. secret_name: str
  159. age: Optional[int] = None
  160. engine = create_engine("sqlite:///database.db")
  161. with Session(engine) as session:
  162. statement = select(Hero).where(Hero.name == "Spider-Boy")
  163. hero = session.exec(statement).first()
  164. print(hero)
  165. ```
  166. ### Editor Support Everywhere
  167. **SQLModel** was carefully designed to give you the best developer experience and editor support, **even after selecting data** from the database:
  168. <img class="shadow" src="https://sqlmodel.tiangolo.com/img/index/autocompletion02.png">
  169. ## SQLAlchemy and Pydantic
  170. That class `Hero` is a **SQLModel** model.
  171. But at the same time, ✨ it is a **SQLAlchemy** model ✨. So, you can combine it and use it with other SQLAlchemy models, or you could easily migrate applications with SQLAlchemy to **SQLModel**.
  172. And at the same time, ✨ it is also a **Pydantic** model ✨. You can use inheritance with it to define all your **data models** while avoiding code duplication. That makes it very easy to use with **FastAPI**.
  173. ## License
  174. This project is licensed under the terms of the [MIT license](https://github.com/fastapi/sqlmodel/blob/main/LICENSE).