diff --git a/.github/actions/comment-docs-preview-in-pr/Dockerfile b/.github/actions/comment-docs-preview-in-pr/Dockerfile
index 4f20c5f10b..5dfc6d4404 100644
--- a/.github/actions/comment-docs-preview-in-pr/Dockerfile
+++ b/.github/actions/comment-docs-preview-in-pr/Dockerfile
@@ -1,6 +1,6 @@
-FROM python:3.7
+FROM python:3.8
-RUN pip install httpx "pydantic==1.5.1" pygithub
+RUN pip install httpx "pydantic==1.8.2" pygithub
COPY ./app /app
diff --git a/.github/actions/watch-previews/Dockerfile b/.github/actions/watch-previews/Dockerfile
index b8cc64d948..e58bc18f62 100644
--- a/.github/actions/watch-previews/Dockerfile
+++ b/.github/actions/watch-previews/Dockerfile
@@ -1,6 +1,6 @@
-FROM python:3.7
+FROM python:3.8
-RUN pip install httpx PyGithub "pydantic==1.5.1"
+RUN pip install httpx PyGithub "pydantic==1.8.2"
COPY ./app /app
diff --git a/.github/workflows/build-docs.yml b/.github/workflows/build-docs.yml
index 6400691533..212b5e9389 100644
--- a/.github/workflows/build-docs.yml
+++ b/.github/workflows/build-docs.yml
@@ -23,7 +23,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
- python-version: "3.7"
+ python-version: "3.8"
# Allow debugging with tmate
- name: Setup tmate session
uses: mxschmitt/action-tmate@v3
@@ -37,21 +37,16 @@ jobs:
key: ${{ runner.os }}-python-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}-root-docs
- name: Install poetry
if: steps.cache.outputs.cache-hit != 'true'
- # TODO: remove python -m pip install --force git+https://github.com/python-poetry/poetry-core.git@ad33bc2
- # once there's a release of Poetry 1.2.x including poetry-core > 1.1.0a6
- # Ref: https://github.com/python-poetry/poetry-core/pull/188
run: |
python -m pip install --upgrade pip
- python -m pip install --force git+https://github.com/python-poetry/poetry-core.git@ad33bc2
- python -m pip install "poetry==1.2.0a2"
- python -m poetry plugin add poetry-version-plugin
+ python -m pip install poetry
- name: Configure poetry
run: python -m poetry config virtualenvs.create false
- name: Install Dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: python -m poetry install
- name: Install Material for MkDocs Insiders
- if: github.event.pull_request.head.repo.fork == false && steps.cache.outputs.cache-hit != 'true'
+ if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false && steps.cache.outputs.cache-hit != 'true'
run: python -m poetry run pip install git+https://${{ secrets.ACTIONS_TOKEN }}@github.com/squidfunk/mkdocs-material-insiders.git
- uses: actions/cache@v3
with:
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
index f3c1e980a6..c6f1313d70 100644
--- a/.github/workflows/publish.yml
+++ b/.github/workflows/publish.yml
@@ -19,7 +19,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
- python-version: "3.7"
+ python-version: "3.8"
# Allow debugging with tmate
- name: Setup tmate session
uses: mxschmitt/action-tmate@v3
@@ -33,14 +33,9 @@ jobs:
key: ${{ runner.os }}-python-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}-root
- name: Install poetry
if: steps.cache.outputs.cache-hit != 'true'
- # TODO: remove python -m pip install --force git+https://github.com/python-poetry/poetry-core.git@ad33bc2
- # once there's a release of Poetry 1.2.x including poetry-core > 1.1.0a6
- # Ref: https://github.com/python-poetry/poetry-core/pull/188
run: |
python -m pip install --upgrade pip
- python -m pip install --force git+https://github.com/python-poetry/poetry-core.git@ad33bc2
- python -m pip install "poetry==1.2.0a2"
- python -m poetry plugin add poetry-version-plugin
+ python -m pip install poetry
- name: Configure poetry
run: python -m poetry config virtualenvs.create false
- name: Install Dependencies
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 585ffc0455..895ccc31ff 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
- python-version: ["3.6.15", "3.7", "3.8", "3.9", "3.10"]
+ python-version: ["3.8", "3.9", "3.10"]
fail-fast: false
steps:
@@ -40,21 +40,15 @@ jobs:
key: ${{ runner.os }}-python-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}-root
- name: Install poetry
if: steps.cache.outputs.cache-hit != 'true'
- # TODO: remove python -m pip install --force git+https://github.com/python-poetry/poetry-core.git@ad33bc2
- # once there's a release of Poetry 1.2.x including poetry-core > 1.1.0a6
- # Ref: https://github.com/python-poetry/poetry-core/pull/188
run: |
python -m pip install --upgrade pip
- python -m pip install --force git+https://github.com/python-poetry/poetry-core.git@ad33bc2
- python -m pip install "poetry==1.2.0a2"
- python -m poetry plugin add poetry-version-plugin
+ python -m pip install poetry
- name: Configure poetry
run: python -m poetry config virtualenvs.create false
- name: Install Dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: python -m poetry install
- name: Lint
- if: ${{ matrix.python-version != '3.6.15' }}
run: python -m poetry run bash scripts/lint.sh
- run: mkdir coverage
- name: Test
diff --git a/README.md b/README.md
index 5721f1cdb0..50a02cb231 100644
--- a/README.md
+++ b/README.md
@@ -50,7 +50,7 @@ It combines SQLAlchemy and Pydantic and tries to simplify the code you write as
## Requirements
-A recent and currently supported version of Python (right now, Python supports versions 3.6 and above).
+A recent and currently supported version of Python (right now, Python supports versions 3.8 and above).
As **SQLModel** is based on **Pydantic** and **SQLAlchemy**, it requires them. They will be automatically installed when you install SQLModel.
diff --git a/docs/advanced/asyncio.md b/docs/advanced/asyncio.md
new file mode 100644
index 0000000000..2405ff9814
--- /dev/null
+++ b/docs/advanced/asyncio.md
@@ -0,0 +1,87 @@
+# Asyncio Session and Engine
+
+In some cases, you may use asynchronous session and engine.
+This is especially important if you are building a highly loaded service.
+
+The advantage of asynchronous over synchronous is that our application does
+not wait for a response from a query to the database,
+but performs other tasks until it receives a response.
+
+To use the asynchronous mode, we need to install the asynchronous driver.
+
+There is aiosqlite driver for sqlite.
+
+
+
+```console
+$ pip install aiosqlite
+---> 100%
+Successfully installed aiosqlite
+```
+
+
+
+
+Next, you can import the asyncio module, or use the async framework.
+In this case, we will use the asyncio library.
+
+```Python
+{!./docs_src/advanced/asyncio/tutorial001.py[ln:1]!}
+```
+
+Importing the asynchronous session and engine.
+
+```Python
+{!./docs_src/advanced/asyncio/tutorial001.py[ln:4]!}
+```
+
+Create Model Hero.
+
+```Python
+{!./docs_src/advanced/asyncio/tutorial001.py[ln:7-11]!}
+```
+
+Setting engine.
+
+Pay attention to the connection url,
+it clearly specifies which driver to use to connect to the database.
+
+```Python
+{!./docs_src/advanced/asyncio/tutorial001.py[ln:14-17]!}
+```
+
+Create table database.
+
+```Python
+{!./docs_src/advanced/asyncio/tutorial001.py[ln:20-22]!}
+```
+
+Create rows.
+
+```Python
+{!./docs_src/advanced/asyncio/tutorial001.py[ln:25-37]!}
+```
+
+Read rows. Using the asynchronous context manager.
+
+```Python
+{!./docs_src/advanced/asyncio/tutorial001.py[ln:40-45]!}
+```
+
+We connect all the functions together.
+
+```Python
+{!./docs_src/advanced/asyncio/tutorial001.py[ln:48-51]!}
+```
+
+Running an asynchronous function (python3.7+)
+
+```Python
+{!./docs_src/advanced/asyncio/tutorial001.py[ln:53-55]!}
+```
+
+Full example.
+
+```Python
+{!./docs_src/advanced/asyncio/tutorial001.py!}
+```
\ No newline at end of file
diff --git a/docs/advanced/index.md b/docs/advanced/index.md
index f6178249ce..9e34ec947e 100644
--- a/docs/advanced/index.md
+++ b/docs/advanced/index.md
@@ -5,6 +5,8 @@ The **Advanced User Guide** is gradually growing, you can already read about som
At some point it will include:
* How to use `async` and `await` with the async session.
+* See: `docs_src\tutorial_async` for working examples as equivalents to `docs_src\tutorial`
+* Also see `tests\tutorial_async` for fully functioning pytest tests against the examples
* How to run migrations.
* How to combine **SQLModel** models with SQLAlchemy.
* ...and more. 🤓
diff --git a/docs/contributing.md b/docs/contributing.md
index f2964fba9b..0cf28a5ceb 100644
--- a/docs/contributing.md
+++ b/docs/contributing.md
@@ -8,7 +8,7 @@ If you already cloned the repository and you know that you need to deep dive in
### Python
-SQLModel supports Python 3.6 and above, but for development you should have at least **Python 3.7**.
+SQLModel supports Python 3.8 and above.
### Poetry
diff --git a/docs/features.md b/docs/features.md
index 09de0c17f9..718b1522ca 100644
--- a/docs/features.md
+++ b/docs/features.md
@@ -12,7 +12,7 @@ Nevertheless, SQLModel is completely **independent** of FastAPI and can be used
## Just Modern Python
-It's all based on standard modern **Python** type annotations. No new syntax to learn. Just standard modern Python.
+It's all based on standard modern **Python** type annotations. No new syntax to learn. Just standard modern Python.
If you need a 2 minute refresher of how to use Python types (even if you don't use SQLModel or FastAPI), check the FastAPI tutorial section: Python types intro.
diff --git a/docs/index.md b/docs/index.md
index 5721f1cdb0..50a02cb231 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -50,7 +50,7 @@ It combines SQLAlchemy and Pydantic and tries to simplify the code you write as
## Requirements
-A recent and currently supported version of Python (right now, Python supports versions 3.6 and above).
+A recent and currently supported version of Python (right now, Python supports versions 3.8 and above).
As **SQLModel** is based on **Pydantic** and **SQLAlchemy**, it requires them. They will be automatically installed when you install SQLModel.
diff --git a/docs/tutorial/fastapi/tests.md b/docs/tutorial/fastapi/tests.md
index f817a883a1..c91eb52aed 100644
--- a/docs/tutorial/fastapi/tests.md
+++ b/docs/tutorial/fastapi/tests.md
@@ -389,7 +389,7 @@ Now we can run the tests with `pytest` and see the results:
$ pytest
============= test session starts ==============
-platform linux -- Python 3.7.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
+platform linux -- Python 3.8.1, pytest-7.2.0, py-1.10.0, pluggy-0.13.1
rootdir: /home/user/code/sqlmodel-tutorial
collected 7 items
diff --git a/docs/tutorial/index.md b/docs/tutorial/index.md
index 33cf6226c4..5e31c9b574 100644
--- a/docs/tutorial/index.md
+++ b/docs/tutorial/index.md
@@ -64,7 +64,7 @@ $ cd sqlmodel-tutorial
Make sure you have an officially supported version of Python.
-Currently it is **Python 3.6** and above (Python 3.5 was already deprecated).
+Currently it is **Python 3.8** and above (Python 3.6 and 3.7 were already deprecated).
You can check which version you have with:
@@ -72,7 +72,7 @@ You can check which version you have with:
```console
$ python3 --version
-Python 3.6.9
+Python 3.8.1
```
@@ -84,8 +84,6 @@ You might want to try with the specific versions, for example with:
* `python3.10`
* `python3.9`
* `python3.8`
-* `python3.7`
-* `python3.6`
The code would look like this:
diff --git a/docs_src/advanced/asyncio/__init__.py b/docs_src/advanced/asyncio/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/docs_src/advanced/asyncio/tutorial001.py b/docs_src/advanced/asyncio/tutorial001.py
new file mode 100644
index 0000000000..d69dbafbac
--- /dev/null
+++ b/docs_src/advanced/asyncio/tutorial001.py
@@ -0,0 +1,55 @@
+import asyncio
+from typing import Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine, select
+
+
+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}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables():
+ async with engine.begin() as session:
+ await session.run_sync(SQLModel.metadata.create_all)
+
+
+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)
+
+ session = AsyncSession(engine)
+ session.add(hero_1)
+ session.add(hero_2)
+ session.add(hero_3)
+
+ await session.commit()
+
+ await session.close()
+
+
+async def select_heroes():
+ async with AsyncSession(engine) as session:
+ statement = select(Hero)
+ results = await session.exec(statement)
+ for hero in results:
+ print(hero)
+
+
+async def main():
+ await create_db_and_tables()
+ await create_heroes()
+ await select_heroes()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/docs_src/advanced/asyncio/tutorial001_py36.py b/docs_src/advanced/asyncio/tutorial001_py36.py
new file mode 100644
index 0000000000..968327fa51
--- /dev/null
+++ b/docs_src/advanced/asyncio/tutorial001_py36.py
@@ -0,0 +1,56 @@
+import asyncio
+from typing import Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine, select
+
+
+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}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables():
+ async with engine.begin() as session:
+ await session.run_sync(SQLModel.metadata.create_all)
+
+
+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)
+
+ session = AsyncSession(engine)
+ session.add(hero_1)
+ session.add(hero_2)
+ session.add(hero_3)
+
+ await session.commit()
+
+ await session.close()
+
+
+async def select_heroes():
+ async with AsyncSession(engine) as session:
+ statement = select(Hero)
+ results = await session.exec(statement)
+ for hero in results:
+ print(hero)
+
+
+async def main():
+ await create_db_and_tables()
+ await create_heroes()
+ await select_heroes()
+
+
+if __name__ == "__main__":
+ loop = asyncio.get_event_loop()
+ loop.run_until_complete(asyncio.wait([main()]))
diff --git a/docs_src/tutorial/code_structure/tutorial001/app.py b/docs_src/tutorial/code_structure/tutorial001/app.py
index 065f8a78b5..0207b1b625 100644
--- a/docs_src/tutorial/code_structure/tutorial001/app.py
+++ b/docs_src/tutorial/code_structure/tutorial001/app.py
@@ -9,7 +9,10 @@ def create_heroes():
team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
hero_deadpond = Hero(
- name="Deadpond", secret_name="Dive Wilson", team=team_z_force
+ name="Deadpond",
+ secret_name="Dive Wilson",
+ team=team_z_force,
+ experience_points=1,
)
session.add(hero_deadpond)
session.commit()
diff --git a/docs_src/tutorial/code_structure/tutorial001/models.py b/docs_src/tutorial/code_structure/tutorial001/models.py
index 8e2647b3c4..7307e414d6 100644
--- a/docs_src/tutorial/code_structure/tutorial001/models.py
+++ b/docs_src/tutorial/code_structure/tutorial001/models.py
@@ -1,5 +1,6 @@
from typing import List, Optional
+from pydantic import condecimal
from sqlmodel import Field, Relationship, SQLModel
@@ -16,6 +17,9 @@ class Hero(SQLModel, table=True):
name: str = Field(index=True)
secret_name: str
age: Optional[int] = Field(default=None, index=True)
+ experience_points: condecimal(max_digits=6, decimal_places=3) = Field(
+ default=0, nullable=False, index=True
+ )
team_id: Optional[int] = Field(default=None, foreign_key="team.id")
team: Optional[Team] = Relationship(back_populates="heroes")
diff --git a/docs_src/tutorial/code_structure/tutorial002/app.py b/docs_src/tutorial/code_structure/tutorial002/app.py
index 8afaee7c16..208f95a2b4 100644
--- a/docs_src/tutorial/code_structure/tutorial002/app.py
+++ b/docs_src/tutorial/code_structure/tutorial002/app.py
@@ -10,7 +10,10 @@ def create_heroes():
team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
hero_deadpond = Hero(
- name="Deadpond", secret_name="Dive Wilson", team=team_z_force
+ name="Deadpond",
+ secret_name="Dive Wilson",
+ team=team_z_force,
+ experience_points=1,
)
session.add(hero_deadpond)
session.commit()
diff --git a/docs_src/tutorial/code_structure/tutorial002/hero_model.py b/docs_src/tutorial/code_structure/tutorial002/hero_model.py
index 06dd9c6dfd..e4c9af1d5d 100644
--- a/docs_src/tutorial/code_structure/tutorial002/hero_model.py
+++ b/docs_src/tutorial/code_structure/tutorial002/hero_model.py
@@ -1,5 +1,6 @@
from typing import TYPE_CHECKING, Optional
+from pydantic import condecimal
from sqlmodel import Field, Relationship, SQLModel
if TYPE_CHECKING:
@@ -11,6 +12,9 @@ class Hero(SQLModel, table=True):
name: str = Field(index=True)
secret_name: str
age: Optional[int] = Field(default=None, index=True)
+ experience_points: condecimal(max_digits=6, decimal_places=3) = Field(
+ default=0, nullable=False, index=True
+ )
team_id: Optional[int] = Field(default=None, foreign_key="team.id")
team: Optional["Team"] = Relationship(back_populates="heroes")
diff --git a/docs_src/tutorial_async/.python-version b/docs_src/tutorial_async/.python-version
new file mode 100644
index 0000000000..55689195cb
--- /dev/null
+++ b/docs_src/tutorial_async/.python-version
@@ -0,0 +1 @@
+3.10.7
diff --git a/docs_src/tutorial_async/__init__.py b/docs_src/tutorial_async/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/docs_src/tutorial_async/connect_async/__init__.py b/docs_src/tutorial_async/connect_async/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/docs_src/tutorial_async/connect_async/create_tables_async/__init__.py b/docs_src/tutorial_async/connect_async/create_tables_async/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/docs_src/tutorial_async/connect_async/create_tables_async/tutorial001_async.py b/docs_src/tutorial_async/connect_async/create_tables_async/tutorial001_async.py
new file mode 100644
index 0000000000..ea2fd1ba66
--- /dev/null
+++ b/docs_src/tutorial_async/connect_async/create_tables_async/tutorial001_async.py
@@ -0,0 +1,43 @@
+import asyncio
+from typing import Annotated, Optional
+
+from sqlmodel import Field, SQLModel, create_async_engine
+
+
+class Team(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ headquarters: str
+
+
+class Hero(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ secret_name: str
+ age: Annotated[Optional[int], Field(default_factory=lambda: None, index=True)]
+
+ team_id: Annotated[
+ Optional[int], Field(default_factory=lambda: None, foreign_key="team.id")
+ ]
+
+
+sqlite_file_name = "database.db"
+sqlite_url = f"sqlite+aiosqlite:///{sqlite_file_name}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables() -> None:
+ meta = SQLModel.metadata
+
+ async with engine.begin() as conn:
+ await conn.run_sync(meta.drop_all)
+ await conn.run_sync(meta.create_all)
+
+
+async def main() -> None:
+ await create_db_and_tables()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/docs_src/tutorial_async/connect_async/delete_async/__init__.py b/docs_src/tutorial_async/connect_async/delete_async/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/docs_src/tutorial_async/connect_async/delete_async/tutorial001_async.py b/docs_src/tutorial_async/connect_async/delete_async/tutorial001_async.py
new file mode 100644
index 0000000000..a2ed6a84c1
--- /dev/null
+++ b/docs_src/tutorial_async/connect_async/delete_async/tutorial001_async.py
@@ -0,0 +1,89 @@
+import asyncio
+from typing import Annotated, Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine
+
+
+class Team(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ headquarters: str
+
+
+class Hero(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ secret_name: str
+ age: Annotated[Optional[int], Field(default_factory=lambda: None, index=True)]
+
+ team_id: Annotated[
+ Optional[int], Field(default_factory=lambda: None, foreign_key="team.id")
+ ]
+
+
+sqlite_file_name = "database.db"
+sqlite_url = f"sqlite+aiosqlite:///{sqlite_file_name}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables() -> None:
+ meta = SQLModel.metadata
+
+ async with engine.begin() as conn:
+ await conn.run_sync(meta.drop_all)
+ await conn.run_sync(meta.create_all)
+
+
+async def create_heroes() -> None:
+
+ async with AsyncSession(engine, expire_on_commit=False) as session:
+ team_preventers = Team(name="Preventers", headquarters="Sharp Tower")
+ team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
+ session.add(team_preventers)
+ session.add(team_z_force)
+ await session.commit()
+
+ hero_deadpond = Hero(
+ name="Deadpond", secret_name="Dive Wilson", team_id=team_z_force.id
+ )
+ hero_rusty_man = Hero(
+ name="Rusty-Man",
+ secret_name="Tommy Sharp",
+ age=48,
+ team_id=team_preventers.id,
+ )
+ hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
+ session.add(hero_deadpond)
+ session.add(hero_rusty_man)
+ session.add(hero_spider_boy)
+ await session.commit()
+
+ await session.refresh(hero_deadpond)
+ await session.refresh(hero_rusty_man)
+ await session.refresh(hero_spider_boy)
+
+ print("Created hero:", hero_deadpond)
+ print("Created hero:", hero_rusty_man)
+ print("Created hero:", hero_spider_boy)
+
+ hero_spider_boy.team_id = team_preventers.id
+ session.add(hero_spider_boy)
+ await session.commit()
+ await session.refresh(hero_spider_boy)
+ print("Updated hero:", hero_spider_boy)
+
+ hero_spider_boy.team_id = None
+ session.add(hero_spider_boy)
+ await session.commit()
+ await session.refresh(hero_spider_boy)
+ print("No longer Preventer:", hero_spider_boy)
+
+
+async def main() -> None:
+ await create_db_and_tables()
+ await create_heroes()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/docs_src/tutorial_async/connect_async/insert_async/__init__.py b/docs_src/tutorial_async/connect_async/insert_async/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/docs_src/tutorial_async/connect_async/insert_async/tutorial001_async.py b/docs_src/tutorial_async/connect_async/insert_async/tutorial001_async.py
new file mode 100644
index 0000000000..bf3ff126d8
--- /dev/null
+++ b/docs_src/tutorial_async/connect_async/insert_async/tutorial001_async.py
@@ -0,0 +1,77 @@
+import asyncio
+from typing import Annotated, Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine
+
+
+class Team(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ headquarters: str
+
+
+class Hero(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ secret_name: str
+ age: Annotated[Optional[int], Field(default_factory=lambda: None, index=True)]
+
+ team_id: Annotated[
+ Optional[int], Field(default_factory=lambda: None, foreign_key="team.id")
+ ]
+
+
+sqlite_file_name = "database.db"
+sqlite_url = f"sqlite+aiosqlite:///{sqlite_file_name}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables() -> None:
+ meta = SQLModel.metadata
+
+ async with engine.begin() as conn:
+ await conn.run_sync(meta.drop_all)
+ await conn.run_sync(meta.create_all)
+
+
+async def create_heroes() -> None:
+
+ async with AsyncSession(engine, expire_on_commit=False) as session:
+ team_preventers = Team(name="Preventers", headquarters="Sharp Tower")
+ team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
+ session.add(team_preventers)
+ session.add(team_z_force)
+ await session.commit()
+
+ hero_deadpond = Hero(
+ name="Deadpond", secret_name="Dive Wilson", team_id=team_z_force.id
+ )
+ hero_rusty_man = Hero(
+ name="Rusty-Man",
+ secret_name="Tommy Sharp",
+ age=48,
+ team_id=team_preventers.id,
+ )
+ hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
+ session.add(hero_deadpond)
+ session.add(hero_rusty_man)
+ session.add(hero_spider_boy)
+ await session.commit()
+
+ await session.refresh(hero_deadpond)
+ await session.refresh(hero_rusty_man)
+ await session.refresh(hero_spider_boy)
+
+ print("Created hero:", hero_deadpond)
+ print("Created hero:", hero_rusty_man)
+ print("Created hero:", hero_spider_boy)
+
+
+async def main() -> None:
+ await create_db_and_tables()
+ await create_heroes()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/docs_src/tutorial_async/connect_async/select_async/__init__.py b/docs_src/tutorial_async/connect_async/select_async/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/docs_src/tutorial_async/connect_async/select_async/tutorial001_async.py b/docs_src/tutorial_async/connect_async/select_async/tutorial001_async.py
new file mode 100644
index 0000000000..2082912878
--- /dev/null
+++ b/docs_src/tutorial_async/connect_async/select_async/tutorial001_async.py
@@ -0,0 +1,86 @@
+import asyncio
+from typing import Annotated, Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine, select
+
+
+class Team(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ headquarters: str
+
+
+class Hero(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ secret_name: str
+ age: Annotated[Optional[int], Field(default_factory=lambda: None, index=True)]
+
+ team_id: Annotated[
+ Optional[int], Field(default_factory=lambda: None, foreign_key="team.id")
+ ]
+
+
+sqlite_file_name = "database.db"
+sqlite_url = f"sqlite+aiosqlite:///{sqlite_file_name}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables() -> None:
+ meta = SQLModel.metadata
+
+ async with engine.begin() as conn:
+ await conn.run_sync(meta.drop_all)
+ await conn.run_sync(meta.create_all)
+
+
+async def create_heroes() -> None:
+
+ async with AsyncSession(engine, expire_on_commit=False) as session:
+ team_preventers = Team(name="Preventers", headquarters="Sharp Tower")
+ team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
+ session.add(team_preventers)
+ session.add(team_z_force)
+ await session.commit()
+
+ hero_deadpond = Hero(
+ name="Deadpond", secret_name="Dive Wilson", team_id=team_z_force.id
+ )
+ hero_rusty_man = Hero(
+ name="Rusty-Man",
+ secret_name="Tommy Sharp",
+ age=48,
+ team_id=team_preventers.id,
+ )
+ hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
+ session.add(hero_deadpond)
+ session.add(hero_rusty_man)
+ session.add(hero_spider_boy)
+ await session.commit()
+
+ await session.refresh(hero_deadpond)
+ await session.refresh(hero_rusty_man)
+ await session.refresh(hero_spider_boy)
+
+ print("Created hero:", hero_deadpond)
+ print("Created hero:", hero_rusty_man)
+ print("Created hero:", hero_spider_boy)
+
+
+async def select_heroes() -> None:
+ async with AsyncSession(engine) as session:
+ statement = select(Hero, Team).where(Hero.team_id == Team.id)
+ results = await session.exec(statement)
+ for hero, team in results:
+ print("Hero:", hero, "Team:", team)
+
+
+async def main() -> None:
+ await create_db_and_tables()
+ await create_heroes()
+ await select_heroes()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/docs_src/tutorial_async/connect_async/select_async/tutorial002_async.py b/docs_src/tutorial_async/connect_async/select_async/tutorial002_async.py
new file mode 100644
index 0000000000..cb0cdf4d60
--- /dev/null
+++ b/docs_src/tutorial_async/connect_async/select_async/tutorial002_async.py
@@ -0,0 +1,86 @@
+import asyncio
+from typing import Annotated, Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine, select
+
+
+class Team(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ headquarters: str
+
+
+class Hero(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ secret_name: str
+ age: Annotated[Optional[int], Field(default_factory=lambda: None, index=True)]
+
+ team_id: Annotated[
+ Optional[int], Field(default_factory=lambda: None, foreign_key="team.id")
+ ]
+
+
+sqlite_file_name = "database.db"
+sqlite_url = f"sqlite+aiosqlite:///{sqlite_file_name}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables() -> None:
+ meta = SQLModel.metadata
+
+ async with engine.begin() as conn:
+ await conn.run_sync(meta.drop_all)
+ await conn.run_sync(meta.create_all)
+
+
+async def create_heroes() -> None:
+
+ async with AsyncSession(engine, expire_on_commit=False) as session:
+ team_preventers = Team(name="Preventers", headquarters="Sharp Tower")
+ team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
+ session.add(team_preventers)
+ session.add(team_z_force)
+ await session.commit()
+
+ hero_deadpond = Hero(
+ name="Deadpond", secret_name="Dive Wilson", team_id=team_z_force.id
+ )
+ hero_rusty_man = Hero(
+ name="Rusty-Man",
+ secret_name="Tommy Sharp",
+ age=48,
+ team_id=team_preventers.id,
+ )
+ hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
+ session.add(hero_deadpond)
+ session.add(hero_rusty_man)
+ session.add(hero_spider_boy)
+ await session.commit()
+
+ await session.refresh(hero_deadpond)
+ await session.refresh(hero_rusty_man)
+ await session.refresh(hero_spider_boy)
+
+ print("Created hero:", hero_deadpond)
+ print("Created hero:", hero_rusty_man)
+ print("Created hero:", hero_spider_boy)
+
+
+async def select_heroes() -> None:
+ async with AsyncSession(engine) as session:
+ statement = select(Hero, Team).join(Team)
+ results = await session.exec(statement)
+ for hero, team in results:
+ print("Hero:", hero, "Team:", team)
+
+
+async def main() -> None:
+ await create_db_and_tables()
+ await create_heroes()
+ await select_heroes()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/docs_src/tutorial_async/connect_async/select_async/tutorial003_async.py b/docs_src/tutorial_async/connect_async/select_async/tutorial003_async.py
new file mode 100644
index 0000000000..7b6135ad09
--- /dev/null
+++ b/docs_src/tutorial_async/connect_async/select_async/tutorial003_async.py
@@ -0,0 +1,86 @@
+import asyncio
+from typing import Annotated, Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine, select
+
+
+class Team(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ headquarters: str
+
+
+class Hero(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ secret_name: str
+ age: Annotated[Optional[int], Field(default_factory=lambda: None, index=True)]
+
+ team_id: Annotated[
+ Optional[int], Field(default_factory=lambda: None, foreign_key="team.id")
+ ]
+
+
+sqlite_file_name = "database.db"
+sqlite_url = f"sqlite+aiosqlite:///{sqlite_file_name}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables() -> None:
+ meta = SQLModel.metadata
+
+ async with engine.begin() as conn:
+ await conn.run_sync(meta.drop_all)
+ await conn.run_sync(meta.create_all)
+
+
+async def create_heroes() -> None:
+
+ async with AsyncSession(engine, expire_on_commit=False) as session:
+ team_preventers = Team(name="Preventers", headquarters="Sharp Tower")
+ team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
+ session.add(team_preventers)
+ session.add(team_z_force)
+ await session.commit()
+
+ hero_deadpond = Hero(
+ name="Deadpond", secret_name="Dive Wilson", team_id=team_z_force.id
+ )
+ hero_rusty_man = Hero(
+ name="Rusty-Man",
+ secret_name="Tommy Sharp",
+ age=48,
+ team_id=team_preventers.id,
+ )
+ hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
+ session.add(hero_deadpond)
+ session.add(hero_rusty_man)
+ session.add(hero_spider_boy)
+ await session.commit()
+
+ await session.refresh(hero_deadpond)
+ await session.refresh(hero_rusty_man)
+ await session.refresh(hero_spider_boy)
+
+ print("Created hero:", hero_deadpond)
+ print("Created hero:", hero_rusty_man)
+ print("Created hero:", hero_spider_boy)
+
+
+async def select_heroes() -> None:
+ async with AsyncSession(engine) as session:
+ statement = select(Hero, Team).join(Team, isouter=True)
+ results = await session.exec(statement)
+ for hero, team in results:
+ print("Hero:", hero, "Team:", team)
+
+
+async def main() -> None:
+ await create_db_and_tables()
+ await create_heroes()
+ await select_heroes()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/docs_src/tutorial_async/connect_async/select_async/tutorial004_async.py b/docs_src/tutorial_async/connect_async/select_async/tutorial004_async.py
new file mode 100644
index 0000000000..31346e642a
--- /dev/null
+++ b/docs_src/tutorial_async/connect_async/select_async/tutorial004_async.py
@@ -0,0 +1,86 @@
+import asyncio
+from typing import Annotated, Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine, select
+
+
+class Team(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ headquarters: str
+
+
+class Hero(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ secret_name: str
+ age: Annotated[Optional[int], Field(default_factory=lambda: None, index=True)]
+
+ team_id: Annotated[
+ Optional[int], Field(default_factory=lambda: None, foreign_key="team.id")
+ ]
+
+
+sqlite_file_name = "database.db"
+sqlite_url = f"sqlite+aiosqlite:///{sqlite_file_name}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables() -> None:
+ meta = SQLModel.metadata
+
+ async with engine.begin() as conn:
+ await conn.run_sync(meta.drop_all)
+ await conn.run_sync(meta.create_all)
+
+
+async def create_heroes() -> None:
+
+ async with AsyncSession(engine, expire_on_commit=False) as session:
+ team_preventers = Team(name="Preventers", headquarters="Sharp Tower")
+ team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
+ session.add(team_preventers)
+ session.add(team_z_force)
+ await session.commit()
+
+ hero_deadpond = Hero(
+ name="Deadpond", secret_name="Dive Wilson", team_id=team_z_force.id
+ )
+ hero_rusty_man = Hero(
+ name="Rusty-Man",
+ secret_name="Tommy Sharp",
+ age=48,
+ team_id=team_preventers.id,
+ )
+ hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
+ session.add(hero_deadpond)
+ session.add(hero_rusty_man)
+ session.add(hero_spider_boy)
+ await session.commit()
+
+ await session.refresh(hero_deadpond)
+ await session.refresh(hero_rusty_man)
+ await session.refresh(hero_spider_boy)
+
+ print("Created hero:", hero_deadpond)
+ print("Created hero:", hero_rusty_man)
+ print("Created hero:", hero_spider_boy)
+
+
+async def select_heroes() -> None:
+ async with AsyncSession(engine, expire_on_commit=False) as session:
+ statement = select(Hero).join(Team).where(Team.name == "Preventers")
+ results = await session.exec(statement)
+ for hero in results:
+ print("Preventer Hero:", hero)
+
+
+async def main() -> None:
+ await create_db_and_tables()
+ await create_heroes()
+ await select_heroes()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/docs_src/tutorial_async/connect_async/select_async/tutorial005_async.py b/docs_src/tutorial_async/connect_async/select_async/tutorial005_async.py
new file mode 100644
index 0000000000..92e6d3f50d
--- /dev/null
+++ b/docs_src/tutorial_async/connect_async/select_async/tutorial005_async.py
@@ -0,0 +1,86 @@
+import asyncio
+from typing import Annotated, Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine, select
+
+
+class Team(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ headquarters: str
+
+
+class Hero(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ secret_name: str
+ age: Annotated[Optional[int], Field(default_factory=lambda: None, index=True)]
+
+ team_id: Annotated[
+ Optional[int], Field(default_factory=lambda: None, foreign_key="team.id")
+ ]
+
+
+sqlite_file_name = "database.db"
+sqlite_url = f"sqlite+aiosqlite:///{sqlite_file_name}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables() -> None:
+ meta = SQLModel.metadata
+
+ async with engine.begin() as conn:
+ await conn.run_sync(meta.drop_all)
+ await conn.run_sync(meta.create_all)
+
+
+async def create_heroes() -> None:
+
+ async with AsyncSession(engine, expire_on_commit=False) as session:
+ team_preventers = Team(name="Preventers", headquarters="Sharp Tower")
+ team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
+ session.add(team_preventers)
+ session.add(team_z_force)
+ await session.commit()
+
+ hero_deadpond = Hero(
+ name="Deadpond", secret_name="Dive Wilson", team_id=team_z_force.id
+ )
+ hero_rusty_man = Hero(
+ name="Rusty-Man",
+ secret_name="Tommy Sharp",
+ age=48,
+ team_id=team_preventers.id,
+ )
+ hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
+ session.add(hero_deadpond)
+ session.add(hero_rusty_man)
+ session.add(hero_spider_boy)
+ await session.commit()
+
+ await session.refresh(hero_deadpond)
+ await session.refresh(hero_rusty_man)
+ await session.refresh(hero_spider_boy)
+
+ print("Created hero:", hero_deadpond)
+ print("Created hero:", hero_rusty_man)
+ print("Created hero:", hero_spider_boy)
+
+
+async def select_heroes() -> None:
+ async with AsyncSession(engine) as session:
+ statement = select(Hero, Team).join(Team).where(Team.name == "Preventers")
+ results = await session.exec(statement)
+ for hero, team in results:
+ print("Preventer Hero:", hero, "Team:", team)
+
+
+async def main() -> None:
+ await create_db_and_tables()
+ await create_heroes()
+ await select_heroes()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/docs_src/tutorial_async/connect_async/update_async/__init__.py b/docs_src/tutorial_async/connect_async/update_async/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/docs_src/tutorial_async/connect_async/update_async/tutorial001_async.py b/docs_src/tutorial_async/connect_async/update_async/tutorial001_async.py
new file mode 100644
index 0000000000..51ba03dd3e
--- /dev/null
+++ b/docs_src/tutorial_async/connect_async/update_async/tutorial001_async.py
@@ -0,0 +1,83 @@
+import asyncio
+from typing import Annotated, Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine
+
+
+class Team(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ headquarters: str
+
+
+class Hero(SQLModel, table=True):
+ id: Annotated[int, Field(primary_key=True)]
+ name: Annotated[str, Field(index=True)]
+ secret_name: str
+ age: Annotated[Optional[int], Field(default_factory=lambda: None, index=True)]
+
+ team_id: Annotated[
+ Optional[int], Field(default_factory=lambda: None, foreign_key="team.id")
+ ]
+
+
+sqlite_file_name = "database.db"
+sqlite_url = f"sqlite+aiosqlite:///{sqlite_file_name}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables() -> None:
+ meta = SQLModel.metadata
+
+ async with engine.begin() as conn:
+ await conn.run_sync(meta.drop_all)
+ await conn.run_sync(meta.create_all)
+
+
+async def create_heroes() -> None:
+
+ async with AsyncSession(engine, expire_on_commit=False) as session:
+ team_preventers = Team(name="Preventers", headquarters="Sharp Tower")
+ team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
+ session.add(team_preventers)
+ session.add(team_z_force)
+ await session.commit()
+
+ hero_deadpond = Hero(
+ name="Deadpond", secret_name="Dive Wilson", team_id=team_z_force.id
+ )
+ hero_rusty_man = Hero(
+ name="Rusty-Man",
+ secret_name="Tommy Sharp",
+ age=48,
+ team_id=team_preventers.id,
+ )
+ hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
+ session.add(hero_deadpond)
+ session.add(hero_rusty_man)
+ session.add(hero_spider_boy)
+ await session.commit()
+
+ await session.refresh(hero_deadpond)
+ await session.refresh(hero_rusty_man)
+ await session.refresh(hero_spider_boy)
+
+ print("Created hero:", hero_deadpond)
+ print("Created hero:", hero_rusty_man)
+ print("Created hero:", hero_spider_boy)
+
+ hero_spider_boy.team_id = team_preventers.id
+ session.add(hero_spider_boy)
+ await session.commit()
+ await session.refresh(hero_spider_boy)
+ print("Updated hero:", hero_spider_boy)
+
+
+async def main() -> None:
+ await create_db_and_tables()
+ await create_heroes()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/docs_src/tutorial_async/select_async/__init__.py b/docs_src/tutorial_async/select_async/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/docs_src/tutorial_async/select_async/annotations/en/tutorial002.md b/docs_src/tutorial_async/select_async/annotations/en/tutorial002.md
new file mode 100644
index 0000000000..2570b542c4
--- /dev/null
+++ b/docs_src/tutorial_async/select_async/annotations/en/tutorial002.md
@@ -0,0 +1,63 @@
+1. Import from `sqlmodel` everything we will use, including the new `select()` function.
+
+2. Create the `Hero` class model, representing the `hero` table.
+
+3. Create the **engine**, we should use a single one shared by all the application code, and that's what we are doing here.
+
+4. Create all the tables for the models registered in `SQLModel.metadata`.
+
+ This also creates the database if it doesn't exist already.
+
+5. Create each one of the `Hero` objects.
+
+ You might not have this in your version if you had already created the data in the database.
+
+6. Create a new **session** and use it to `add` the heroes to the database, and then `commit` the changes.
+
+7. Create a new **session** to query data.
+
+ !!! tip
+ Notice that this is a new **session** independent from the one in the other function above.
+
+ But it still uses the same **engine**. We still have one engine for the whole application.
+
+8. Use the `select()` function to create a statement selecting all the `Hero` objects.
+
+ This selects all the rows in the `hero` table.
+
+9. Use `session.exec(statement)` to make the **session** use the **engine** to execute the internal SQL statement.
+
+ This will go to the database, execute that SQL, and get the results back.
+
+ It returns a special iterable object that we put in the variable `results`.
+
+ This generates the output:
+
+ ```
+ INFO Engine BEGIN (implicit)
+ INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age
+ FROM hero
+ INFO Engine [no key 0.00032s] ()
+ ```
+
+10. Iterate for each `Hero` object in the `results`.
+
+11. Print each `hero`.
+
+ The 3 iterations in the `for` loop will generate this output:
+
+ ```
+ id=1 name='Deadpond' age=None secret_name='Dive Wilson'
+ id=2 name='Spider-Boy' age=None secret_name='Pedro Parqueador'
+ id=3 name='Rusty-Man' age=48 secret_name='Tommy Sharp'
+ ```
+
+12. At this point, after the `with` block, the **session** is closed.
+
+ This generates the output:
+
+ ```
+ INFO Engine ROLLBACK
+ ```
+
+13. Add this function `select_heroes()` to the `main()` function so that it is called when we run this program from the command line.
diff --git a/docs_src/tutorial_async/select_async/tutorial001_async.py b/docs_src/tutorial_async/select_async/tutorial001_async.py
new file mode 100644
index 0000000000..b2a9d8a786
--- /dev/null
+++ b/docs_src/tutorial_async/select_async/tutorial001_async.py
@@ -0,0 +1,57 @@
+import asyncio
+from typing import Annotated, Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine, select
+
+
+class Hero(SQLModel, table=True):
+ id: Annotated[int, Field(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}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables() -> None:
+ meta = SQLModel.metadata
+
+ async with engine.begin() as conn:
+ await conn.run_sync(meta.drop_all)
+ await conn.run_sync(meta.create_all)
+
+
+async def create_heroes() -> None:
+
+ 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(engine) as session:
+ session.add(hero_1)
+ session.add(hero_2)
+ session.add(hero_3)
+
+ await session.commit()
+
+
+async def select_heroes() -> None:
+ async with AsyncSession(engine) as session:
+ statement = select(Hero)
+ results = await session.exec(statement)
+ for hero in results:
+ print(hero)
+
+
+async def main() -> None:
+ await create_db_and_tables()
+ await create_heroes()
+ await select_heroes()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/docs_src/tutorial_async/select_async/tutorial002_async.py b/docs_src/tutorial_async/select_async/tutorial002_async.py
new file mode 100644
index 0000000000..e0cb1c8ce7
--- /dev/null
+++ b/docs_src/tutorial_async/select_async/tutorial002_async.py
@@ -0,0 +1,57 @@
+import asyncio
+from typing import Annotated, Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine, select
+
+
+class Hero(SQLModel, table=True): # (2)
+ id: Annotated[int, Field(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}"
+
+engine = create_async_engine(sqlite_url, echo=True) # (3)
+
+
+async def create_db_and_tables() -> None:
+ meta = SQLModel.metadata
+
+ async with engine.begin() as conn:
+ await conn.run_sync(meta.drop_all)
+ await conn.run_sync(meta.create_all) # (4)
+
+
+async def create_heroes() -> None:
+ hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") # (5)
+ 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(engine) as session: # (6)
+ session.add(hero_1)
+ session.add(hero_2)
+ session.add(hero_3)
+
+ await session.commit()
+
+
+async def select_heroes() -> None:
+ async with AsyncSession(engine) as session: # (7)
+ statement = select(Hero) # (8)
+ results = await session.exec(statement) # (9)
+ for hero in results: # (10)
+ print(hero) # (11)
+ # (12)
+
+
+async def main() -> None:
+ await create_db_and_tables()
+ await create_heroes()
+ await select_heroes() # (13)
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/docs_src/tutorial_async/select_async/tutorial003_async.py b/docs_src/tutorial_async/select_async/tutorial003_async.py
new file mode 100644
index 0000000000..5eb3f4493b
--- /dev/null
+++ b/docs_src/tutorial_async/select_async/tutorial003_async.py
@@ -0,0 +1,56 @@
+import asyncio
+from typing import Annotated, Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine, select
+
+
+class Hero(SQLModel, table=True):
+ id: Annotated[int, Field(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}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables() -> None:
+ meta = SQLModel.metadata
+
+ async with engine.begin() as conn:
+ await conn.run_sync(meta.drop_all)
+ await conn.run_sync(meta.create_all)
+
+
+async def create_heroes() -> None:
+ 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(engine) as session:
+ session.add(hero_1)
+ session.add(hero_2)
+ session.add(hero_3)
+
+ await session.commit()
+
+
+async def select_heroes() -> None:
+ async with AsyncSession(engine) as session:
+ statement = select(Hero)
+ results = await session.exec(statement)
+ heroes = results.all()
+ print(heroes)
+
+
+async def main() -> None:
+ await create_db_and_tables()
+ await create_heroes()
+ await select_heroes()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/docs_src/tutorial_async/select_async/tutorial004_async.py b/docs_src/tutorial_async/select_async/tutorial004_async.py
new file mode 100644
index 0000000000..3883b167b7
--- /dev/null
+++ b/docs_src/tutorial_async/select_async/tutorial004_async.py
@@ -0,0 +1,57 @@
+import asyncio
+from typing import Annotated, Optional
+
+from sqlmodel import AsyncSession, Field, SQLModel, create_async_engine, select
+
+
+class Hero(SQLModel, table=True):
+ id: Annotated[int, Field(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}"
+
+engine = create_async_engine(sqlite_url, echo=True)
+
+
+async def create_db_and_tables() -> None:
+ meta = SQLModel.metadata
+
+ async with engine.begin() as conn:
+ await conn.run_sync(meta.drop_all)
+ await conn.run_sync(meta.create_all)
+
+
+async def create_heroes() -> None:
+ 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(engine) as session:
+ session.add(hero_1)
+ session.add(hero_2)
+ session.add(hero_3)
+
+ await session.commit()
+
+
+async def select_heroes() -> None:
+ async with AsyncSession(engine) as session:
+ # TODO: in async, this does not work `await session.exec(select(Hero)).all()`
+ # heroes = await session.exec(select(Hero)).all()
+ results = await session.exec(select(Hero))
+ heroes = results.all()
+ print(heroes)
+
+
+async def main() -> None:
+ await create_db_and_tables()
+ await create_heroes()
+ await select_heroes()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/mkdocs.yml b/mkdocs.yml
index a27bbde8a1..e1b7f25cfb 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -85,6 +85,7 @@ nav:
- Advanced User Guide:
- advanced/index.md
- advanced/decimal.md
+ - advanced/asyncio.md
- alternatives.md
- help.md
- contributing.md
diff --git a/pyproject.toml b/pyproject.toml
index e3b1d3c279..690be0d9f2 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sqlmodel"
-version = "0"
+version = "0.0.9-a.5"
description = "SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness."
authors = ["Sebastián RamÃrez "]
readme = "README.md"
@@ -17,8 +17,6 @@ classifiers = [
"Intended Audience :: System Administrators",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3 :: Only",
- "Programming Language :: Python :: 3.6",
- "Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Topic :: Database",
@@ -30,36 +28,35 @@ classifiers = [
]
[tool.poetry.dependencies]
-python = "^3.6.1"
-SQLAlchemy = ">=1.4.17,<=1.4.41"
+python = "^3.8.1"
+SQLAlchemy = ">=1.4.46,<=1.5.0"
pydantic = "^1.8.2"
sqlalchemy2-stubs = {version = "*", allow-prereleases = true}
[tool.poetry.dev-dependencies]
-pytest = "^7.0.1"
-mypy = "0.971"
-flake8 = "^5.0.4"
-black = {version = "^22.10.0", python = "^3.7"}
-mkdocs = "^1.2.1"
-mkdocs-material = "^8.1.4"
-pillow = {version = "^9.3.0", python = "^3.7"}
-cairosvg = {version = "^2.5.2", python = "^3.7"}
-mdx-include = "^1.4.1"
-coverage = {extras = ["toml"], version = "^6.2"}
-fastapi = "^0.68.1"
-requests = "^2.26.0"
-autoflake = "^1.4"
-isort = "^5.9.3"
-async_generator = {version = "*", python = "~3.6"}
-async-exit-stack = {version = "*", python = "~3.6"}
+pytest = "^7.2.0"
+mypy = "0.991"
+flake8 = "^6.0.0"
+black = {version = "^22.12.0", python = "^3.8"}
+mkdocs = "^1.4.2"
+mkdocs-material = "^8.5.11"
+pillow = {version = "^9.3.0", python = "^3.8"}
+cairosvg = {version = "^2.5.2", python = "^3.8"}
+mdx-include = "^1.4.2"
+coverage = {extras = ["toml"], version = "^6.5"}
+fastapi = "^0.88.0"
+requests = "^2.28.0"
+httpx = "^0.23.1"
+autoflake = "^2.0.0"
+isort = "^5.11.3"
+pytest-asyncio = "^0.19.0"
+SQLAlchemy = {extras = ["asyncio"], version = ">=1.4.46,<=1.5.0"}
+aiosqlite = "^0.17.0"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
-[tool.poetry-version-plugin]
-source = "init"
-
[tool.coverage.run]
parallel = true
source = [
diff --git a/scripts/lint.sh b/scripts/lint.sh
index 02568cda6b..4191d90f1f 100755
--- a/scripts/lint.sh
+++ b/scripts/lint.sh
@@ -7,5 +7,3 @@ mypy sqlmodel
flake8 sqlmodel tests docs_src
black sqlmodel tests docs_src --check
isort sqlmodel tests docs_src scripts --check-only
-# TODO: move this to test.sh after deprecating Python 3.6
-CHECK_JINJA=1 python scripts/generate_select.py
diff --git a/scripts/test.sh b/scripts/test.sh
index 9b758bdbdf..4fae64f046 100755
--- a/scripts/test.sh
+++ b/scripts/test.sh
@@ -3,6 +3,8 @@
set -e
set -x
+CHECK_JINJA=1 python scripts/generate_select.py
+
coverage run -m pytest tests
coverage combine
coverage report --show-missing
diff --git a/sqlmodel/__init__.py b/sqlmodel/__init__.py
index 720aa8c929..ea1c5a8b5a 100644
--- a/sqlmodel/__init__.py
+++ b/sqlmodel/__init__.py
@@ -1,4 +1,4 @@
-__version__ = "0.0.8"
+import importlib.metadata
# Re-export from SQLAlchemy
from sqlalchemy.engine import create_mock_engine as create_mock_engine
@@ -127,8 +127,12 @@
from sqlalchemy.types import VARCHAR as VARCHAR
# Extensions and modifications of SQLAlchemy in SQLModel
-from .engine.create import create_engine as create_engine
+from .engine.create import (
+ create_engine as create_engine,
+ create_async_engine as create_async_engine,
+)
from .orm.session import Session as Session
+from .ext.asyncio.session import AsyncSession as AsyncSession
from .sql.expression import select as select
from .sql.expression import col as col
from .sql.sqltypes import AutoString as AutoString
@@ -137,3 +141,5 @@
from .main import SQLModel as SQLModel
from .main import Field as Field
from .main import Relationship as Relationship
+
+__version__ = importlib.metadata.version("sqlmodel")
diff --git a/sqlmodel/engine/create.py b/sqlmodel/engine/create.py
index b2d567b1b1..d56443d1eb 100644
--- a/sqlmodel/engine/create.py
+++ b/sqlmodel/engine/create.py
@@ -4,6 +4,8 @@
from sqlalchemy import create_engine as _create_engine
from sqlalchemy.engine.url import URL
+from sqlalchemy.ext.asyncio import AsyncEngine as _AsyncEngine
+from sqlalchemy.ext.asyncio import create_async_engine as _create_async_engine
from sqlalchemy.future import Engine as _FutureEngine
from sqlalchemy.pool import Pool
from typing_extensions import Literal, TypedDict
@@ -137,3 +139,100 @@ def create_engine(
current_kwargs["query_cache_size"] = query_cache_size
current_kwargs.update(kwargs)
return _create_engine(url, **current_kwargs) # type: ignore
+
+
+def create_async_engine(
+ url: Union[str, URL],
+ *,
+ connect_args: _ConnectArgs = Default({}), # type: ignore
+ echo: Union[bool, _Debug] = Default(False),
+ echo_pool: Union[bool, _Debug] = Default(False),
+ enable_from_linting: bool = Default(True),
+ encoding: str = Default("utf-8"),
+ execution_options: Dict[Any, Any] = Default({}),
+ future: bool = True,
+ hide_parameters: bool = Default(False),
+ implicit_returning: bool = Default(True),
+ isolation_level: Optional[_IsolationLevel] = Default(None),
+ json_deserializer: Callable[..., Any] = Default(json.loads),
+ json_serializer: Callable[..., Any] = Default(json.dumps),
+ label_length: Optional[int] = Default(None),
+ logging_name: Optional[str] = Default(None),
+ max_identifier_length: Optional[int] = Default(None),
+ max_overflow: int = Default(10),
+ module: Optional[Any] = Default(None),
+ paramstyle: Optional[_ParamStyle] = Default(None),
+ pool: Optional[Pool] = Default(None),
+ poolclass: Optional[Type[Pool]] = Default(None),
+ pool_logging_name: Optional[str] = Default(None),
+ pool_pre_ping: bool = Default(False),
+ pool_size: int = Default(5),
+ pool_recycle: int = Default(-1),
+ pool_reset_on_return: Optional[_ResetOnReturn] = Default("rollback"),
+ pool_timeout: float = Default(30),
+ pool_use_lifo: bool = Default(False),
+ plugins: Optional[List[str]] = Default(None),
+ query_cache_size: Optional[int] = Default(None),
+ **kwargs: Any,
+) -> _AsyncEngine:
+ current_kwargs: Dict[str, Any] = {
+ "future": future,
+ }
+ if not isinstance(echo, _DefaultPlaceholder):
+ current_kwargs["echo"] = echo
+ if not isinstance(echo_pool, _DefaultPlaceholder):
+ current_kwargs["echo_pool"] = echo_pool
+ if not isinstance(enable_from_linting, _DefaultPlaceholder):
+ current_kwargs["enable_from_linting"] = enable_from_linting
+ if not isinstance(connect_args, _DefaultPlaceholder):
+ current_kwargs["connect_args"] = connect_args
+ if not isinstance(encoding, _DefaultPlaceholder):
+ current_kwargs["encoding"] = encoding
+ if not isinstance(execution_options, _DefaultPlaceholder):
+ current_kwargs["execution_options"] = execution_options
+ if not isinstance(hide_parameters, _DefaultPlaceholder):
+ current_kwargs["hide_parameters"] = hide_parameters
+ if not isinstance(implicit_returning, _DefaultPlaceholder):
+ current_kwargs["implicit_returning"] = implicit_returning
+ if not isinstance(isolation_level, _DefaultPlaceholder):
+ current_kwargs["isolation_level"] = isolation_level
+ if not isinstance(json_deserializer, _DefaultPlaceholder):
+ current_kwargs["json_deserializer"] = json_deserializer
+ if not isinstance(json_serializer, _DefaultPlaceholder):
+ current_kwargs["json_serializer"] = json_serializer
+ if not isinstance(label_length, _DefaultPlaceholder):
+ current_kwargs["label_length"] = label_length
+ if not isinstance(logging_name, _DefaultPlaceholder):
+ current_kwargs["logging_name"] = logging_name
+ if not isinstance(max_identifier_length, _DefaultPlaceholder):
+ current_kwargs["max_identifier_length"] = max_identifier_length
+ if not isinstance(max_overflow, _DefaultPlaceholder):
+ current_kwargs["max_overflow"] = max_overflow
+ if not isinstance(module, _DefaultPlaceholder):
+ current_kwargs["module"] = module
+ if not isinstance(paramstyle, _DefaultPlaceholder):
+ current_kwargs["paramstyle"] = paramstyle
+ if not isinstance(pool, _DefaultPlaceholder):
+ current_kwargs["pool"] = pool
+ if not isinstance(poolclass, _DefaultPlaceholder):
+ current_kwargs["poolclass"] = poolclass
+ if not isinstance(pool_logging_name, _DefaultPlaceholder):
+ current_kwargs["pool_logging_name"] = pool_logging_name
+ if not isinstance(pool_pre_ping, _DefaultPlaceholder):
+ current_kwargs["pool_pre_ping"] = pool_pre_ping
+ if not isinstance(pool_size, _DefaultPlaceholder):
+ current_kwargs["pool_size"] = pool_size
+ if not isinstance(pool_recycle, _DefaultPlaceholder):
+ current_kwargs["pool_recycle"] = pool_recycle
+ if not isinstance(pool_reset_on_return, _DefaultPlaceholder):
+ current_kwargs["pool_reset_on_return"] = pool_reset_on_return
+ if not isinstance(pool_timeout, _DefaultPlaceholder):
+ current_kwargs["pool_timeout"] = pool_timeout
+ if not isinstance(pool_use_lifo, _DefaultPlaceholder):
+ current_kwargs["pool_use_lifo"] = pool_use_lifo
+ if not isinstance(plugins, _DefaultPlaceholder):
+ current_kwargs["plugins"] = plugins
+ if not isinstance(query_cache_size, _DefaultPlaceholder):
+ current_kwargs["query_cache_size"] = query_cache_size
+ current_kwargs.update(kwargs)
+ return _create_async_engine(url, **current_kwargs)
diff --git a/sqlmodel/ext/asyncio/session.py b/sqlmodel/ext/asyncio/session.py
index 80267b25e5..925b581ef4 100644
--- a/sqlmodel/ext/asyncio/session.py
+++ b/sqlmodel/ext/asyncio/session.py
@@ -1,62 +1,117 @@
-from typing import Any, Mapping, Optional, Sequence, TypeVar, Union
+from typing import Any, Mapping, Optional, Sequence, Type, TypeVar, Union, overload
from sqlalchemy import util
from sqlalchemy.ext.asyncio import AsyncSession as _AsyncSession
-from sqlalchemy.ext.asyncio import engine
from sqlalchemy.ext.asyncio.engine import AsyncConnection, AsyncEngine
-from sqlalchemy.util.concurrency import greenlet_spawn
+from sqlalchemy.sql.base import Executable as _Executable
from sqlmodel.sql.base import Executable
+from typing_extensions import Literal
-from ...engine.result import ScalarResult
+from ...engine.result import Result, ScalarResult
from ...orm.session import Session
-from ...sql.expression import Select
+from ...sql.expression import Select, SelectOfScalar
-_T = TypeVar("_T")
+_TSelectParam = TypeVar("_TSelectParam")
class AsyncSession(_AsyncSession):
+ sync_session_class = Session
sync_session: Session
def __init__(
self,
bind: Optional[Union[AsyncConnection, AsyncEngine]] = None,
binds: Optional[Mapping[object, Union[AsyncConnection, AsyncEngine]]] = None,
+ sync_session_class: Type[Session] = Session,
**kw: Any,
):
- # All the same code of the original AsyncSession
- kw["future"] = True
- if bind:
- self.bind = bind
- bind = engine._get_sync_engine_or_connection(bind) # type: ignore
-
- if binds:
- self.binds = binds
- binds = {
- key: engine._get_sync_engine_or_connection(b) # type: ignore
- for key, b in binds.items()
- }
-
- self.sync_session = self._proxied = self._assign_proxied( # type: ignore
- Session(bind=bind, binds=binds, **kw) # type: ignore
+ super().__init__(
+ bind=bind,
+ binds=binds,
+ sync_session_class=sync_session_class,
+ **kw,
)
+ @overload
+ async def exec(
+ self,
+ statement: Select[_TSelectParam],
+ *,
+ params: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]] = None,
+ execution_options: Mapping[str, Any] = util.EMPTY_DICT,
+ bind_arguments: Optional[Mapping[str, Any]] = None,
+ **kw: Any,
+ ) -> Result[_TSelectParam]:
+ ...
+
+ @overload
async def exec(
self,
- statement: Union[Select[_T], Executable[_T]],
+ statement: SelectOfScalar[_TSelectParam],
+ *,
params: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]] = None,
- execution_options: Mapping[Any, Any] = util.EMPTY_DICT,
+ execution_options: Mapping[str, Any] = util.EMPTY_DICT,
bind_arguments: Optional[Mapping[str, Any]] = None,
**kw: Any,
- ) -> ScalarResult[_T]:
- # TODO: the documentation says execution_options accepts a dict, but only
- # util.immutabledict has the union() method. Is this a bug in SQLAlchemy?
- execution_options = execution_options.union({"prebuffer_rows": True}) # type: ignore
+ ) -> ScalarResult[_TSelectParam]:
+ ...
- return await greenlet_spawn(
- self.sync_session.exec,
+ async def exec(
+ self,
+ statement: Union[
+ Select[_TSelectParam],
+ SelectOfScalar[_TSelectParam],
+ Executable[_TSelectParam],
+ ],
+ *,
+ params: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]] = None,
+ execution_options: Mapping[str, Any] = util.EMPTY_DICT,
+ bind_arguments: Optional[Mapping[str, Any]] = None,
+ **kw: Any,
+ ) -> Union[Result[_TSelectParam], ScalarResult[_TSelectParam]]:
+ results = super().execute(
statement,
params=params,
execution_options=execution_options,
bind_arguments=bind_arguments,
**kw,
)
+ if isinstance(statement, SelectOfScalar):
+ return (await results).scalars() # type: ignore
+ return await results # type: ignore
+
+ async def execute(
+ self,
+ statement: _Executable,
+ params: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]]]] = None,
+ execution_options: Optional[Mapping[str, Any]] = util.EMPTY_DICT,
+ bind_arguments: Optional[Mapping[str, Any]] = None,
+ **kw: Any,
+ ) -> Result[Any]:
+ return await super().execute( # type: ignore
+ statement=statement,
+ params=params,
+ execution_options=execution_options,
+ bind_arguments=bind_arguments,
+ **kw,
+ )
+
+ async def get(
+ self,
+ entity: Type[_TSelectParam],
+ ident: Any,
+ options: Optional[Sequence[Any]] = None,
+ populate_existing: bool = False,
+ with_for_update: Optional[Union[Literal[True], Mapping[str, Any]]] = None,
+ identity_token: Optional[Any] = None,
+ execution_options: Optional[Mapping[Any, Any]] = util.EMPTY_DICT,
+ ) -> Optional[_TSelectParam]:
+ return await super().get(
+ entity=entity,
+ ident=ident,
+ options=options,
+ populate_existing=populate_existing,
+ with_for_update=with_for_update,
+ identity_token=identity_token,
+ execution_options=execution_options,
+ )
diff --git a/sqlmodel/main.py b/sqlmodel/main.py
index d95c498507..98366b7276 100644
--- a/sqlmodel/main.py
+++ b/sqlmodel/main.py
@@ -11,6 +11,7 @@
Callable,
ClassVar,
Dict,
+ ForwardRef,
List,
Mapping,
Optional,
@@ -29,7 +30,7 @@
from pydantic.fields import FieldInfo as PydanticFieldInfo
from pydantic.fields import ModelField, Undefined, UndefinedType
from pydantic.main import ModelMetaclass, validate_model
-from pydantic.typing import ForwardRef, NoArgAnyCallable, resolve_annotations
+from pydantic.typing import NoArgAnyCallable, resolve_annotations
from pydantic.utils import ROOT_KEY, Representation
from sqlalchemy import Boolean, Column, Date, DateTime
from sqlalchemy import Enum as sa_Enum
@@ -325,13 +326,12 @@ def __init__(
break
if getattr(cls.__config__, "table", False) and not base_is_table:
dict_used = dict_.copy()
- for field_name, field_value in cls.__fields__.items():
- dict_used[field_name] = get_column_from_field(field_value)
for rel_name, rel_info in cls.__sqlmodel_relationships__.items():
if rel_info.sa_relationship:
# There's a SQLAlchemy relationship declared, that takes precedence
# over anything else, use that and continue with the next attribute
dict_used[rel_name] = rel_info.sa_relationship
+ setattr(cls, rel_name, rel_info.sa_relationship) # Fix #315
continue
ann = cls.__annotations__[rel_name]
temp_field = ModelField.infer(
@@ -420,9 +420,12 @@ def get_column_from_field(field: ModelField) -> Column: # type: ignore
return sa_column
sa_type = get_sqlalchemy_type(field)
primary_key = getattr(field.field_info, "primary_key", False)
+ foreign_key = getattr(field.field_info, "foreign_key", None)
+ unique = getattr(field.field_info, "unique", False)
index = getattr(field.field_info, "index", Undefined)
if index is Undefined:
- index = False
+ # If the field is not unique, it is not internally indexed. FKs are a good candidate for indices.
+ index = foreign_key is not None and not unique
nullable = not primary_key and _is_field_noneable(field)
# Override derived nullability if the nullable property is set explicitly
# on the field
@@ -431,8 +434,6 @@ def get_column_from_field(field: ModelField) -> Column: # type: ignore
if field_nullable != Undefined:
nullable = field_nullable
args = []
- foreign_key = getattr(field.field_info, "foreign_key", None)
- unique = getattr(field.field_info, "unique", False)
if foreign_key:
args.append(ForeignKey(foreign_key))
kwargs = {
diff --git a/sqlmodel/orm/session.py b/sqlmodel/orm/session.py
index 1692fdcbcb..0c70c290ae 100644
--- a/sqlmodel/orm/session.py
+++ b/sqlmodel/orm/session.py
@@ -4,11 +4,11 @@
from sqlalchemy.orm import Query as _Query
from sqlalchemy.orm import Session as _Session
from sqlalchemy.sql.base import Executable as _Executable
-from sqlmodel.sql.expression import Select, SelectOfScalar
from typing_extensions import Literal
from ..engine.result import Result, ScalarResult
from ..sql.base import Executable
+from ..sql.expression import Select, SelectOfScalar
_TSelectParam = TypeVar("_TSelectParam")
diff --git a/sqlmodel/sql/expression.py b/sqlmodel/sql/expression.py
index 31c0bc1a1e..264e39cba7 100644
--- a/sqlmodel/sql/expression.py
+++ b/sqlmodel/sql/expression.py
@@ -1,6 +1,5 @@
# WARNING: do not modify this code, it is generated by expression.py.jinja2
-import sys
from datetime import datetime
from typing import (
TYPE_CHECKING,
@@ -12,7 +11,6 @@
Type,
TypeVar,
Union,
- cast,
overload,
)
from uuid import UUID
@@ -24,36 +22,17 @@
_TSelect = TypeVar("_TSelect")
-# Workaround Generics incompatibility in Python 3.6
-# Ref: https://github.com/python/typing/issues/449#issuecomment-316061322
-if sys.version_info.minor >= 7:
- class Select(_Select, Generic[_TSelect]):
- inherit_cache = True
+class Select(_Select, Generic[_TSelect]):
+ inherit_cache = True
- # This is not comparable to sqlalchemy.sql.selectable.ScalarSelect, that has a different
- # purpose. This is the same as a normal SQLAlchemy Select class where there's only one
- # entity, so the result will be converted to a scalar by default. This way writing
- # for loops on the results will feel natural.
- class SelectOfScalar(_Select, Generic[_TSelect]):
- inherit_cache = True
-else:
- from typing import GenericMeta # type: ignore
-
- class GenericSelectMeta(GenericMeta, _Select.__class__): # type: ignore
- pass
-
- class _Py36Select(_Select, Generic[_TSelect], metaclass=GenericSelectMeta):
- inherit_cache = True
-
- class _Py36SelectOfScalar(_Select, Generic[_TSelect], metaclass=GenericSelectMeta):
- inherit_cache = True
-
- # Cast them for editors to work correctly, from several tricks tried, this works
- # for both VS Code and PyCharm
- Select = cast("Select", _Py36Select) # type: ignore
- SelectOfScalar = cast("SelectOfScalar", _Py36SelectOfScalar) # type: ignore
+# This is not comparable to sqlalchemy.sql.selectable.ScalarSelect, that has a different
+# purpose. This is the same as a normal SQLAlchemy Select class where there's only one
+# entity, so the result will be converted to a scalar by default. This way writing
+# for loops on the results will feel natural.
+class SelectOfScalar(_Select, Generic[_TSelect]):
+ inherit_cache = True
if TYPE_CHECKING: # pragma: no cover
diff --git a/sqlmodel/sql/expression.py.jinja2 b/sqlmodel/sql/expression.py.jinja2
index 51f04a215d..80c2c5016e 100644
--- a/sqlmodel/sql/expression.py.jinja2
+++ b/sqlmodel/sql/expression.py.jinja2
@@ -1,4 +1,3 @@
-import sys
from datetime import datetime
from typing import (
TYPE_CHECKING,
@@ -10,7 +9,6 @@ from typing import (
Type,
TypeVar,
Union,
- cast,
overload,
)
from uuid import UUID
@@ -22,36 +20,17 @@ from sqlalchemy.sql.expression import Select as _Select
_TSelect = TypeVar("_TSelect")
-# Workaround Generics incompatibility in Python 3.6
-# Ref: https://github.com/python/typing/issues/449#issuecomment-316061322
-if sys.version_info.minor >= 7:
- class Select(_Select, Generic[_TSelect]):
- inherit_cache = True
+class Select(_Select, Generic[_TSelect]):
+ inherit_cache = True
- # This is not comparable to sqlalchemy.sql.selectable.ScalarSelect, that has a different
- # purpose. This is the same as a normal SQLAlchemy Select class where there's only one
- # entity, so the result will be converted to a scalar by default. This way writing
- # for loops on the results will feel natural.
- class SelectOfScalar(_Select, Generic[_TSelect]):
- inherit_cache = True
-else:
- from typing import GenericMeta # type: ignore
-
- class GenericSelectMeta(GenericMeta, _Select.__class__): # type: ignore
- pass
-
- class _Py36Select(_Select, Generic[_TSelect], metaclass=GenericSelectMeta):
- inherit_cache = True
-
- class _Py36SelectOfScalar(_Select, Generic[_TSelect], metaclass=GenericSelectMeta):
- inherit_cache = True
-
- # Cast them for editors to work correctly, from several tricks tried, this works
- # for both VS Code and PyCharm
- Select = cast("Select", _Py36Select) # type: ignore
- SelectOfScalar = cast("SelectOfScalar", _Py36SelectOfScalar) # type: ignore
+# This is not comparable to sqlalchemy.sql.selectable.ScalarSelect, that has a different
+# purpose. This is the same as a normal SQLAlchemy Select class where there's only one
+# entity, so the result will be converted to a scalar by default. This way writing
+# for loops on the results will feel natural.
+class SelectOfScalar(_Select, Generic[_TSelect]):
+ inherit_cache = True
if TYPE_CHECKING: # pragma: no cover
diff --git a/tests/test_advanced/test_asyncio/__init__.py b/tests/test_advanced/test_asyncio/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/test_advanced/test_asyncio/test_tutorial001.py b/tests/test_advanced/test_asyncio/test_tutorial001.py
new file mode 100644
index 0000000000..474d49127c
--- /dev/null
+++ b/tests/test_advanced/test_asyncio/test_tutorial001.py
@@ -0,0 +1,63 @@
+import asyncio
+from typing import Any, Dict, List, Union
+from unittest.mock import patch
+
+from sqlmodel import AsyncSession, create_async_engine, select
+
+from tests.conftest import get_testing_print_function
+
+
+def check_calls(calls: List[List[Union[str, Dict[str, Any]]]]):
+ assert calls[0][0] == {
+ "name": "Deadpond",
+ "secret_name": "Dive Wilson",
+ "age": None,
+ "id": 1,
+ }
+ assert calls[1][0] == {
+ "name": "Spider-Boy",
+ "secret_name": "Pedro Parqueador",
+ "age": None,
+ "id": 2,
+ }
+ assert calls[2][0] == {
+ "name": "Rusty-Man",
+ "secret_name": "Tommy Sharp",
+ "age": 48,
+ "id": 3,
+ }
+
+
+async def tutorial_async001():
+ from docs_src.advanced.asyncio import tutorial001 as mod
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+ calls = []
+
+ new_print = get_testing_print_function(calls)
+ with patch("builtins.print", new=new_print):
+ await mod.main()
+ async with AsyncSession(mod.engine) as session:
+ heroes = (await session.exec(select(mod.Hero))).all()
+ heroes_by_name = {hero.name: hero for hero in heroes}
+ deadpond = heroes_by_name["Deadpond"]
+ spider_boy = heroes_by_name["Spider-Boy"]
+ rusty_man = heroes_by_name["Rusty-Man"]
+ assert deadpond.name == "Deadpond"
+ assert deadpond.age is None
+ assert deadpond.id is not None
+ assert deadpond.secret_name == "Dive Wilson"
+ assert spider_boy.name == "Spider-Boy"
+ assert spider_boy.age is None
+ assert spider_boy.id is not None
+ assert spider_boy.secret_name == "Pedro Parqueador"
+ assert rusty_man.name == "Rusty-Man"
+ assert rusty_man.age == 48
+ assert rusty_man.id is not None
+ assert rusty_man.secret_name == "Tommy Sharp"
+ check_calls(calls)
+
+
+def test_tutorial_001(clear_sqlmodel):
+ asyncio.run(tutorial_async001())
diff --git a/tests/test_async_tutorial/test_async_connect/__init__.py b/tests/test_async_tutorial/test_async_connect/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/test_async_tutorial/test_async_connect/test_async_create_connected_tables/__init__.py b/tests/test_async_tutorial/test_async_connect/test_async_create_connected_tables/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/test_async_tutorial/test_async_connect/test_async_create_connected_tables/test_async_tutorial001.py b/tests/test_async_tutorial/test_async_connect/test_async_create_connected_tables/test_async_tutorial001.py
new file mode 100644
index 0000000000..c36285a917
--- /dev/null
+++ b/tests/test_async_tutorial/test_async_connect/test_async_create_connected_tables/test_async_tutorial001.py
@@ -0,0 +1,43 @@
+import sys
+
+import pytest
+from sqlalchemy import inspect
+from sqlalchemy.ext.asyncio import create_async_engine
+
+beforePYTHON3_9 = sys.version_info < (3, 9)
+reasonPEP593 = "Annotations(PEP593 https://peps.python.org/pep-0593/) only compatible with Python ver >= 3.9"
+
+
+@pytest.mark.skipif(beforePYTHON3_9, reason=reasonPEP593)
+@pytest.mark.asyncio
+async def test_async_tutorial001() -> None:
+ from docs_src.tutorial_async.connect_async.create_tables_async import (
+ tutorial001_async as mod,
+ )
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+
+ await mod.main()
+
+ # Following code lines are for sync implementation , see following note regarding async
+ # https://docs.sqlalchemy.org/en/14/errors.html#error-xd3s
+ # insp: Inspector = inspect(mod.engine)
+ # assert insp.has_table(str(mod.Hero.__tablename__))
+ # assert insp.has_table(str(mod.Team.__tablename__))
+
+ # following is recommended workaround
+ async with mod.engine.connect() as conn:
+ tables = await conn.run_sync(
+ lambda sync_conn: inspect(sync_conn).get_table_names()
+ )
+ assert str(mod.Team.__tablename__) in tables
+ assert str(mod.Hero.__tablename__) in tables
+
+ # TODO; work out how to call insp.has_tables with run_sync
+ # async with mod.engine.connect() as conn:
+ # insp: Inspector = await conn.run_sync(
+ # lambda sync_conn: inspect(sync_conn)
+ # )
+ # assert insp.has_table(str(mod.Hero.__tablename__))
+ # assert insp.has_table(str(mod.Team.__tablename__))
diff --git a/tests/test_async_tutorial/test_async_connect/test_async_delete/__init__.py b/tests/test_async_tutorial/test_async_connect/test_async_delete/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/test_async_tutorial/test_async_connect/test_async_delete/test_async_tutorial001.py b/tests/test_async_tutorial/test_async_connect/test_async_delete/test_async_tutorial001.py
new file mode 100644
index 0000000000..796b33024b
--- /dev/null
+++ b/tests/test_async_tutorial/test_async_connect/test_async_delete/test_async_tutorial001.py
@@ -0,0 +1,83 @@
+import sys
+from typing import Any, List
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy.ext.asyncio import create_async_engine
+
+from tests.conftest import get_testing_print_function
+
+expected_calls = [
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 1,
+ "secret_name": "Dive Wilson",
+ "team_id": 2,
+ "name": "Deadpond",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": 48,
+ "id": 2,
+ "secret_name": "Tommy Sharp",
+ "team_id": 1,
+ "name": "Rusty-Man",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 3,
+ "secret_name": "Pedro Parqueador",
+ "team_id": None,
+ "name": "Spider-Boy",
+ },
+ ],
+ [
+ "Updated hero:",
+ {
+ "age": None,
+ "id": 3,
+ "secret_name": "Pedro Parqueador",
+ "team_id": 1,
+ "name": "Spider-Boy",
+ },
+ ],
+ [
+ "No longer Preventer:",
+ {
+ "age": None,
+ "id": 3,
+ "secret_name": "Pedro Parqueador",
+ "team_id": None,
+ "name": "Spider-Boy",
+ },
+ ],
+]
+
+
+beforePYTHON3_9 = sys.version_info < (3, 9)
+reasonPEP593 = "Annotations(PEP593 https://peps.python.org/pep-0593/) only compatible with Python ver >= 3.9"
+
+
+@pytest.mark.skipif(beforePYTHON3_9, reason=reasonPEP593)
+@pytest.mark.asyncio()
+async def test_tutorial_async(clear_sqlmodel: Any) -> None:
+ from docs_src.tutorial_async.connect_async.delete_async import (
+ tutorial001_async as mod,
+ )
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+ calls: List[Any] = []
+
+ new_print = get_testing_print_function(calls)
+
+ with patch("builtins.print", new=new_print):
+ await mod.main()
+ assert calls == expected_calls
diff --git a/tests/test_async_tutorial/test_async_connect/test_async_insert/__init__.py b/tests/test_async_tutorial/test_async_connect/test_async_insert/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/test_async_tutorial/test_async_connect/test_async_insert/test_async_tutorial001.py b/tests/test_async_tutorial/test_async_connect/test_async_insert/test_async_tutorial001.py
new file mode 100644
index 0000000000..aefdc7a99f
--- /dev/null
+++ b/tests/test_async_tutorial/test_async_connect/test_async_insert/test_async_tutorial001.py
@@ -0,0 +1,63 @@
+import sys
+from typing import Any, List
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy.ext.asyncio import create_async_engine
+
+from tests.conftest import get_testing_print_function
+
+expected_calls = [
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 1,
+ "secret_name": "Dive Wilson",
+ "team_id": 2,
+ "name": "Deadpond",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": 48,
+ "id": 2,
+ "secret_name": "Tommy Sharp",
+ "team_id": 1,
+ "name": "Rusty-Man",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 3,
+ "secret_name": "Pedro Parqueador",
+ "team_id": None,
+ "name": "Spider-Boy",
+ },
+ ],
+]
+
+
+beforePYTHON3_9 = sys.version_info < (3, 9)
+reasonPEP593 = "Annotations(PEP593 https://peps.python.org/pep-0593/) only compatible with Python ver >= 3.9"
+
+
+@pytest.mark.skipif(beforePYTHON3_9, reason=reasonPEP593)
+@pytest.mark.asyncio()
+async def test_async_tutorial001(clear_sqlmodel: Any) -> None:
+ from docs_src.tutorial_async.connect_async.insert_async import (
+ tutorial001_async as mod,
+ )
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+ calls: List[Any] = []
+
+ new_print = get_testing_print_function(calls)
+
+ with patch("builtins.print", new=new_print):
+ await mod.main()
+ assert calls == expected_calls
diff --git a/tests/test_async_tutorial/test_async_connect/test_async_select/__init__.py b/tests/test_async_tutorial/test_async_connect/test_async_select/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/test_async_tutorial/test_async_connect/test_async_select/test_async_tutorial001_tutorial002.py b/tests/test_async_tutorial/test_async_connect/test_async_select/test_async_tutorial001_tutorial002.py
new file mode 100644
index 0000000000..7d2cb667e7
--- /dev/null
+++ b/tests/test_async_tutorial/test_async_connect/test_async_select/test_async_tutorial001_tutorial002.py
@@ -0,0 +1,105 @@
+import sys
+from typing import Any, List
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy.ext.asyncio import create_async_engine
+
+from tests.conftest import get_testing_print_function
+
+expected_calls = [
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 1,
+ "secret_name": "Dive Wilson",
+ "team_id": 2,
+ "name": "Deadpond",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": 48,
+ "id": 2,
+ "secret_name": "Tommy Sharp",
+ "team_id": 1,
+ "name": "Rusty-Man",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 3,
+ "secret_name": "Pedro Parqueador",
+ "team_id": None,
+ "name": "Spider-Boy",
+ },
+ ],
+ [
+ "Hero:",
+ {
+ "age": None,
+ "id": 1,
+ "secret_name": "Dive Wilson",
+ "team_id": 2,
+ "name": "Deadpond",
+ },
+ "Team:",
+ {"id": 2, "name": "Z-Force", "headquarters": "Sister Margaret’s Bar"},
+ ],
+ [
+ "Hero:",
+ {
+ "age": 48,
+ "id": 2,
+ "secret_name": "Tommy Sharp",
+ "team_id": 1,
+ "name": "Rusty-Man",
+ },
+ "Team:",
+ {"id": 1, "name": "Preventers", "headquarters": "Sharp Tower"},
+ ],
+]
+
+
+beforePYTHON3_9 = sys.version_info < (3, 9)
+reasonPEP593 = "Annotations(PEP593 https://peps.python.org/pep-0593/) only compatible with Python ver >= 3.9"
+
+
+@pytest.mark.skipif(beforePYTHON3_9, reason=reasonPEP593)
+@pytest.mark.asyncio()
+async def test_tutorial001(clear_sqlmodel: Any) -> None:
+ from docs_src.tutorial_async.connect_async.select_async import (
+ tutorial001_async as mod,
+ )
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+ calls: List[Any] = []
+
+ new_print = get_testing_print_function(calls)
+
+ with patch("builtins.print", new=new_print):
+ await mod.main()
+ assert calls == expected_calls
+
+
+@pytest.mark.skipif(beforePYTHON3_9, reason=reasonPEP593)
+@pytest.mark.asyncio()
+async def test_tutorial002(clear_sqlmodel: Any) -> None:
+ from docs_src.tutorial_async.connect_async.select_async import (
+ tutorial002_async as mod,
+ )
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+ calls: List[Any] = []
+
+ new_print = get_testing_print_function(calls)
+
+ with patch("builtins.print", new=new_print):
+ await mod.main()
+ assert calls == expected_calls
diff --git a/tests/test_async_tutorial/test_async_connect/test_async_select/test_async_tutorial003.py b/tests/test_async_tutorial/test_async_connect/test_async_select/test_async_tutorial003.py
new file mode 100644
index 0000000000..f2d7b8e2e8
--- /dev/null
+++ b/tests/test_async_tutorial/test_async_connect/test_async_select/test_async_tutorial003.py
@@ -0,0 +1,99 @@
+import sys
+from typing import Any, List
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy.ext.asyncio import create_async_engine
+
+from tests.conftest import get_testing_print_function
+
+expected_calls = [
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 1,
+ "secret_name": "Dive Wilson",
+ "team_id": 2,
+ "name": "Deadpond",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": 48,
+ "id": 2,
+ "secret_name": "Tommy Sharp",
+ "team_id": 1,
+ "name": "Rusty-Man",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 3,
+ "secret_name": "Pedro Parqueador",
+ "team_id": None,
+ "name": "Spider-Boy",
+ },
+ ],
+ [
+ "Hero:",
+ {
+ "age": None,
+ "id": 1,
+ "secret_name": "Dive Wilson",
+ "team_id": 2,
+ "name": "Deadpond",
+ },
+ "Team:",
+ {"id": 2, "name": "Z-Force", "headquarters": "Sister Margaret’s Bar"},
+ ],
+ [
+ "Hero:",
+ {
+ "age": 48,
+ "id": 2,
+ "secret_name": "Tommy Sharp",
+ "team_id": 1,
+ "name": "Rusty-Man",
+ },
+ "Team:",
+ {"id": 1, "name": "Preventers", "headquarters": "Sharp Tower"},
+ ],
+ [
+ "Hero:",
+ {
+ "age": None,
+ "id": 3,
+ "secret_name": "Pedro Parqueador",
+ "team_id": None,
+ "name": "Spider-Boy",
+ },
+ "Team:",
+ None,
+ ],
+]
+
+
+beforePYTHON3_9 = sys.version_info < (3, 9)
+reasonPEP593 = "Annotations(PEP593 https://peps.python.org/pep-0593/) only compatible with Python ver >= 3.9"
+
+
+@pytest.mark.skipif(beforePYTHON3_9, reason=reasonPEP593)
+@pytest.mark.asyncio()
+async def test_tutorial(clear_sqlmodel: Any) -> None:
+ from docs_src.tutorial_async.connect_async.select_async import (
+ tutorial003_async as mod,
+ )
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+ calls: List[Any] = []
+
+ new_print = get_testing_print_function(calls)
+
+ with patch("builtins.print", new=new_print):
+ await mod.main()
+ assert calls == expected_calls
diff --git a/tests/test_async_tutorial/test_async_connect/test_async_select/test_async_tutorial004.py b/tests/test_async_tutorial/test_async_connect/test_async_select/test_async_tutorial004.py
new file mode 100644
index 0000000000..2131137579
--- /dev/null
+++ b/tests/test_async_tutorial/test_async_connect/test_async_select/test_async_tutorial004.py
@@ -0,0 +1,73 @@
+import sys
+from typing import Any, List
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy.ext.asyncio import create_async_engine
+
+from tests.conftest import get_testing_print_function
+
+expected_calls = [
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 1,
+ "secret_name": "Dive Wilson",
+ "team_id": 2,
+ "name": "Deadpond",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": 48,
+ "id": 2,
+ "secret_name": "Tommy Sharp",
+ "team_id": 1,
+ "name": "Rusty-Man",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 3,
+ "secret_name": "Pedro Parqueador",
+ "team_id": None,
+ "name": "Spider-Boy",
+ },
+ ],
+ [
+ "Preventer Hero:",
+ {
+ "age": 48,
+ "id": 2,
+ "secret_name": "Tommy Sharp",
+ "team_id": 1,
+ "name": "Rusty-Man",
+ },
+ ],
+]
+
+
+beforePYTHON3_9 = sys.version_info < (3, 9)
+reasonPEP593 = "Annotations(PEP593 https://peps.python.org/pep-0593/) only compatible with Python ver >= 3.9"
+
+
+@pytest.mark.skipif(beforePYTHON3_9, reason=reasonPEP593)
+@pytest.mark.asyncio()
+async def test_tutorial(clear_sqlmodel: Any) -> None:
+ from docs_src.tutorial_async.connect_async.select_async import (
+ tutorial004_async as mod,
+ )
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+ calls: List[Any] = []
+
+ new_print = get_testing_print_function(calls)
+
+ with patch("builtins.print", new=new_print):
+ await mod.main()
+ assert calls == expected_calls
diff --git a/tests/test_async_tutorial/test_async_connect/test_async_select/test_async_tutorial005.py b/tests/test_async_tutorial/test_async_connect/test_async_select/test_async_tutorial005.py
new file mode 100644
index 0000000000..468d0532a3
--- /dev/null
+++ b/tests/test_async_tutorial/test_async_connect/test_async_select/test_async_tutorial005.py
@@ -0,0 +1,75 @@
+import sys
+from typing import Any, List
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy.ext.asyncio import create_async_engine
+
+from tests.conftest import get_testing_print_function
+
+expected_calls = [
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 1,
+ "secret_name": "Dive Wilson",
+ "team_id": 2,
+ "name": "Deadpond",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": 48,
+ "id": 2,
+ "secret_name": "Tommy Sharp",
+ "team_id": 1,
+ "name": "Rusty-Man",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 3,
+ "secret_name": "Pedro Parqueador",
+ "team_id": None,
+ "name": "Spider-Boy",
+ },
+ ],
+ [
+ "Preventer Hero:",
+ {
+ "age": 48,
+ "id": 2,
+ "secret_name": "Tommy Sharp",
+ "team_id": 1,
+ "name": "Rusty-Man",
+ },
+ "Team:",
+ {"id": 1, "name": "Preventers", "headquarters": "Sharp Tower"},
+ ],
+]
+
+
+beforePYTHON3_9 = sys.version_info < (3, 9)
+reasonPEP593 = "Annotations(PEP593 https://peps.python.org/pep-0593/) only compatible with Python ver >= 3.9"
+
+
+@pytest.mark.skipif(beforePYTHON3_9, reason=reasonPEP593)
+@pytest.mark.asyncio()
+async def test_tutorial(clear_sqlmodel: Any) -> None:
+ from docs_src.tutorial_async.connect_async.select_async import (
+ tutorial005_async as mod,
+ )
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+ calls: List[Any] = []
+
+ new_print = get_testing_print_function(calls)
+
+ with patch("builtins.print", new=new_print):
+ await mod.main()
+ assert calls == expected_calls
diff --git a/tests/test_async_tutorial/test_async_connect/test_async_update/__init__.py b/tests/test_async_tutorial/test_async_connect/test_async_update/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/test_async_tutorial/test_async_connect/test_async_update/test_async_tutorial001.py b/tests/test_async_tutorial/test_async_connect/test_async_update/test_async_tutorial001.py
new file mode 100644
index 0000000000..b62072ab93
--- /dev/null
+++ b/tests/test_async_tutorial/test_async_connect/test_async_update/test_async_tutorial001.py
@@ -0,0 +1,73 @@
+import sys
+from typing import Any, List
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy.ext.asyncio import create_async_engine
+
+from tests.conftest import get_testing_print_function
+
+expected_calls = [
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 1,
+ "secret_name": "Dive Wilson",
+ "team_id": 2,
+ "name": "Deadpond",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": 48,
+ "id": 2,
+ "secret_name": "Tommy Sharp",
+ "team_id": 1,
+ "name": "Rusty-Man",
+ },
+ ],
+ [
+ "Created hero:",
+ {
+ "age": None,
+ "id": 3,
+ "secret_name": "Pedro Parqueador",
+ "team_id": None,
+ "name": "Spider-Boy",
+ },
+ ],
+ [
+ "Updated hero:",
+ {
+ "age": None,
+ "id": 3,
+ "secret_name": "Pedro Parqueador",
+ "team_id": 1,
+ "name": "Spider-Boy",
+ },
+ ],
+]
+
+
+beforePYTHON3_9 = sys.version_info < (3, 9)
+reasonPEP593 = "Annotations(PEP593 https://peps.python.org/pep-0593/) only compatible with Python ver >= 3.9"
+
+
+@pytest.mark.skipif(beforePYTHON3_9, reason=reasonPEP593)
+@pytest.mark.asyncio()
+async def test_tutorial(clear_sqlmodel: Any) -> None:
+ from docs_src.tutorial_async.connect_async.update_async import (
+ tutorial001_async as mod,
+ )
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+ calls: List[Any] = []
+
+ new_print = get_testing_print_function(calls)
+
+ with patch("builtins.print", new=new_print):
+ await mod.main()
+ assert calls == expected_calls
diff --git a/tests/test_async_tutorial/test_async_select/__init__.py b/tests/test_async_tutorial/test_async_select/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/test_async_tutorial/test_async_select/test_async_tutorial001_tutorial002.py b/tests/test_async_tutorial/test_async_select/test_async_tutorial001_tutorial002.py
new file mode 100644
index 0000000000..4ec7fe464e
--- /dev/null
+++ b/tests/test_async_tutorial/test_async_select/test_async_tutorial001_tutorial002.py
@@ -0,0 +1,65 @@
+import sys
+from typing import Any, Dict, List, Union
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy.ext.asyncio import create_async_engine
+
+from tests.conftest import get_testing_print_function
+
+
+def check_calls(calls: List[List[Union[str, Dict[str, Any]]]]) -> None:
+ assert calls[0][0] == {
+ "name": "Deadpond",
+ "secret_name": "Dive Wilson",
+ "age": None,
+ "id": 1,
+ }
+ assert calls[1][0] == {
+ "name": "Spider-Boy",
+ "secret_name": "Pedro Parqueador",
+ "age": None,
+ "id": 2,
+ }
+ assert calls[2][0] == {
+ "name": "Rusty-Man",
+ "secret_name": "Tommy Sharp",
+ "age": 48,
+ "id": 3,
+ }
+
+
+beforePYTHON3_9 = sys.version_info < (3, 9)
+reasonPEP593 = "Annotations(PEP593 https://peps.python.org/pep-0593/) only compatible with Python ver >= 3.9"
+
+
+@pytest.mark.skipif(beforePYTHON3_9, reason=reasonPEP593)
+@pytest.mark.asyncio
+async def test_tutorial_async_001(clear_sqlmodel: Any) -> None:
+ from docs_src.tutorial_async.select_async import tutorial001_async as mod
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+ calls: List[Any] = []
+
+ new_print = get_testing_print_function(calls)
+
+ with patch("builtins.print", new=new_print):
+ await mod.main()
+ check_calls(calls)
+
+
+@pytest.mark.skipif(beforePYTHON3_9, reason=reasonPEP593)
+@pytest.mark.asyncio
+async def test_tutorial_async_002(clear_sqlmodel: Any) -> None:
+ from docs_src.tutorial_async.select_async import tutorial002_async as mod
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+ calls: List[Any] = []
+
+ new_print = get_testing_print_function(calls)
+
+ with patch("builtins.print", new=new_print):
+ await mod.main()
+ check_calls(calls)
diff --git a/tests/test_async_tutorial/test_async_select/test_async_tutorial003_tutorial004.py b/tests/test_async_tutorial/test_async_select/test_async_tutorial003_tutorial004.py
new file mode 100644
index 0000000000..88d3c79dfe
--- /dev/null
+++ b/tests/test_async_tutorial/test_async_select/test_async_tutorial003_tutorial004.py
@@ -0,0 +1,70 @@
+import sys
+from typing import Any, Dict, List, Union
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy.ext.asyncio import create_async_engine
+
+from tests.conftest import get_testing_print_function
+
+
+def check_calls(calls: List[List[List[Union[str, Dict[str, Any]]]]]) -> None:
+ expected_result: List[Union[str, Dict[str, Any]]] = [
+ {
+ "name": "Deadpond",
+ "secret_name": "Dive Wilson",
+ "age": None,
+ "id": 1,
+ },
+ {
+ "name": "Spider-Boy",
+ "secret_name": "Pedro Parqueador",
+ "age": None,
+ "id": 2,
+ },
+ {
+ "name": "Rusty-Man",
+ "secret_name": "Tommy Sharp",
+ "age": 48,
+ "id": 3,
+ },
+ ]
+
+ assert calls[0][0] == expected_result
+
+
+beforePYTHON3_9 = sys.version_info < (3, 9)
+reasonPEP593 = "Annotations(PEP593 https://peps.python.org/pep-0593/) only compatible with Python ver >= 3.9"
+
+
+@pytest.mark.skipif(beforePYTHON3_9, reason=reasonPEP593)
+@pytest.mark.asyncio
+async def test_tutorial_003(clear_sqlmodel: Any) -> None:
+ from docs_src.tutorial_async.select_async import tutorial003_async as mod
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+
+ calls: List[Any] = []
+
+ new_print = get_testing_print_function(calls)
+
+ with patch("builtins.print", new=new_print):
+ await mod.main()
+ check_calls(calls)
+
+
+@pytest.mark.skipif(beforePYTHON3_9, reason=reasonPEP593)
+@pytest.mark.asyncio
+async def test_tutorial_004(clear_sqlmodel: Any) -> None:
+ from docs_src.tutorial_async.select_async import tutorial004_async as mod
+
+ mod.sqlite_url = "sqlite+aiosqlite://"
+ mod.engine = create_async_engine(mod.sqlite_url)
+ calls: List[Any] = []
+
+ new_print = get_testing_print_function(calls)
+
+ with patch("builtins.print", new=new_print):
+ await mod.main()
+ check_calls(calls)
diff --git a/tests/test_main.py b/tests/test_main.py
index 22c62327da..51ecdf79f4 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -1,8 +1,9 @@
-from typing import Optional
+from typing import List, Optional
import pytest
from sqlalchemy.exc import IntegrityError
-from sqlmodel import Field, Session, SQLModel, create_engine
+from sqlalchemy.orm import RelationshipProperty
+from sqlmodel import Field, Relationship, Session, SQLModel, create_engine
def test_should_allow_duplicate_row_if_unique_constraint_is_not_passed(clear_sqlmodel):
@@ -91,3 +92,36 @@ class Hero(SQLModel, table=True):
session.add(hero_2)
session.commit()
session.refresh(hero_2)
+
+
+def test_sa_relationship(clear_sqlmodel):
+ """Test https://github.com/tiangolo/sqlmodel/issues/315#issuecomment-1272122306"""
+
+ class Team(SQLModel, table=True):
+ id: Optional[int] = Field(default=None, primary_key=True)
+ name: str = Field(unique=True)
+ heroes: List["Hero"] = Relationship( # noqa: F821
+ sa_relationship=RelationshipProperty("Hero", back_populates="team")
+ )
+
+ class Hero(SQLModel, table=True):
+ id: Optional[int] = Field(default=None, primary_key=True)
+ name: str = Field(unique=True)
+ team_id: Optional[int] = Field(default=None, foreign_key="team.id")
+ team: Optional[Team] = Relationship(
+ sa_relationship=RelationshipProperty("Team", back_populates="heroes")
+ )
+
+ team_preventers = Team(name="Preventers")
+ hero_rusty_man = Hero(name="Rusty-Man", team=team_preventers)
+
+ engine = create_engine("sqlite://", echo=True)
+
+ SQLModel.metadata.create_all(engine)
+
+ with Session(engine) as session:
+ session.add(hero_rusty_man)
+ session.commit()
+ session.refresh(hero_rusty_man)
+ # The next statement should not raise an AttributeError
+ assert hero_rusty_man.team.name == "Preventers"
diff --git a/tests/test_tutorial/test_code_structure/test_tutorial001.py b/tests/test_tutorial/test_code_structure/test_tutorial001.py
index 4192e00434..3a2b2c89d6 100644
--- a/tests/test_tutorial/test_code_structure/test_tutorial001.py
+++ b/tests/test_tutorial/test_code_structure/test_tutorial001.py
@@ -13,6 +13,7 @@
"age": None,
"secret_name": "Dive Wilson",
"team_id": 1,
+ "experience_points": 1,
},
],
[
diff --git a/tests/test_tutorial/test_code_structure/test_tutorial002.py b/tests/test_tutorial/test_code_structure/test_tutorial002.py
index 37e4e54de6..00ebcb0d90 100644
--- a/tests/test_tutorial/test_code_structure/test_tutorial002.py
+++ b/tests/test_tutorial/test_code_structure/test_tutorial002.py
@@ -13,6 +13,7 @@
"age": None,
"secret_name": "Dive Wilson",
"team_id": 1,
+ "experience_points": 1,
},
],
[
diff --git a/tests/test_tutorial/test_fastapi/test_delete/test_tutorial001.py b/tests/test_tutorial/test_fastapi/test_delete/test_tutorial001.py
index e560d04c0e..9de5de1fa8 100644
--- a/tests/test_tutorial/test_fastapi/test_delete/test_tutorial001.py
+++ b/tests/test_tutorial/test_fastapi/test_delete/test_tutorial001.py
@@ -241,7 +241,7 @@
"loc": {
"title": "Location",
"type": "array",
- "items": {"type": "string"},
+ "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
diff --git a/tests/test_tutorial/test_fastapi/test_limit_and_offset/test_tutorial001.py b/tests/test_tutorial/test_fastapi/test_limit_and_offset/test_tutorial001.py
index b58afdf683..3425dd693e 100644
--- a/tests/test_tutorial/test_fastapi/test_limit_and_offset/test_tutorial001.py
+++ b/tests/test_tutorial/test_fastapi/test_limit_and_offset/test_tutorial001.py
@@ -164,7 +164,7 @@
"loc": {
"title": "Location",
"type": "array",
- "items": {"type": "string"},
+ "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
diff --git a/tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial001.py b/tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial001.py
index cf008563f4..4bf5a32b7e 100644
--- a/tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial001.py
+++ b/tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial001.py
@@ -103,7 +103,7 @@
"loc": {
"title": "Location",
"type": "array",
- "items": {"type": "string"},
+ "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
@@ -166,15 +166,25 @@ def test_tutorial(clear_sqlmodel):
data = response.json()
assert response.status_code == 200, response.text
-
+ print(data)
assert data == openapi_schema
# Test inherited indexes
insp: Inspector = inspect(mod.engine)
indexes = insp.get_indexes(str(mod.Hero.__tablename__))
expected_indexes = [
- {"name": "ix_hero_name", "column_names": ["name"], "unique": 0},
- {"name": "ix_hero_age", "column_names": ["age"], "unique": 0},
+ {
+ "name": "ix_hero_name",
+ "column_names": ["name"],
+ "unique": 0,
+ "dialect_options": {},
+ },
+ {
+ "name": "ix_hero_age",
+ "column_names": ["age"],
+ "unique": 0,
+ "dialect_options": {},
+ },
]
for index in expected_indexes:
assert index in indexes, "This expected index should be in the indexes in DB"
diff --git a/tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial002.py b/tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial002.py
index 57393a7ddc..0bd18732c5 100644
--- a/tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial002.py
+++ b/tests/test_tutorial/test_fastapi/test_multiple_models/test_tutorial002.py
@@ -103,7 +103,7 @@
"loc": {
"title": "Location",
"type": "array",
- "items": {"type": "string"},
+ "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
@@ -173,8 +173,18 @@ def test_tutorial(clear_sqlmodel):
insp: Inspector = inspect(mod.engine)
indexes = insp.get_indexes(str(mod.Hero.__tablename__))
expected_indexes = [
- {"name": "ix_hero_age", "column_names": ["age"], "unique": 0},
- {"name": "ix_hero_name", "column_names": ["name"], "unique": 0},
+ {
+ "name": "ix_hero_age",
+ "column_names": ["age"],
+ "unique": 0,
+ "dialect_options": {},
+ },
+ {
+ "name": "ix_hero_name",
+ "column_names": ["name"],
+ "unique": 0,
+ "dialect_options": {},
+ },
]
for index in expected_indexes:
assert index in indexes, "This expected index should be in the indexes in DB"
diff --git a/tests/test_tutorial/test_fastapi/test_read_one/test_tutorial001.py b/tests/test_tutorial/test_fastapi/test_read_one/test_tutorial001.py
index 5b3c771bb9..4cce5a6674 100644
--- a/tests/test_tutorial/test_fastapi/test_read_one/test_tutorial001.py
+++ b/tests/test_tutorial/test_fastapi/test_read_one/test_tutorial001.py
@@ -135,7 +135,7 @@
"loc": {
"title": "Location",
"type": "array",
- "items": {"type": "string"},
+ "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
diff --git a/tests/test_tutorial/test_fastapi/test_relationships/test_tutorial001.py b/tests/test_tutorial/test_fastapi/test_relationships/test_tutorial001.py
index 125e00179c..e631660e13 100644
--- a/tests/test_tutorial/test_fastapi/test_relationships/test_tutorial001.py
+++ b/tests/test_tutorial/test_fastapi/test_relationships/test_tutorial001.py
@@ -488,7 +488,7 @@
"loc": {
"title": "Location",
"type": "array",
- "items": {"type": "string"},
+ "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
diff --git a/tests/test_tutorial/test_fastapi/test_response_model/test_tutorial001.py b/tests/test_tutorial/test_fastapi/test_response_model/test_tutorial001.py
index 54fbbdccad..fc94c64337 100644
--- a/tests/test_tutorial/test_fastapi/test_response_model/test_tutorial001.py
+++ b/tests/test_tutorial/test_fastapi/test_response_model/test_tutorial001.py
@@ -91,7 +91,7 @@
"loc": {
"title": "Location",
"type": "array",
- "items": {"type": "string"},
+ "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
diff --git a/tests/test_tutorial/test_fastapi/test_session_with_dependency/test_tutorial001.py b/tests/test_tutorial/test_fastapi/test_session_with_dependency/test_tutorial001.py
index d8dbe3f7fb..333f471fe2 100644
--- a/tests/test_tutorial/test_fastapi/test_session_with_dependency/test_tutorial001.py
+++ b/tests/test_tutorial/test_fastapi/test_session_with_dependency/test_tutorial001.py
@@ -241,7 +241,7 @@
"loc": {
"title": "Location",
"type": "array",
- "items": {"type": "string"},
+ "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
diff --git a/tests/test_tutorial/test_fastapi/test_simple_hero_api/test_tutorial001.py b/tests/test_tutorial/test_fastapi/test_simple_hero_api/test_tutorial001.py
index 2f87fafeff..6d784018e5 100644
--- a/tests/test_tutorial/test_fastapi/test_simple_hero_api/test_tutorial001.py
+++ b/tests/test_tutorial/test_fastapi/test_simple_hero_api/test_tutorial001.py
@@ -79,7 +79,7 @@
"loc": {
"title": "Location",
"type": "array",
- "items": {"type": "string"},
+ "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
diff --git a/tests/test_tutorial/test_fastapi/test_teams/test_tutorial001.py b/tests/test_tutorial/test_fastapi/test_teams/test_tutorial001.py
index 6ac1cffc5e..5d049d3720 100644
--- a/tests/test_tutorial/test_fastapi/test_teams/test_tutorial001.py
+++ b/tests/test_tutorial/test_fastapi/test_teams/test_tutorial001.py
@@ -454,7 +454,7 @@
"loc": {
"title": "Location",
"type": "array",
- "items": {"type": "string"},
+ "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
diff --git a/tests/test_tutorial/test_fastapi/test_update/test_tutorial001.py b/tests/test_tutorial/test_fastapi/test_update/test_tutorial001.py
index e622fd37fb..8a9b60e61a 100644
--- a/tests/test_tutorial/test_fastapi/test_update/test_tutorial001.py
+++ b/tests/test_tutorial/test_fastapi/test_update/test_tutorial001.py
@@ -213,7 +213,7 @@
"loc": {
"title": "Location",
"type": "array",
- "items": {"type": "string"},
+ "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
diff --git a/tests/test_tutorial/test_indexes/test_tutorial001.py b/tests/test_tutorial/test_indexes/test_tutorial001.py
index 596207737d..bc89522a67 100644
--- a/tests/test_tutorial/test_indexes/test_tutorial001.py
+++ b/tests/test_tutorial/test_indexes/test_tutorial001.py
@@ -25,8 +25,18 @@ def test_tutorial(clear_sqlmodel):
insp: Inspector = inspect(mod.engine)
indexes = insp.get_indexes(str(mod.Hero.__tablename__))
expected_indexes = [
- {"name": "ix_hero_name", "column_names": ["name"], "unique": 0},
- {"name": "ix_hero_age", "column_names": ["age"], "unique": 0},
+ {
+ "name": "ix_hero_name",
+ "column_names": ["name"],
+ "unique": 0,
+ "dialect_options": {},
+ },
+ {
+ "name": "ix_hero_age",
+ "column_names": ["age"],
+ "unique": 0,
+ "dialect_options": {},
+ },
]
for index in expected_indexes:
assert index in indexes, "This expected index should be in the indexes in DB"
diff --git a/tests/test_tutorial/test_indexes/test_tutorial006.py b/tests/test_tutorial/test_indexes/test_tutorial006.py
index e26f8b2ed8..8d574dd0df 100644
--- a/tests/test_tutorial/test_indexes/test_tutorial006.py
+++ b/tests/test_tutorial/test_indexes/test_tutorial006.py
@@ -26,8 +26,18 @@ def test_tutorial(clear_sqlmodel):
insp: Inspector = inspect(mod.engine)
indexes = insp.get_indexes(str(mod.Hero.__tablename__))
expected_indexes = [
- {"name": "ix_hero_name", "column_names": ["name"], "unique": 0},
- {"name": "ix_hero_age", "column_names": ["age"], "unique": 0},
+ {
+ "name": "ix_hero_name",
+ "column_names": ["name"],
+ "unique": 0,
+ "dialect_options": {},
+ },
+ {
+ "name": "ix_hero_age",
+ "column_names": ["age"],
+ "unique": 0,
+ "dialect_options": {},
+ },
]
for index in expected_indexes:
assert index in indexes, "This expected index should be in the indexes in DB"