diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml deleted file mode 100644 index 0ffc101a3f..0000000000 --- a/.github/FUNDING.yml +++ /dev/null @@ -1 +0,0 @@ -github: [tiangolo] diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml deleted file mode 100644 index b4f46138cd..0000000000 --- a/.github/ISSUE_TEMPLATE/config.yml +++ /dev/null @@ -1,13 +0,0 @@ -blank_issues_enabled: false -contact_links: - - name: Security Contact - about: Please report security vulnerabilities to security@tiangolo.com - - name: Question or Problem - about: Ask a question or ask about a problem in GitHub Discussions. - url: https://github.com/fastapi/sqlmodel/discussions/categories/questions - - name: Feature Request - about: To suggest an idea or ask about a feature, please start with a question saying what you would like to achieve. There might be a way to do it already. - url: https://github.com/fastapi/sqlmodel/discussions/categories/questions - - name: Show and tell - about: Show what you built with SQLModel or to be used with SQLModel. - url: https://github.com/fastapi/sqlmodel/discussions/categories/show-and-tell diff --git a/.github/ISSUE_TEMPLATE/privileged.yml b/.github/ISSUE_TEMPLATE/privileged.yml deleted file mode 100644 index 8cc6002633..0000000000 --- a/.github/ISSUE_TEMPLATE/privileged.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: Privileged -description: You are @tiangolo or he asked you directly to create an issue here. If not, check the other options. πŸ‘‡ -body: - - type: markdown - attributes: - value: | - Thanks for your interest in SQLModel! πŸš€ - - If you are not @tiangolo or he didn't ask you directly to create an issue here, please start the conversation in a [Question in GitHub Discussions](https://github.com/fastapi/sqlmodel/discussions/categories/questions) instead. - - type: checkboxes - id: privileged - attributes: - label: Privileged issue - description: Confirm that you are allowed to create an issue here. - options: - - label: I'm @tiangolo or he asked me directly to create an issue here. - required: true - - type: textarea - id: content - attributes: - label: Issue Content - description: Add the content of the issue here. diff --git a/.github/dependabot.yml b/.github/dependabot.yml index cd972a0ba4..959ec970fb 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -8,7 +8,7 @@ updates: commit-message: prefix: ⬆ # Python - - package-ecosystem: "pip" + - package-ecosystem: "uv" directory: "/" schedule: interval: "daily" diff --git a/.github/workflows/add-to-project.yml b/.github/workflows/add-to-project.yml deleted file mode 100644 index dccea83f35..0000000000 --- a/.github/workflows/add-to-project.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: Add to Project - -on: - pull_request_target: - issues: - types: - - opened - - reopened - -jobs: - add-to-project: - name: Add to project - runs-on: ubuntu-latest - steps: - - uses: actions/add-to-project@v1.0.2 - with: - project-url: https://github.com/orgs/fastapi/projects/2 - github-token: ${{ secrets.PROJECTS_TOKEN }} diff --git a/.github/workflows/build-docs.yml b/.github/workflows/build-docs.yml deleted file mode 100644 index 0e44035915..0000000000 --- a/.github/workflows/build-docs.yml +++ /dev/null @@ -1,101 +0,0 @@ -name: Build Docs -on: - push: - branches: - - main - pull_request: - types: - - opened - - synchronize - -env: - UV_SYSTEM_PYTHON: 1 - - -jobs: - changes: - runs-on: ubuntu-latest - # Required permissions - permissions: - pull-requests: read - # Set job outputs to values from filter step - outputs: - docs: ${{ steps.filter.outputs.docs }} - steps: - - uses: actions/checkout@v5 - # For pull requests it's not necessary to checkout the code but for the main branch it is - - uses: dorny/paths-filter@v3 - id: filter - with: - filters: | - docs: - - README.md - - docs/** - - docs_src/** - - requirements-docs.txt - - requirements-docs-insiders.txt - - pyproject.toml - - mkdocs.yml - - mkdocs.insiders.yml - - mkdocs.maybe-insiders.yml - - mkdocs.no-insiders.yml - - .github/workflows/build-docs.yml - - .github/workflows/deploy-docs.yml - - data/** - - build-docs: - needs: - - changes - if: ${{ needs.changes.outputs.docs == 'true' }} - runs-on: ubuntu-latest - steps: - - name: Dump GitHub context - env: - GITHUB_CONTEXT: ${{ toJson(github) }} - run: echo "$GITHUB_CONTEXT" - - uses: actions/checkout@v5 - - name: Set up Python - uses: actions/setup-python@v6 - with: - python-version: "3.11" - - name: Setup uv - uses: astral-sh/setup-uv@v7 - with: - version: "0.4.15" - enable-cache: true - cache-dependency-glob: | - requirements**.txt - pyproject.toml - - name: Install docs extras - run: uv pip install -r requirements-docs.txt - - name: Install Material for MkDocs Insiders - if: ( github.event_name != 'pull_request' || github.secret_source == 'Actions' ) - run: uv pip install -r requirements-docs-insiders.txt - env: - TOKEN: ${{ secrets.SQLMODEL_MKDOCS_MATERIAL_INSIDERS }} - - uses: actions/cache@v4 - with: - key: mkdocs-cards-${{ github.ref }} - path: .cache - - name: Verify README - run: python ./scripts/docs.py verify-readme - - name: Build Docs - run: python ./scripts/docs.py build - - uses: actions/upload-artifact@v4 - with: - name: docs-site - path: ./site/** - include-hidden-files: true - - # https://github.com/marketplace/actions/alls-green#why - docs-all-green: # This job does nothing and is only used for the branch protection - if: always() - needs: - - build-docs - runs-on: ubuntu-latest - steps: - - name: Decide whether the needed jobs succeeded or failed - uses: re-actors/alls-green@release/v1 - with: - jobs: ${{ toJSON(needs) }} - allowed-skips: build-docs diff --git a/.github/workflows/deploy-docs.yml b/.github/workflows/deploy-docs.yml deleted file mode 100644 index 0704bbcb30..0000000000 --- a/.github/workflows/deploy-docs.yml +++ /dev/null @@ -1,86 +0,0 @@ -name: Deploy Docs -on: - workflow_run: - workflows: - - Build Docs - types: - - completed - -permissions: - deployments: write - issues: write - pull-requests: write - statuses: write - -env: - UV_SYSTEM_PYTHON: 1 - -jobs: - deploy-docs: - runs-on: ubuntu-latest - steps: - - name: Dump GitHub context - env: - GITHUB_CONTEXT: ${{ toJson(github) }} - run: echo "$GITHUB_CONTEXT" - - uses: actions/checkout@v5 - - name: Set up Python - uses: actions/setup-python@v6 - with: - python-version: "3.11" - - name: Setup uv - uses: astral-sh/setup-uv@v7 - with: - version: "0.4.15" - enable-cache: true - cache-dependency-glob: | - requirements**.txt - pyproject.toml - - name: Install GitHub Actions dependencies - run: uv pip install -r requirements-github-actions.txt - - name: Deploy Docs Status Pending - run: python ./scripts/deploy_docs_status.py - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - COMMIT_SHA: ${{ github.event.workflow_run.head_sha }} - RUN_ID: ${{ github.run_id }} - STATE: "pending" - - name: Clean site - run: | - rm -rf ./site - mkdir ./site - - uses: actions/download-artifact@v5 - with: - path: ./site/ - pattern: docs-site - merge-multiple: true - github-token: ${{ secrets.GITHUB_TOKEN }} - run-id: ${{ github.event.workflow_run.id }} - - name: Deploy to Cloudflare Pages - # hashFiles returns an empty string if there are no files - if: hashFiles('./site/*') - id: deploy - env: - PROJECT_NAME: sqlmodel - BRANCH: ${{ ( github.event.workflow_run.head_repository.full_name == github.repository && github.event.workflow_run.head_branch == 'main' && 'main' ) || ( github.event.workflow_run.head_sha ) }} - uses: cloudflare/wrangler-action@v3 - with: - apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} - accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - command: pages deploy ./site --project-name=${{ env.PROJECT_NAME }} --branch=${{ env.BRANCH }} - - name: Deploy Docs Status Error - if: failure() - run: python ./scripts/deploy_docs_status.py - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - COMMIT_SHA: ${{ github.event.workflow_run.head_sha }} - RUN_ID: ${{ github.run_id }} - STATE: "error" - - name: Comment Deploy - run: python ./scripts/deploy_docs_status.py - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - DEPLOY_URL: ${{ steps.deploy.outputs.deployment-url }} - COMMIT_SHA: ${{ github.event.workflow_run.head_sha }} - RUN_ID: ${{ github.run_id }} - STATE: "success" diff --git a/.github/workflows/detect-conflicts.yml b/.github/workflows/detect-conflicts.yml deleted file mode 100644 index aba329db85..0000000000 --- a/.github/workflows/detect-conflicts.yml +++ /dev/null @@ -1,19 +0,0 @@ -name: "Conflict detector" -on: - push: - pull_request_target: - types: [synchronize] - -jobs: - main: - permissions: - contents: read - pull-requests: write - runs-on: ubuntu-latest - steps: - - name: Check if PRs have merge conflicts - uses: eps1lon/actions-label-merge-conflict@v3 - with: - dirtyLabel: "conflicts" - repoToken: "${{ secrets.GITHUB_TOKEN }}" - commentOnDirty: "This pull request has a merge conflict that needs to be resolved." diff --git a/.github/workflows/issue-manager.yml b/.github/workflows/issue-manager.yml deleted file mode 100644 index 137ed16de3..0000000000 --- a/.github/workflows/issue-manager.yml +++ /dev/null @@ -1,51 +0,0 @@ -name: Issue Manager - -on: - schedule: - - cron: "13 18 * * *" - issue_comment: - types: - - created - issues: - types: - - labeled - pull_request_target: - types: - - labeled - workflow_dispatch: - -permissions: - issues: write - pull-requests: write - -jobs: - issue-manager: - if: github.repository_owner == 'fastapi' - runs-on: ubuntu-latest - steps: - - name: Dump GitHub context - env: - GITHUB_CONTEXT: ${{ toJson(github) }} - run: echo "$GITHUB_CONTEXT" - - uses: tiangolo/issue-manager@0.6.0 - with: - token: ${{ secrets.GITHUB_TOKEN }} - config: > - { - "answered": { - "delay": 864000, - "message": "Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs." - }, - "waiting": { - "delay": 2628000, - "message": "As this PR has been waiting for the original user for a while but seems to be inactive, it's now going to be closed. But if there's anyone interested, feel free to create a new PR.", - "reminder": { - "before": "P3D", - "message": "Heads-up: this will be closed in 3 days unless there’s new activity." - } - }, - "invalid": { - "delay": 0, - "message": "This was marked as invalid and will be closed now. If this is an error, please provide additional details." - } - } diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index b35142f50e..4320f9534e 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,37 +1,60 @@ -name: Publish +name: Publish to PyPI on: - release: - types: - - created - workflow_dispatch: - inputs: - debug_enabled: - description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)' - required: false - default: 'false' + push: + branches: [main] + tags: ["v*"] + pull_request: + branches: [main] jobs: - publish: + publish-production: runs-on: ubuntu-latest - strategy: - matrix: - package: - - sqlmodel - - sqlmodel-slim + if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) permissions: - id-token: write + id-token: write # Required for Trusted Publishing + contents: write # Required for pushing tags + steps: - - uses: actions/checkout@v5 + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Full history for version bumping + - name: Set up Python - uses: actions/setup-python@v6 + uses: actions/setup-python@v4 + with: + python-version: "3.13" + + - name: Install uv + uses: astral-sh/setup-uv@v2 with: - python-version: "3.11" - - name: Install build dependencies - run: pip install build - - name: Build distribution - env: - TIANGOLO_BUILD_PACKAGE: ${{ matrix.package }} - run: python -m build - - name: Publish - uses: pypa/gh-action-pypi-publish@v1.13.0 + version: "latest" + + - name: Configure Git + run: | + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + # Configure Git to use the token for authentication + git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git + + - name: Install dependencies + run: uv sync + + - name: Build package + run: uv build + + - name: Publish to PyPI + if: github.ref == 'refs/heads/main' + uses: pypa/gh-action-pypi-publish@release/v1 + # Trusted Publishing is enabled by default when no password is provided + # and id-token: write permission is set + # Only publish when pushing to main, not when tags are pushed + # (tags trigger this workflow again, causing duplicate publish attempts) + + - name: Create Release + if: github.ref == 'refs/heads/main' + run: | + VERSION=$(uv version --short) + git tag -a "v$VERSION" -m "Release v$VERSION" + git push origin "v$VERSION" diff --git a/.github/workflows/test-redistribute.yml b/.github/workflows/test-redistribute.yml deleted file mode 100644 index 2a7ff20d31..0000000000 --- a/.github/workflows/test-redistribute.yml +++ /dev/null @@ -1,65 +0,0 @@ -name: Test Redistribute - -on: - push: - branches: - - main - pull_request: - types: - - opened - - synchronize - -jobs: - test-redistribute: - runs-on: ubuntu-latest - strategy: - matrix: - package: - - sqlmodel - - sqlmodel-slim - steps: - - name: Dump GitHub context - env: - GITHUB_CONTEXT: ${{ toJson(github) }} - run: echo "$GITHUB_CONTEXT" - - uses: actions/checkout@v5 - - name: Set up Python - uses: actions/setup-python@v6 - with: - python-version: "3.10" - - name: Install build dependencies - run: pip install build - - name: Build source distribution - env: - TIANGOLO_BUILD_PACKAGE: ${{ matrix.package }} - run: python -m build --sdist - - name: Decompress source distribution - run: | - cd dist - tar xvf sqlmodel*.tar.gz - - name: Install test dependencies - run: | - cd dist/sqlmodel*/ - pip install -r requirements-tests.txt - env: - TIANGOLO_BUILD_PACKAGE: ${{ matrix.package }} - - name: Run source distribution tests - run: | - cd dist/sqlmodel*/ - bash scripts/test.sh - - name: Build wheel distribution - run: | - cd dist - pip wheel --no-deps sqlmodel*.tar.gz - - # https://github.com/marketplace/actions/alls-green#why - test-redistribute-alls-green: # This job does nothing and is only used for the branch protection - if: always() - needs: - - test-redistribute - runs-on: ubuntu-latest - steps: - - name: Decide whether the needed jobs succeeded or failed - uses: re-actors/alls-green@release/v1 - with: - jobs: ${{ toJSON(needs) }} diff --git a/.gitignore b/.gitignore index 9e195bfa79..9cafc6935b 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ site *.db .cache .venv* +uv.lock diff --git a/CITATION.cff b/CITATION.cff deleted file mode 100644 index 5ea2d61946..0000000000 --- a/CITATION.cff +++ /dev/null @@ -1,24 +0,0 @@ -# This CITATION.cff file was generated with cffinit. -# Visit https://bit.ly/cffinit to generate yours today! - -cff-version: 1.2.0 -title: SQLModel -message: >- - If you use this software, please cite it using the - metadata from this file. -type: software -authors: - - given-names: SebastiΓ‘n - family-names: RamΓ­rez - email: tiangolo@gmail.com -identifiers: -repository-code: 'https://github.com/fastapi/sqlmodel' -url: 'https://sqlmodel.tiangolo.com' -abstract: >- - SQLModel, SQL databases in Python, designed for - simplicity, compatibility, and robustness. -keywords: - - fastapi - - pydantic - - sqlalchemy -license: MIT diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 47524515d5..0000000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1 +0,0 @@ -Please read the [Development - Contributing](https://sqlmodel.tiangolo.com/contributing/) guidelines in the documentation site. diff --git a/README.md b/README.md index 712167ffd6..0b5d8f645c 100644 --- a/README.md +++ b/README.md @@ -3,27 +3,19 @@

- SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness. + SQLModel with Protocol Buffers & gRPC support

- - Test - - - Publish - - - Coverage - - Package version - + A fork of SQLModel adding seamless protobuf/gRPC integration

--- -**Documentation**: https://sqlmodel.tiangolo.com +## πŸš€ What is this? -**Source Code**: https://github.com/fastapi/sqlmodel +This is a **fork of [SQLModel](https://github.com/fastapi/sqlmodel)** that extends the original library with **Protocol Buffers (protobuf) and gRPC integration**. SQLModel is a library for interacting with SQL databases from Python code, with Python objects. + +**Original SQLModel**: https://sqlmodel.tiangolo.com | GitHub --- @@ -38,14 +30,9 @@ The key features are: * **Compatible**: It is designed to be compatible with **FastAPI**, Pydantic, and SQLAlchemy. * **Extensible**: You have all the power of SQLAlchemy and Pydantic underneath. * **Short**: Minimize code duplication. A single type annotation does a lot of work. No need to duplicate models in SQLAlchemy and Pydantic. - -## Sponsors - - - - - - +* **πŸ†• Protocol Buffers Integration**: SQLModel classes now implement the protobuf `Message` interface, enabling direct serialization/deserialization and gRPC compatibility. +* **πŸ†• Automatic Descriptor Generation**: Protobuf descriptors are automatically synthesized from your SQLModel classes with intelligent type inference. +* **πŸ†• gRPC Ready**: Use your SQLModel models directly in gRPC services without additional conversion code. ## SQL Databases in FastAPI @@ -57,26 +44,87 @@ It combines SQLAlchemy and Pydantic and tries to simplify the code you write as **SQLModel** is, in fact, a thin layer on top of **Pydantic** and **SQLAlchemy**, carefully designed to be compatible with both. +## New Features: Protocol Buffers & gRPC Integration + +This fork extends SQLModel with seamless Protocol Buffers (protobuf) and gRPC support: + +### ✨ Key Additions + +1. **Protobuf Message Interface**: All SQLModel classes now inherit from `google.protobuf.message.Message`, making them fully compatible with protobuf serialization and gRPC. + +2. **Automatic Descriptor Generation**: Protobuf descriptors are automatically synthesized from your SQLModel class definitions, with intelligent type inference from Python type annotations. + +3. **Full Protobuf API**: All standard protobuf methods are implemented: + - `SerializeToString()` / `serialize_to_string()` - Serialize to binary + - `ParseFromString()` / `parse_from_string()` - Deserialize from binary + - `MergeFrom()` / `merge_from()` - Merge from another message + - `Clear()` / `clear()` - Reset to defaults + - `HasField()` / `has_field()` - Check field presence + - And more... + +4. **Type Inference**: Automatically maps Python types to protobuf field types: + - `int` β†’ `TYPE_INT64` + - `float` β†’ `TYPE_DOUBLE` + - `bool` β†’ `TYPE_BOOL` + - `str` β†’ `TYPE_STRING` + - `bytes` β†’ `TYPE_BYTES` + - Lists/tuples β†’ `LABEL_REPEATED` + +5. **Custom Field Descriptors**: Optionally provide custom protobuf field descriptors via `Field(grpc_descriptor=...)` for fine-grained control. + +### Example: Using SQLModel with gRPC + +```Python +from sqlmodel import Field, SQLModel + +class Hero(SQLModel, table=True): + id: int | None = Field(default=None, primary_key=True) + name: str + age: int | None = None + +# Create a hero instance +hero = Hero(name="Deadpond", age=30) + +# Serialize to protobuf binary format +serialized = hero.SerializeToString() + +# Deserialize back +hero2 = Hero() +hero2.ParseFromString(serialized) + +# Use directly in gRPC services! +# Your SQLModel classes work seamlessly with gRPC +``` + ## Requirements A recent and currently supported version of Python. As **SQLModel** is based on **Pydantic** and **SQLAlchemy**, it requires them. They will be automatically installed when you install SQLModel. +**Additional requirement for this fork**: `protobuf>=5.29.5` (automatically installed). + ## Installation -Make sure you create a virtual environment, activate it, and then install SQLModel, for example with: +Make sure you create a virtual environment, activate it, and then install this fork:
```console -$ pip install sqlmodel ----> 100% -Successfully installed sqlmodel +# Install from source (this fork) +$ uv pip install -e . + +# Or install directly from git +$ uv pip install git+https://github.com/YOUR_USERNAME/grpcmodel.git ```
+**Note**: For the original SQLModel without protobuf support, install from PyPI: +```console +$ uv pip install sqlmodel +``` + ## Example For an introduction to databases, SQL, and everything else, see the SQLModel documentation. @@ -206,7 +254,7 @@ with Session(engine) as session: -## SQLAlchemy and Pydantic +## SQLAlchemy, Pydantic, and Protocol Buffers That class `Hero` is a **SQLModel** model. @@ -214,6 +262,23 @@ But at the same time, ✨ it is a **SQLAlchemy** model ✨. So, you can combine And at the same time, ✨ it is also a **Pydantic** model ✨. You can use inheritance with it to define all your **data models** while avoiding code duplication. That makes it very easy to use with **FastAPI**. +**And now**, ✨ it is also a **protobuf Message** ✨. You can serialize it to binary format, use it directly in gRPC services, and interact with any protobuf-based system without additional conversion layers. + +## Differences from Original SQLModel + +This fork maintains 100% compatibility with the original SQLModel while adding: + +- βœ… All original SQLModel features work exactly as before +- βœ… New protobuf/gRPC capabilities seamlessly integrated +- βœ… Automatic descriptor generation (no extra code needed) +- βœ… Backward compatible - existing code continues to work + +## Contributing + +This is a fork of the original SQLModel. For issues and contributions related to: +- **Core SQLModel features**: Please refer to the [original SQLModel repository](https://github.com/fastapi/sqlmodel) +- **Protobuf/gRPC features**: Please open issues in this repository + ## License -This project is licensed under the terms of the [MIT license](https://github.com/fastapi/sqlmodel/blob/main/LICENSE). +This project is licensed under the terms of the [MIT license](LICENSE), same as the original SQLModel. diff --git a/data/members.yml b/data/members.yml deleted file mode 100644 index 7a51cbc9ac..0000000000 --- a/data/members.yml +++ /dev/null @@ -1,3 +0,0 @@ -members: -- login: tiangolo -- login: alejsdev diff --git a/data/sponsors.yml b/data/sponsors.yml deleted file mode 100644 index 95cf878530..0000000000 --- a/data/sponsors.yml +++ /dev/null @@ -1,6 +0,0 @@ -gold: [] -silver: - - url: https://www.govcert.lu - title: This project is being supported by GOVCERT.LU - img: https://sqlmodel.tiangolo.com/img/sponsors/govcert.png -bronze: [] diff --git a/docs/about/index.md b/docs/about/index.md deleted file mode 100644 index 53cae9270d..0000000000 --- a/docs/about/index.md +++ /dev/null @@ -1,3 +0,0 @@ -# About - -About **SQLModel**, its design, inspiration, and more. πŸ€“ diff --git a/docs/advanced/decimal.md b/docs/advanced/decimal.md deleted file mode 100644 index ce971b201b..0000000000 --- a/docs/advanced/decimal.md +++ /dev/null @@ -1,109 +0,0 @@ -# Decimal Numbers - -In some cases you might need to be able to store decimal numbers with guarantees about the precision. - -This is particularly important if you are storing things like **currencies**, **prices**, **accounts**, and others, as you would want to know that you wouldn't have rounding errors. - -As an example, if you open Python and sum `1.1` + `2.2` you would expect to see `3.3`, but you will actually get `3.3000000000000003`: - -```Python ->>> 1.1 + 2.2 -3.3000000000000003 -``` - -This is because of the way numbers are stored in "ones and zeros" (binary). But Python has a module and some types to have strict decimal values. You can read more about it in the official Python docs for Decimal. - -Because databases store data in the same ways as computers (in binary), they would have the same types of issues. And because of that, they also have a special **decimal** type. - -In most cases this would probably not be a problem, for example measuring views in a video, or the life bar in a videogame. But as you can imagine, this is particularly important when dealing with **money** and **finances**. - -## Decimal Types - -Pydantic has special support for `Decimal` types. - -When you use `Decimal` you can specify the number of digits and decimal places to support in the `Field()` function. They will be validated by Pydantic (for example when using FastAPI) and the same information will also be used for the database columns. - -/// info - -For the database, **SQLModel** will use SQLAlchemy's `DECIMAL` type. - -/// - -## Decimals in SQLModel - -Let's say that each hero in the database will have an amount of money. We could make that field a `Decimal` type using the `condecimal()` function: - -{* ./docs_src/advanced/decimal/tutorial001_py310.py ln[1:11] hl[11] *} - -Here we are saying that `money` can have at most `5` digits with `max_digits`, **this includes the integers** (to the left of the decimal dot) **and the decimals** (to the right of the decimal dot). - -We are also saying that the number of decimal places (to the right of the decimal dot) is `3`, so we can have **3 decimal digits** for these numbers in the `money` field. This means that we will have **2 digits for the integer part** and **3 digits for the decimal part**. - -βœ… So, for example, these are all valid numbers for the `money` field: - -* `12.345` -* `12.3` -* `12` -* `1.2` -* `0.123` -* `0` - -🚫 But these are all invalid numbers for that `money` field: - -* `1.2345` - * This number has more than 3 decimal places. -* `123.234` - * This number has more than 5 digits in total (integer and decimal part). -* `123` - * Even though this number doesn't have any decimals, we still have 3 places saved for them, which means that we can **only use 2 places** for the **integer part**, and this number has 3 integer digits. So, the allowed number of integer digits is `max_digits` - `decimal_places` = 2. - -/// tip - -Make sure you adjust the number of digits and decimal places for your own needs, in your own application. πŸ€“ - -/// - -## Create models with Decimals - -When creating new models you can actually pass normal (`float`) numbers, Pydantic will automatically convert them to `Decimal` types, and **SQLModel** will store them as `Decimal` types in the database (using SQLAlchemy). - -{* ./docs_src/advanced/decimal/tutorial001_py310.py ln[24:34] hl[25:27] *} - -## Select Decimal data - -Then, when working with Decimal types, you can confirm that they indeed avoid those rounding errors from floats: - -{* ./docs_src/advanced/decimal/tutorial001_py310.py ln[37:50] hl[49:50] *} - -## Review the results - -Now if you run this, instead of printing the unexpected number `3.3000000000000003`, it prints `3.300`: - -
- -```console -$ python app.py - -// Some boilerplate and previous output omitted πŸ˜‰ - -// The type of money is Decimal('1.100') -Hero 1: id=1 secret_name='Dive Wilson' age=None name='Deadpond' money=Decimal('1.100') - -// More output omitted here πŸ€“ - -// The type of money is Decimal('1.100') -Hero 2: id=3 secret_name='Tommy Sharp' age=48 name='Rusty-Man' money=Decimal('2.200') - -// No rounding errors, just 3.3! πŸŽ‰ -Total money: 3.300 -``` - -
- -/// warning - -Although Decimal types are supported and used in the Python side, not all databases support it. In particular, SQLite doesn't support decimals, so it will convert them to the same floating `NUMERIC` type it supports. - -But decimals are supported by most of the other SQL databases. πŸŽ‰ - -/// diff --git a/docs/advanced/index.md b/docs/advanced/index.md deleted file mode 100644 index f6178249ce..0000000000 --- a/docs/advanced/index.md +++ /dev/null @@ -1,10 +0,0 @@ -# Advanced User Guide - -The **Advanced User Guide** is gradually growing, you can already read about some advanced topics. - -At some point it will include: - -* How to use `async` and `await` with the async session. -* How to run migrations. -* How to combine **SQLModel** models with SQLAlchemy. -* ...and more. πŸ€“ diff --git a/docs/advanced/uuid.md b/docs/advanced/uuid.md deleted file mode 100644 index abfcfeda2b..0000000000 --- a/docs/advanced/uuid.md +++ /dev/null @@ -1,178 +0,0 @@ -# UUID (Universally Unique Identifiers) - -We have discussed some data types like `str`, `int`, etc. - -There's another data type called `UUID` (Universally Unique Identifier). - -You might have seen **UUIDs**, for example in URLs. They look something like this: - -``` -4ff2dab7-bffe-414d-88a5-1826b9fea8df -``` - -UUIDs can be particularly useful as an alternative to auto-incrementing integers for **primary keys**. - -/// info - -Official support for UUIDs was added in SQLModel version `0.0.20`. - -/// - -## About UUIDs - -UUIDs are numbers with 128 bits, that is, 16 bytes. - -They are normally seen as 32 hexadecimal characters separated by dashes. - -There are several versions of UUID, some versions include the current time in the bytes, but **UUIDs version 4** are mainly random, the way they are generated makes them virtually **unique**. - -### Distributed UUIDs - -You could generate one UUID in one computer, and someone else could generate another UUID in another computer, and it would be almost **impossible** for both UUIDs to be the **same**. - -This means that you don't have to wait for the DB to generate the ID for you, you can **generate it in code before sending it to the database**, because you can be quite certain it will be unique. - -/// note | Technical Details - -Because the number of possible UUIDs is so large (2^128), the probability of generating the same UUID version 4 (the random ones) twice is very low. - -If you had 103 trillion version 4 UUIDs stored in the database, the probability of generating a duplicated new one is one in a billion. πŸ€“ - -/// - -For the same reason, if you decided to migrate your database, combine it with another database and mix records, etc. you would most probably be able to **just use the same UUIDs** you had originally. - -/// warning - -There's still a chance you could have a collision, but it's very low. In most cases you could assume you wouldn't have it, but it would be good to be prepared for it. - -/// - -### UUIDs Prevent Information Leakage - -Because UUIDs version 4 are **random**, you could give these IDs to the application users or to other systems, **without exposing information** about your application. - -When using **auto-incremented integers** for primary keys, you could implicitly expose information about your system. For example, someone could create a new hero, and by getting the hero ID `20` **they would know that you have 20 heroes** in your system (or even less, if some heroes were already deleted). - -### UUID Storage - -Because UUIDs are 16 bytes, they would **consume more space** in the database than a smaller auto-incremented integer (commonly 4 bytes). - -Depending on the database you use, UUIDs could have **better or worse performance**. If you are concerned about that, you should check the documentation for the specific database. - -SQLite doesn't have a specific UUID type, so it will store the UUID as a string. Other databases like Postgres have a specific UUID type which would result in better performance and space usage than strings. - -## Models with UUIDs - -To use UUIDs as primary keys we need to import `uuid`, which is part of the Python standard library (we don't have to install anything) and use `uuid.UUID` as the **type** for the ID field. - -We also want the Python code to **generate a new UUID** when creating a new instance, so we use `default_factory`. - -The parameter `default_factory` takes a function (or in general, a "callable"). This function will be **called when creating a new instance** of the model and the value returned by the function will be used as the default value for the field. - -For the function in `default_factory` we pass `uuid.uuid4`, which is a function that generates a **new UUID version 4**. - -/// tip - -We don't call `uuid.uuid4()` ourselves in the code (we don't put the parenthesis). Instead, we pass the function itself, just `uuid.uuid4`, so that SQLModel can call it every time we create a new instance. - -/// - -This means that the UUID will be generated in the Python code, **before sending the data to the database**. - -{* ./docs_src/advanced/uuid/tutorial001_py310.py ln[1:10] hl[1,7] *} - -Pydantic has support for `UUID` types. - -For the database, **SQLModel** internally uses SQLAlchemy's `Uuid` type. - -### Create a Record with a UUID - -When creating a `Hero` record, the `id` field will be **automatically populated** with a new UUID because we set `default_factory=uuid.uuid4`. - -As `uuid.uuid4` will be called when creating the model instance, even before sending it to the database, we can **access and use the ID right away**. - -And that **same ID (a UUID)** will be saved in the database. - -{* ./docs_src/advanced/uuid/tutorial001_py310.py ln[23:34] hl[25,27,29,34] *} - -### Select a Hero - -We can do the same operations we could do with other fields. - -For example we can **select a hero by ID**: - -{* ./docs_src/advanced/uuid/tutorial001_py310.py ln[37:54] hl[49] *} - -/// tip - -Even if a database like SQLite stores the UUID as a string, we can select and run comparisons using a Python UUID object and it will work. - -SQLModel (actually SQLAlchemy) will take care of making it work. ✨ - -/// - -#### Select with `session.get()` - -We could also select by ID with `session.get()`: - -{* ./docs_src/advanced/uuid/tutorial002_py310.py ln[37:53] hl[49] *} - -The same way as with other fields, we could update, delete, etc. πŸš€ - -### Run the program - -If you run the program, you will see the **UUID** generated in the Python code, and then the record **saved in the database with the same UUID**. - -
- -```console -$ python app.py - -// Some boilerplate and previous output omitted πŸ˜‰ - -// In SQLite, the UUID will be stored as a string -// other DBs like Postgres have a specific UUID type -CREATE TABLE hero ( - id CHAR(32) NOT NULL, - name VARCHAR NOT NULL, - secret_name VARCHAR NOT NULL, - age INTEGER, - PRIMARY KEY (id) -) - -// Before saving in the DB we already have the UUID -The hero before saving in the DB -name='Deadpond' secret_name='Dive Wilson' id=UUID('0e44c1a6-88d3-4a35-8b8a-307faa2def28') age=None -The hero ID was already set -0e44c1a6-88d3-4a35-8b8a-307faa2def28 - -// The SQL statement to insert the record uses our UUID -INSERT INTO hero (id, name, secret_name, age) VALUES (?, ?, ?, ?) -('0e44c1a688d34a358b8a307faa2def28', 'Deadpond', 'Dive Wilson', None) - -// And indeed, the record was saved with the UUID we created 😎 -After saving in the DB -age=None id=UUID('0e44c1a6-88d3-4a35-8b8a-307faa2def28') name='Deadpond' secret_name='Dive Wilson' - -// Now we create a new hero (to select it in a bit) -Created hero: -age=None id=UUID('9d90d186-85db-4eaa-891a-def7b4ae2dab') name='Spider-Boy' secret_name='Pedro Parqueador' -Created hero ID: -9d90d186-85db-4eaa-891a-def7b4ae2dab - -// And now we select it -Selected hero: -age=None id=UUID('9d90d186-85db-4eaa-891a-def7b4ae2dab') name='Spider-Boy' secret_name='Pedro Parqueador' -Selected hero ID: -9d90d186-85db-4eaa-891a-def7b4ae2dab -``` - -
- -## Learn More - -You can learn more about **UUIDs** in: - -* The official Python docs for UUID. -* The Wikipedia for UUID. diff --git a/docs/alternatives.md b/docs/alternatives.md deleted file mode 100644 index d0a53727cb..0000000000 --- a/docs/alternatives.md +++ /dev/null @@ -1,3 +0,0 @@ -# Alternatives, Inspiration and Comparisons - -Coming soon... diff --git a/docs/contributing.md b/docs/contributing.md deleted file mode 100644 index 8228bc4132..0000000000 --- a/docs/contributing.md +++ /dev/null @@ -1,164 +0,0 @@ -# Contributing - -First, you might want to see the basic ways to [help SQLModel and get help](help.md){.internal-link target=_blank}. - -## Developing - -If you already cloned the sqlmodel repository and you want to deep dive in the code, here are some guidelines to set up your environment. - -### Virtual Environment - -Follow the instructions to create and activate a [virtual environment](virtual-environments.md){.internal-link target=_blank} for the internal code of `sqlmodel`. - -### Install Requirements Using `pip` - -After activating the environment, install the required packages: - -
- -```console -$ pip install -r requirements.txt - ----> 100% -``` - -
- -It will install all the dependencies and your local SQLModel in your local environment. - -### Using your Local SQLModel - -If you create a Python file that imports and uses SQLModel, and run it with the Python from your local environment, it will use your cloned local SQLModel source code. - -And if you update that local SQLModel source code when you run that Python file again, it will use the fresh version of SQLModel you just edited. - -That way, you don't have to "install" your local version to be able to test every change. - -/// note | "Technical Details" - -This only happens when you install using this included `requirements.txt` instead of running `pip install sqlmodel` directly. - -That is because inside the `requirements.txt` file, the local version of SQLModel is marked to be installed in "editable" mode, with the `-e` option. - -/// - -### Format - -There is a script that you can run that will format and clean all your code: - -
- -```console -$ bash scripts/format.sh -``` - -
- -It will also auto-sort all your imports. - -## Tests - -There is a script that you can run locally to test all the code and generate coverage reports in HTML: - -
- -```console -$ bash scripts/test.sh -``` - -
- -This command generates a directory `./htmlcov/`, if you open the file `./htmlcov/index.html` in your browser, you can explore interactively the regions of code that are covered by the tests, and notice if there is any region missing. - -## Docs - -First, make sure you set up your environment as described above, that will install all the requirements. - -### Docs Live - -During local development, there is a script that builds the site and checks for any changes, live-reloading: - -
- -```console -$ python ./scripts/docs.py live - -[INFO] Serving on http://127.0.0.1:8008 -[INFO] Start watching changes -[INFO] Start detecting changes -``` - -
- -It will serve the documentation on `http://127.0.0.1:8008`. - -That way, you can edit the documentation/source files and see the changes live. - -/// tip - -Alternatively, you can perform the same steps that scripts does manually. - -Go into the docs director at `docs/`: - -```console -$ cd docs/ -``` - -Then run `mkdocs` in that directory: - -```console -$ mkdocs serve --dev-addr 8008 -``` - -/// - -#### Typer CLI (Optional) - -The instructions here show you how to use the script at `./scripts/docs.py` with the `python` program directly. - -But you can also use Typer CLI, and you will get autocompletion in your terminal for the commands after installing completion. - -If you install Typer CLI, you can install completion with: - -
- -```console -$ typer --install-completion - -zsh completion installed in /home/user/.bashrc. -Completion will take effect once you restart the terminal. -``` - -
- -### Docs Structure - -The documentation uses MkDocs. - -And there are extra tools/scripts in place in `./scripts/docs.py`. - -/// tip - -You don't need to see the code in `./scripts/docs.py`, you just use it in the command line. - -/// - -All the documentation is in Markdown format in the directory `./docs`. - -Many of the tutorials have blocks of code. - -In most of the cases, these blocks of code are actual complete applications that can be run as is. - -In fact, those blocks of code are not written inside the Markdown, they are Python files in the `./docs_src/` directory. - -And those Python files are included/injected in the documentation when generating the site. - -### Docs for Tests - -Most of the tests actually run against the example source files in the documentation. - -This helps to make sure that: - -* The documentation is up-to-date. -* The documentation examples can be run as is. -* Most of the features are covered by the documentation, ensured by test coverage. diff --git a/docs/css/custom.css b/docs/css/custom.css deleted file mode 100644 index 200ac45cd6..0000000000 --- a/docs/css/custom.css +++ /dev/null @@ -1,71 +0,0 @@ -.termynal-comment { - color: #4a968f; - font-style: italic; - display: block; -} - -.termy [data-termynal] { - white-space: pre-wrap; -} - -.termy .linenos { - display: none; -} - -a.external-link::after { - /* \00A0 is a non-breaking space - to make the mark be on the same line as the link - */ - content: "\00A0[β†ͺ]"; -} - -a.internal-link::after { - /* \00A0 is a non-breaking space - to make the mark be on the same line as the link - */ - content: "\00A0β†ͺ"; -} - -.shadow { - box-shadow: 5px 5px 10px #999; -} - -.user-list { - display: flex; - flex-wrap: wrap; - margin-bottom: 2rem; -} - -.user-list-center { - justify-content: space-evenly; -} - -.user { - margin: 1em; - min-width: 7em; -} - -.user .avatar-wrapper { - width: 80px; - height: 80px; - margin: 10px auto; - overflow: hidden; - border-radius: 50%; - position: relative; -} - -.user .avatar-wrapper img { - position: absolute; - top: 50%; - left: 50%; - transform: translate(-50%, -50%); -} - -.user .title { - text-align: center; -} - -.user .count { - font-size: 80%; - text-align: center; -} diff --git a/docs/css/termynal.css b/docs/css/termynal.css deleted file mode 100644 index 8534f91021..0000000000 --- a/docs/css/termynal.css +++ /dev/null @@ -1,111 +0,0 @@ -/** - * termynal.js - * - * @author Ines Montani - * @version 0.0.1 - * @license MIT - */ - -:root { - --color-bg: #252a33; - --color-text: #eee; - --color-text-subtle: #a2a2a2; -} - -[data-termynal] { - width: 750px; - max-width: 100%; - background: var(--color-bg); - color: var(--color-text); - /* font-size: 18px; */ - font-size: 15px; - /* font-family: 'Fira Mono', Consolas, Menlo, Monaco, 'Courier New', Courier, monospace; */ - font-family: 'Roboto Mono', 'Fira Mono', Consolas, Menlo, Monaco, 'Courier New', Courier, monospace; - border-radius: 4px; - padding: 75px 45px 35px; - position: relative; - -webkit-box-sizing: border-box; - box-sizing: border-box; - /* Custom line-height */ - line-height: 1.2; -} - -[data-termynal]:before { - content: ''; - position: absolute; - top: 15px; - left: 15px; - display: inline-block; - width: 15px; - height: 15px; - border-radius: 50%; - /* A little hack to display the window buttons in one pseudo element. */ - background: #d9515d; - -webkit-box-shadow: 25px 0 0 #f4c025, 50px 0 0 #3ec930; - box-shadow: 25px 0 0 #f4c025, 50px 0 0 #3ec930; -} - -[data-termynal]:after { - content: 'bash'; - position: absolute; - color: var(--color-text-subtle); - top: 5px; - left: 0; - width: 100%; - text-align: center; -} - -a[data-terminal-control] { - text-align: right; - display: block; - color: #aebbff; -} - -[data-ty] { - display: block; - line-height: 2; -} - -[data-ty]:before { - /* Set up defaults and ensure empty lines are displayed. */ - content: ''; - display: inline-block; - vertical-align: middle; -} - -[data-ty="input"]:before, -[data-ty-prompt]:before { - margin-right: 0.75em; - color: var(--color-text-subtle); -} - -[data-ty="input"]:before { - content: '$'; -} - -[data-ty][data-ty-prompt]:before { - content: attr(data-ty-prompt); -} - -[data-ty-cursor]:after { - content: attr(data-ty-cursor); - font-family: monospace; - margin-left: 0.5em; - -webkit-animation: blink 1s infinite; - animation: blink 1s infinite; -} - - -/* Cursor animation */ - -@-webkit-keyframes blink { - 50% { - opacity: 0; - } -} - -@keyframes blink { - 50% { - opacity: 0; - } -} diff --git a/docs/databases.md b/docs/databases.md deleted file mode 100644 index de25ebb4cb..0000000000 --- a/docs/databases.md +++ /dev/null @@ -1,332 +0,0 @@ -# Intro to Databases - -/// info - -Are you a seasoned developer and already know everything about databases? πŸ€“ - -Then you can skip to the next sections right away. - -/// - -If you don't know everything about databases, here's a quick overview. - -You can always study much more on your own later. - -But this should help you start using databases and being productive with **SQLModel**. πŸš€ - -## What is a Database - -So, what is a database? - -A **database** is a system to store and manage data in a structured and very efficient way. - -/// tip - -It's very common to abbreviate the word "database" as **"DB"**. - -/// - -As there's a lot of information about databases, and it can get very technical and academic, I'll give you a quick overview about some of the main concepts here. - -I'll even tell you a bit about different types of databases, including the ones not covered by SQLModel ("NoSQL" databases). - -## Why Use a Database - -When starting to program, it might **not be obvious** why having a database apart from the code for your program is a **good idea**. Let's start with that. - -/// tip - -If that's obvious to you, just continue in the next section below. πŸ‘‡ - -/// - -In your code you already have **variables**, **dictionaries**, **lists**, etc. They all store **data** in some way already. Why would you need to have a separate database? - -If you look closely, your code is **static**, it doesn't really change over time *once you run it*. Of course, you change the code frequently, adding features, etc, but once you start Python running your code, the program stays as it was when you started it. And if you change the code, the program will only change **once you run it again**. - -And even if you change things in variables, once the program terminates, all that data that was in **memory** is **gone**. πŸ”₯ - -In most of the cases, the objective of your program is to do something with data *outside* of the program. - -* It could be just moving **files** from one place to the other. -* Or it could be taking data from the user in the **terminal** and showing it differently. -* Or a **web API** that takes some data and process it in some way, etc. - -In most cases, the data *comes from outside* the program or *ends outside the program* (for example, shown on the screen, in a file, etc). - -In many cases, you need your program to be able to **create** and store data, **read** it, **update** it, **delete** it, etc. - -You could do all that by reading and writing to files from your code. And that works in simple cases. But for most complex systems with data that is a bit more **complex** that strategy is not very efficient. And you would have to deal with a lot of **caveats**, keeping the data in sync, making sure it is safely stored, etc. - -Databases are designed to **solve these problems**, making the process of handling data much more efficient, and independent of your code. ✨ - -## How to Interact with a Database - -There are many databases of many types. - -### A single file database - -A database could be a single file called `heroes.db`, managed with code in a very efficient way. An example would be SQLite, more about that in a bit. - -![database as a single file](img/databases/single-file.drawio.svg) - -### A server database - -A database could also be a system running as an application on a server, handling multiple files internally in optimized formats. - -Like a web server, but communicating in a custom and very efficient way. That is the most common type of database interaction. - -In this case, your code would talk to this server application instead of reading or modifying files directly. - -The database could be located in a different server/machine: - -![database in an external server](img/databases/external-server.drawio.svg) - -Or the database could be located in the same server/machine: - -![database in the same server](img/databases/same-server.drawio.svg) - -The most important aspect of these types of databases is that **your code doesn't read or modify** the files containing the data directly. - -Instead, your code communicates with the database application and that database application is the one that actually reads and modifies its data files. This is because this database application is normally **much more efficient** than what your code could be. - -Some examples of databases that work like this could be **PostgreSQL**, **MySQL**, or **MongoDB**. - -### Distributed servers - -In some cases, the database could even be a group of server applications running on different machines, working together and communicating between them to be more efficient and handle more data. - -In this case, your code would talk to one or more of these server applications running on different machines. - -![distributed database in multiple servers](img/databases/multiple-servers.drawio.svg) - -Most of the databases that work as server applications also support multiple servers in one way or another. - -Having distributed systems also creates additional challenges, so there's a high chance that you would first interact with a single server application or one based on a single file. - -## SQL Databases - -We already talked about the different ways to interact with a database and how they handle files, etc. That applies to most or all of the databases. - -But there's another way to categorize databases that is very important. As you can imagine, there are many types of databases and many databases in each group. But in general, they can be separated in two big groups: "SQL Databases" and "NoSQL Databases". - -We will get to why the name "SQL" in a bit, but first, let's see what is it all about. - -### SQLModel for SQL Databases - -**SQLModel** is a tool to help you with **SQL Databases**. - -It cannot help you much with **NoSQL Databases**. Nevertheless, I'll explain a bit about them here. - -## Invent SQL Databases - -A long time ago, some smart people realized that a great way to store data was putting it in different tables. - -And by "table" I mean just data in a grid, with different columns and rows, pretty much like a single spreadsheet. - -Each row would represent a specific item or **record**. And each column would represent a specific attribute or field of that record. - -### An example of a big table - -Let's imagine that we need to store some data about heroes. - -If we worked with a single table to store our heroes, it could be like this: - - - - - - - - - - - - - - -
idnamesecret_nameageteamheadquarters
1DeadpondDive WilsonnullZ-FactorSister Margaret's Bar
2Spider-BoyPedro ParqueadornullPreventersSharp Tower
3Rusty-ManTommy Sharp48PreventersSharp Tower
- -That's probably what we would have to do with a single table, for example, with a single spreadsheet. - -But there are some problems with this. Let's check some. - -#### Single table problems - -Imagine that they decided to rename the "Sharp Tower" to "Preventers Tower". - -Now we would have to update that in two places. - -What happens if our code starts to update that name in one place and suddenly there's a power outage and the computer goes off? - -We could end up with inconsistent information, having one place saying "Preventers Tower" and the other one saying "Sharp Tower": - - - - - - - - - - - - - - -
idnamesecret_nameageteamheadquarters
1DeadpondDive WilsonnullZ-ForceSister Margaret's Bar
2Spider-BoyPedro ParqueadornullPreventersPreventers Tower βœ…
3Rusty-ManTommy Sharp48PreventersSharp Tower 🚨
- -And now imagine that we need to add a new hero called "Mahjong" that is part of the team "Z-Force". - -We could forget the name of the team and end up adding "Mahjong" with an invalid team name, for example "Y-Force". - - - - - - - - - - - - - - - - - -
idnamesecret_nameageteamheadquarters
1DeadpondDive WilsonnullZ-ForceSister Margaret's Bar
2Spider-BoyPedro ParqueadornullPreventersPreventers Tower
3Rusty-ManTommy Sharp48PreventersSharp Tower
4MahjongNeena Thurgirl31Y-Force 🚨Sister Margaret's Bar
- -And what if a single hero belongs to two teams? We wouldn't have an easy way to put this into a single big table. - -### Multiple tables - -But these and other problems could be solved better by having the data in multiple tables. - -So, instead of having a single table with all the data, we could have one table for the heroes and one for teams, and a way to connect one with the other. - -The table for the teams could look like this: - - - - - - - - - - - -
idnameheadquarters
1PreventersSharp Tower
2Z-ForceSister Margaret's Bar
- -Now, the table for the heroes would look almost the same. But remember that we mentioned that we need a way to connect the two tables? - -The table for the heroes would now have another column `team_id`. This column shows the relationship from each row (from each hero) to the team they belong to. - - - - - - - - - - - - - - -
idnamesecret_nameageteam_id ✨
1DeadpondDive Wilsonnull2 ✨
2Spider-BoyPedro Parqueadornull1 ✨
3Rusty-ManTommy Sharp481 ✨
- -#### Identifications - Primary Key - -In the example above, each one of the rows has an `id`. Each ID is unique per table and identifies that particular row. - -These SQL databases require having a unique way to identify each row in a table. It could be a combination of columns that is unique, but commonly it is just one single column. This is called the "**primary key**" of the table. - -The **primary key** is frequently a single column, commonly it's just an integer generated automatically by the database, and in many cases, the column is simply called `id`. - -This **primary key**, in this case the column `id`, has to be unique per table. But two different tables could have the same ID. For example, above, both tables have the ID `2` for two different rows, one for "**Z-Force**" in one table and one for "**Spider-Boy**" in the other table, but that's still okay as long as there's a single one per table. - -#### Relationships - Foreign Key - -Each row in a table has a single **primary key** (in our example a single column `id`). - -For example, the table for the teams has the ID `1` for the team `Preventers` and the ID `2` for the team `Z-Force`. - -As these **primary key** IDs can uniquely identify each row on the table for teams, we can now go to the table for heroes and refer to those IDs in the table for teams. - -![table relationships](img/databases/relationships.drawio.svg) - -So, in the table for heroes, we use the `team_id` column to define a relationship to the *foreign* table for teams. Each value in the `team_id` column on the table with heroes will be the same value as the `id` column of one row in the table with teams. - -In the table for heroes we have a **primary key** that is the `id`. But we also have another column `team_id` that refers to a **key** in a **foreign** table. There's a technical term for that too, the `team_id` is a "**foreign key**". - -### Relations and Relational Databases - -The technical and academic term for each one of these tables is a "**relation**". - -You might hear that term a lot when talking about these databases. - -It doesn't have the meaning that you would use in English of something being related to something else, even though each of these tables is actually "related" to the others. - -The technical term **relation** just refers to each one of these tables. - -And because of this technical term, these **SQL Databases** are also called **Relational Databases** (in fact, that is the technically correct term). But it still just refers to these databases made with multiple tables. - -### SQL - The Language - -After developing these ideas of how to store data in multiple tables they also created a **language** that could be used to interact with them. - -The language is called **SQL**, the name comes from for **Structured Query Language**. - -Nevertheless, the language is not only used to *query* for data. It is also used to create records/rows, to update them, to delete them. And to manipulate the database, create tables, etc. - -This language is supported by all these databases that handle multiple tables, that's why they are called **SQL Databases**. Although, each database has small variations in the SQL language they support (*dialect*). - -Let's imagine that the table holding the heroes is called the `hero` table. An example of a SQL query to get all the data from it could look like: - -```SQL -SELECT * -FROM hero; -``` - -And that SQL query would return the table: - - - - - - - - - - - - - - -
idnamesecret_nameageteam_id
1DeadpondDive Wilsonnull2
2Spider-BoyPedro Parqueadornull1
3Rusty-ManTommy Sharp481
- -### SQLModel for SQL - -**SQLModel** is a library that helps you write Python code with regular Python objects, and then it transfers that to **SQL** statements that it sends to a **SQL Database**. - -Next, it receives the data and puts it in Python objects that you can continue to use in your code. - -I'll tell you more about SQL, SQLModel, how to use them, and how they are related in the next sections. - -/// info | Technical Details - -SQLModel is built on top of SQLAlchemy. It is, in fact, just SQLAlchemy and Pydantic mixed together with some sugar on top. - -/// - -## NoSQL Databases - -Although SQL Databases are the oldest and most commonly used type of database, there's another (very interesting) category, the one of **NoSQL Databases**. - -**NoSQL Databases** covers a wide range of different sub-types, including key-value stores, document stores, graph databases, and more. - -**SQLModel** can only help you with SQL Databases. So, that's what we'll talk about in the rest of the documentation. diff --git a/docs/db-to-code.md b/docs/db-to-code.md deleted file mode 100644 index fbba54c162..0000000000 --- a/docs/db-to-code.md +++ /dev/null @@ -1,319 +0,0 @@ -# Database to Code (ORMs) - -Here I'll tell you how **SQLModel** interacts with the database, why you would want to use it (or use a similar tool), and how it relates to SQL. - -## SQL Inline in Code - -Let's check this example of a simple SQL query to get all the data from the `hero` table: - -```SQL -SELECT * -FROM hero; -``` - -And that SQL query would return the table: - - - - - - - - - - - - - - -
idnamesecret_nameageteam_id
1DeadpondDive Wilsonnull2
2Spider-BoyPedro Parqueadornull1
3Rusty-ManTommy Sharp481
- -This SQL language has a little **caveat**. It was not designed to be mixed with normal code in a programming language like Python. 🚨 - -So, if you are working with Python, the simplest option would be to put SQL code inside a string, and send that string directly to the database. - -```Python -statement = "SELECT * FROM hero;" - -results = database.execute(statement) -``` - -But in that case, you wouldn't have editor support, inline errors, autocompletion, etc. Because for the editor, the SQL statement is just a string of text. If you have an error, the editor wouldn't be able to help. πŸ˜” - -And even more importantly, in most of the cases, you would send the SQL strings with modifications and parameters. For example, to get the data for a *specific item ID*, a *range of dates*, etc. - -And in most cases, the parameters your code uses to query or modify the data in the database come, in some way, from an external user. - -For example, check this SQL query: - -```SQL -SELECT * -FROM hero -WHERE id = 2; -``` - -It is using the ID parameter `2`. That number `2` probably comes, in some way, from a user input. - -The user is probably, in some way, telling your application: - -> Hey, I want to get the hero with ID: - -```SQL -2 -``` - -And the result would be this table (with a single row): - - - - - - - - -
idnamesecret_nameageteam_id
2Spider-BoyPedro Parqueadornull1
- -### SQL Injection - -But let's say that your code takes whatever the external user provides and puts it inside the SQL string before sending it to the database. Something like this: - -```Python -# Never do this! 🚨 Continue reading. - -user_id = input("Type the user ID: ") - -statement = f"SELECT * FROM hero WHERE id = {user_id};" - -results = database.execute(statement) -``` - -If the external user is actually an attacker, they could send you a malicious SQL string that does something terrible like deleting all the records. That's called a "**SQL Injection**". - -For example, imagine that this new attacker user says: - -> Hey, I want to get the hero with ID: - -```SQL -2; DROP TABLE hero -``` - -Then the code above that takes the user input and puts it in SQL would actually send this to the database: - -```SQL -SELECT * FROM hero WHERE id = 2; DROP TABLE hero; -``` - -Check that section added at the end. That's another entire SQL statement: - -```SQL -DROP TABLE hero; -``` - -That is how you tell the database in SQL to delete the entire table `hero`. - -Nooooo! We lost all the data in the `hero` table! πŸ’₯😱 - -### SQL Sanitization - -The process of making sure that whatever the external user sends is safe to use in the SQL string is called **sanitization**. - -It comes by default in **SQLModel** (thanks to SQLAlchemy). And many other similar tools would also provide that functionality among many other features. - -Now you are ready for a joke from xkcd: - -![Exploits of a Mom](https://imgs.xkcd.com/comics/exploits_of_a_mom.png) - -## SQL with SQLModel - -With **SQLModel**, instead of writing SQL statements directly, you use Python classes and objects to interact with the database. - -For example, you could ask the database for the same hero with ID `2` with this code: - -```Python -user_id = input("Type the user ID: ") - -session.exec( - select(Hero).where(Hero.id == user_id) -).all() -``` - -If the user provides this ID: - -```SQL -2 -``` - -...the result would be this table (with a single row): - - - - - - - - -
idnamesecret_nameageteam_id
2Spider-BoyPedro Parqueadornull1
- -### Preventing SQL Injections - -If the user is an attacker and tries to send this as the "ID": - -```SQL -2; DROP TABLE hero -``` - -Then **SQLModel** will convert that to a literal string `"2; DROP TABLE hero"`. - -And then, it will tell the SQL Database to try to find a record with that exact ID instead of injecting the attack. - -The difference in the final SQL statement is subtle, but it changes the meaning completely: - -```SQL -SELECT * FROM hero WHERE id = "2; DROP TABLE hero;"; -``` - -/// tip - -Notice the double quotes (`"`) making it a string instead of more raw SQL. - -/// - -The database will not find any record with that ID: - -```SQL -"2; DROP TABLE hero;" -``` - -Then the database will send an empty table as the result because it didn't find any record with that ID. - -Then your code will continue to execute and calmly tell the user that it couldn't find anything. - -But we never deleted the `hero` table. πŸŽ‰ - -/// info - -Of course, there are also other ways to do SQL data sanitization without using a tool like **SQLModel**, but it's still a nice feature you get by default. - -/// - -### Editor Support - -Check that Python snippet above again. - -Because we are using **standard Python classes and objects**, your editor will be able to provide you with autocompletion, inline errors, etc. - -For example, let's say you wanted to query the database to find a hero based on the secret identity. - -Maybe you don't remember how you named the column. Maybe it was: - -* `secret_identity`? - -...or was it: - -* `secretidentity`? - -...or: - -* `private_name`? -* `secret_name`? -* `secretname`? - -If you type that in SQL strings in your code, your editor **won't be able to help you**: - -```SQL -statement = "SELECT * FROM hero WHERE secret_identity = 'Dive Wilson';" - -results = database.execute(statement) -``` - -...your editor will see that as a **long string** with some text inside, and it will **not be able to autocomplete** or detect the error in `secret_identity`. - -But if you use common Python classes and objects, your editor will be able to help you: - -```Python -database.execute( - select(Hero).where(Hero.secret_name == "Dive Wilson") -).all() -``` - -![](img/db-to-code/autocompletion01.png){class="shadow"} - -## ORMs and SQL - -These types of libraries like **SQLModel** (and of course, SQLAlchemy) that translate between SQL and code with classes and objects are called **ORMs**. - -**ORM** means **Object-Relational Mapper**. - -This is a very common term, but it also comes from quite technical and **academical** concepts πŸ‘©β€πŸŽ“: - -* **Object**: refers to code with classes and instances, normally called "Object Oriented Programming", that's why the "**Object**" part. - -For example this class is part of that **Object** Oriented Programming: - -```Python -class Hero(SQLModel): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None -``` - -* **Relational**: refers to the **SQL Databases**. Remember that they are also called **Relational Databases**, because each of those tables is also called a "**relation**"? That's where the "**Relational**" comes from. - -For example this **Relation** or table: - - - - - - - - - - - - - - -
idnamesecret_nameageteam_id
1DeadpondDive Wilsonnull2
2Spider-BoyPedro Parqueadornull1
3Rusty-ManTommy Sharp481
- -* **Mapper**: this comes from Math, when there's something that can convert from some set of things to another, that's called a "**mapping function**". That's where the **Mapper** comes from. - -![Squares to Triangles Mapper](img/db-to-code/mapper.drawio.svg) - -We could also write a **mapping function** in Python that converts from the *set of lowercase letters* to the *set of uppercase letters*, like this: - -```Python -def map_lower_to_upper(value: str): - return value.upper() -``` - -It's actually a simple idea with a very academic and mathematical name. πŸ˜… - -So, an **ORM** is a library that translates from SQL to code, and from code to SQL. All using classes and objects. - -There are many ORMs available apart from **SQLModel**, you can read more about some of them in [Alternatives, Inspiration and Comparisons](alternatives.md){.internal-link target=_blank} - -## SQL Table Names - -/// info | Technical Background - -This is a bit of boring background for SQL purists. Feel free to skip this section. πŸ˜‰ - -/// - -When working with pure SQL, it's common to name the tables in plural. So, the table would be named `heroes` instead of `hero`, because it could contain multiple rows, each with one hero. - -Nevertheless, **SQLModel** and many other similar tools can generate a table name automatically from your code, as you will see later in the tutorial. - -But this name will be derived from a class name. And it's common practice to use **singular** names for classes (e.g. `class Hero`, instead of `class Heroes`). Using singular names for classes like `class Hero` also makes your code more intuitive. - -You will see **your own code** a lot more than the internal table names, so it's probably better to keep the code/class convention than the SQL convention. - -So, to keep things consistent, I'll keep using the same table names that **SQLModel** would have generated. - -/// tip - -You can also override the table name. You can read about it in the Advanced User Guide. - -/// diff --git a/docs/environment-variables.md b/docs/environment-variables.md deleted file mode 100644 index bd8b2a06d0..0000000000 --- a/docs/environment-variables.md +++ /dev/null @@ -1,300 +0,0 @@ -# Environment Variables - -Before we jump into code, let's cover a bit some of the **basics** that we'll need to understand how to work with Python (and programming) in general. Let's check a bit about **environment variables**. - -/// tip - -If you already know what "environment variables" are and how to use them, feel free to skip this. - -/// - -An environment variable (also known as "**env var**") is a variable that lives **outside** of the Python code, in the **operating system**, and could be read by your Python code (or by other programs as well). - -Environment variables could be useful for handling application **settings**, as part of the **installation** of Python, etc. - -## Create and Use Env Vars - -You can **create** and use environment variables in the **shell (terminal)**, without needing Python: - -//// tab | Linux, macOS, Windows Bash - -
- -```console -// You could create an env var MY_NAME with -$ export MY_NAME="Wade Wilson" - -// Then you could use it with other programs, like -$ echo "Hello $MY_NAME" - -Hello Wade Wilson -``` - -
- -//// - -//// tab | Windows PowerShell - -
- -```console -// Create an env var MY_NAME -$ $Env:MY_NAME = "Wade Wilson" - -// Use it with other programs, like -$ echo "Hello $Env:MY_NAME" - -Hello Wade Wilson -``` - -
- -//// - -## Read env vars in Python - -You could also create environment variables **outside** of Python, in the terminal (or with any other method), and then **read them in Python**. - -For example you could have a file `main.py` with: - -```Python hl_lines="3" -import os - -name = os.getenv("MY_NAME", "World") -print(f"Hello {name} from Python") -``` - -/// tip - -The second argument to `os.getenv()` is the default value to return. - -If not provided, it's `None` by default, here we provide `"World"` as the default value to use. - -/// - -Then you could call that Python program: - -//// tab | Linux, macOS, Windows Bash - -
- -```console -// Here we don't set the env var yet -$ python main.py - -// As we didn't set the env var, we get the default value - -Hello World from Python - -// But if we create an environment variable first -$ export MY_NAME="Wade Wilson" - -// And then call the program again -$ python main.py - -// Now it can read the environment variable - -Hello Wade Wilson from Python -``` - -
- -//// - -//// tab | Windows PowerShell - -
- -```console -// Here we don't set the env var yet -$ python main.py - -// As we didn't set the env var, we get the default value - -Hello World from Python - -// But if we create an environment variable first -$ $Env:MY_NAME = "Wade Wilson" - -// And then call the program again -$ python main.py - -// Now it can read the environment variable - -Hello Wade Wilson from Python -``` - -
- -//// - -As environment variables can be set outside of the code, but can be read by the code, and don't have to be stored (committed to `git`) with the rest of the files, it's common to use them for configurations or **settings**. - -You can also create an environment variable only for a **specific program invocation**, that is only available to that program, and only for its duration. - -To do that, create it right before the program itself, on the same line: - -
- -```console -// Create an env var MY_NAME in line for this program call -$ MY_NAME="Wade Wilson" python main.py - -// Now it can read the environment variable - -Hello Wade Wilson from Python - -// The env var no longer exists afterwards -$ python main.py - -Hello World from Python -``` - -
- -/// tip - -You can read more about it at The Twelve-Factor App: Config. - -/// - -## Types and Validation - -These environment variables can only handle **text strings**, as they are external to Python and have to be compatible with other programs and the rest of the system (and even with different operating systems, as Linux, Windows, macOS). - -That means that **any value** read in Python from an environment variable **will be a `str`**, and any conversion to a different type or any validation has to be done in code. - -## `PATH` Environment Variable - -There is a **special** environment variable called **`PATH`** that is used by the operating systems (Linux, macOS, Windows) to find programs to run. - -The value of the variable `PATH` is a long string that is made of directories separated by a colon `:` on Linux and macOS, and by a semicolon `;` on Windows. - -For example, the `PATH` environment variable could look like this: - -//// tab | Linux, macOS - -```plaintext -/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin -``` - -This means that the system should look for programs in the directories: - -* `/usr/local/bin` -* `/usr/bin` -* `/bin` -* `/usr/sbin` -* `/sbin` - -//// - -//// tab | Windows - -```plaintext -C:\Program Files\Python312\Scripts;C:\Program Files\Python312;C:\Windows\System32 -``` - -This means that the system should look for programs in the directories: - -* `C:\Program Files\Python312\Scripts` -* `C:\Program Files\Python312` -* `C:\Windows\System32` - -//// - -When you type a **command** in the terminal, the operating system **looks for** the program in **each of those directories** listed in the `PATH` environment variable. - -For example, when you type `python` in the terminal, the operating system looks for a program called `python` in the **first directory** in that list. - -If it finds it, then it will **use it**. Otherwise it keeps looking in the **other directories**. - -### Installing Python and Updating the `PATH` - -When you install Python, you might be asked if you want to update the `PATH` environment variable. - -//// tab | Linux, macOS - -Let's say you install Python and it ends up in a directory `/opt/custompython/bin`. - -If you say yes to update the `PATH` environment variable, then the installer will add `/opt/custompython/bin` to the `PATH` environment variable. - -It could look like this: - -```plaintext -/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/custompython/bin -``` - -This way, when you type `python` in the terminal, the system will find the Python program in `/opt/custompython/bin` (the last directory) and use that one. - -//// - -//// tab | Windows - -Let's say you install Python and it ends up in a directory `C:\opt\custompython\bin`. - -If you say yes to update the `PATH` environment variable, then the installer will add `C:\opt\custompython\bin` to the `PATH` environment variable. - -```plaintext -C:\Program Files\Python312\Scripts;C:\Program Files\Python312;C:\Windows\System32;C:\opt\custompython\bin -``` - -This way, when you type `python` in the terminal, the system will find the Python program in `C:\opt\custompython\bin` (the last directory) and use that one. - -//// - -This way, when you type `python` in the terminal, the system will find the Python program in `/opt/custompython/bin` (the last directory) and use that one. - -So, if you type: - -
- -```console -$ python -``` - -
- -//// tab | Linux, macOS - -The system will **find** the `python` program in `/opt/custompython/bin` and run it. - -It would be roughly equivalent to typing: - -
- -```console -$ /opt/custompython/bin/python -``` - -
- -//// - -//// tab | Windows - -The system will **find** the `python` program in `C:\opt\custompython\bin\python` and run it. - -It would be roughly equivalent to typing: - -
- -```console -$ C:\opt\custompython\bin\python -``` - -
- -//// - -This information will be useful when learning about [Virtual Environments](virtual-environments.md){.internal-link target=_blank}. - -## Conclusion - -With this you should have a basic understanding of what **environment variables** are and how to use them in Python. - -You can also read more about them in the Wikipedia for Environment Variable. - -In many cases it's not very obvious how environment variables would be useful and applicable right away. But they keep showing up in many different scenarios when you are developing, so it's good to know about them. - -For example, you will need this information in the next section, about [Virtual Environments](virtual-environments.md). diff --git a/docs/features.md b/docs/features.md deleted file mode 100644 index f0d56925c7..0000000000 --- a/docs/features.md +++ /dev/null @@ -1,92 +0,0 @@ -# Features - -## Designed for **FastAPI** - -**SQLModel** was created by the same author of FastAPI. - - - -It follows the same design and ideas, and it was created to be the most intuitive way to interact with SQL databases in FastAPI applications. - -Nevertheless, SQLModel is completely **independent** of FastAPI and can be used with any other type of application. You can still benefit from its features. - -## Just 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. - -You will also see a 20 seconds refresher on the section [Tutorial - User Guide: First Steps](tutorial/index.md){.internal-link target=_blank}. - -## Editor support - -**SQLModel** was designed to be easy and intuitive to use to ensure the best development experience, with autocompletion everywhere. - -Here's how your editor might help you: - -* in Visual Studio Code: - - - -* in PyCharm: - - - -You will get completion for everything while writing the **minimum** amount of code. - -You won't need to keep guessing the types of different attributes in your models, if they could be `None`, etc. Your editor will be able to help you with everything because **SQLModel** is based on **standard Python type annotations**. - -**SQLModel** adopts PEP 681 for Python type annotations to ensure the **best developer experience**, so you will get inline errors and autocompletion even while creating new model instances. - - - -## Short - -**SQLModel** has **sensible defaults** for everything, with **optional configurations** everywhere. - -But by default, it all **"just works"**. - -You can start with the simplest (and most intuitive) type annotations for your data. - -And later, you can fine-tune everything with all the power of SQLAlchemy and Pydantic. - -## Based on Pydantic - -**SQLModel** is based on Pydantic and keeps the same design, syntax, and ideas. - -Underneath, ✨ a **SQLModel** model is also a **Pydantic** model. ✨ - -There was a lot of research and effort dedicated to make it that way. - -That means you get all of **Pydantic's features**, including automatic data **validation**, **serialization**, and **documentation**. You can use SQLModel in the same way you can use Pydantic. - -You can even create SQLModel models that do *not* represent SQL tables. In that case, they would be **the same as Pydantic models**. - -This is useful, in particular, because now you can create a SQL database model that *inherits* from another non-SQL model. You can use that to **reduce code duplication** a lot. It will also make your code more consistent, improve editor support, etc. - -This makes it the perfect combination for working with SQL databases in **FastAPI** applications. πŸš€ - -You will learn more about combining different models later in the tutorial. - -## Based on SQLAlchemy - -**SQLModel** is also based on SQLAlchemy and uses it for everything. - -Underneath, ✨ a **SQLModel** model is also a **SQLAlchemy** model. ✨ - -There was **a lot** of research and effort dedicated to make it that way. In particular, there was a lot of effort and experimentation in making a single model be **both a SQLAlchemy model and a Pydantic** model at the same time. - -That means that you get all the power, robustness, and certainty of SQLAlchemy, the most widely used database library in Python. - -**SQLModel** provides its own utilities to improve the developer experience, but underneath, it uses all of SQLAlchemy. - -You can even **combine** SQLModel models with SQLAlchemy models. - -SQLModel is designed to satisfy the **most common use cases** and to be as simple and convenient as possible for those cases, providing the best developer experience. - -But when you have more exotic use cases that require more complex features, you can still plug SQLAlchemy directly into SQLModel and use all its features in your code. - -## Tested - -* 100% test coverage (currently 97%, reaching 100% in the coming days/weeks). -* 100% type annotated code base. diff --git a/docs/help.md b/docs/help.md deleted file mode 100644 index 6e5fe581f7..0000000000 --- a/docs/help.md +++ /dev/null @@ -1,250 +0,0 @@ -# Help SQLModel - Get Help - -Do you like **SQLModel**? - -Would you like to help SQLModel, other users, and the author? - -Or would you like to get help with **SQLModel**? - -There are very simple ways to help (several involve just one or two clicks). - -And there are several ways to get help too. - -## Subscribe to the FastAPI and Friends newsletter - -You can subscribe to the (infrequent) **FastAPI and friends** newsletter to stay updated about: - -* News about FastAPI and friends, including SQLModel πŸš€ -* Guides πŸ“ -* Features ✨ -* Breaking changes 🚨 -* Tips and tricks βœ… - -## Star **SQLModel** in GitHub - -You can "star" SQLModel in GitHub (clicking the star button at the top right): https://github.com/fastapi/sqlmodel. ⭐️ - -By adding a star, other users will be able to find it more easily and see that it has been already useful for others. - -## Watch the GitHub repository for releases - -You can "watch" SQLModel in GitHub (clicking the "watch" button at the top right): https://github.com/fastapi/sqlmodel. πŸ‘€ - -There you can select "Releases only". - -By doing it, you will receive notifications (in your email) whenever there's a new release (a new version) of **SQLModel** with bug fixes and new features. - -## Connect with the author - -You can connect with me (SebastiΓ‘n RamΓ­rez / `tiangolo`), the author. - -You can: - -* Follow me on **GitHub**. - * See other Open Source projects I have created that could help you. - * Follow me to see when I create a new Open Source project. -* Follow me on **Twitter**. - * Tell me how you use SQLModel (I love to hear that). - * Hear when I make announcements or release new tools. -* Connect with me on **Linkedin**. - * Hear when I make announcements or release new tools (although I use Twitter more often πŸ€·β€β™‚). -* Read what I write (or follow me) on **Dev.to** or **Medium**. - * Read other ideas, articles, and read about tools I have created. - * Follow me to read when I publish something new. - -## Tweet about **SQLModel** - -Tweet about **SQLModel** and let me and others know why you like it. πŸŽ‰ - -I love to hear about how **SQLModel** is being used, what you have liked in it, in which project/company are you using it, etc. - -## Help others with questions in GitHub - -You can try and help others with their questions in: - -* GitHub Discussions -* GitHub Issues - -In many cases you might already know the answer for those questions. πŸ€“ - -Just remember, the most important point is: try to be kind. People come with their frustrations and in many cases don't ask in the best way, but try as best as you can to be kind. πŸ€— - -The idea is for the **SQLModel** community to be kind and welcoming. At the same time, don't accept bullying or disrespectful behavior towards others. We have to take care of each other. - ---- - -Here's how to help others with questions (in discussions or issues): - -### Understand the question - -* Check if you can understand what is the **purpose** and use case of the person asking. - -* Then check if the question (the vast majority are questions) is **clear**. - -* In many cases the question asked is about an imaginary solution from the user, but there might be a **better** one. If you can understand the problem and use case better, you might be able to suggest a better **alternative solution**. - -* If you can't understand the question, ask for more **details**. - -### Reproduce the problem - -For most of the cases and most of the questions there's something related to the person's **original code**. - -In many cases they will only copy a fragment of the code, but that's not enough to **reproduce the problem**. - -* You can ask them to provide a minimal, reproducible, example, that you can **copy-paste** and run locally to see the same error or behavior they are seeing, or to understand their use case better. - -* If you are feeling too generous, you can try to **create an example** like that yourself, just based on the description of the problem. Just have in mind that this might take a lot of time and it might be better to ask them to clarify the problem first. - -### Suggest solutions - -* After being able to understand the question, you can give them a possible **answer**. - -* In many cases, it's better to understand their **underlying problem or use case**, because there might be a better way to solve it than what they are trying to do. - -### Ask to close - -If they reply, there's a high chance you would have solved their problem, congrats, **you're a hero**! 🦸 - -* Now, if that solved their problem, you can ask them to: - - * In GitHub Discussions: mark the comment as the **answer**. - * In GitHub Issues: **close** the issue**. - -## Watch the GitHub repository - -You can "watch" SQLModel in GitHub (clicking the "watch" button at the top right): https://github.com/fastapi/sqlmodel. πŸ‘€ - -If you select "Watching" instead of "Releases only" you will receive notifications when someone creates a new issue or question. You can also specify that you only want to be notified about new issues, or discussions, or PRs, etc. - -Then you can try and help them solve those questions. - -## Ask Questions - -You can create a new question in the GitHub repository, for example to: - -* Ask a **question** or ask about a **problem**. -* Suggest a new **feature**. - -**Note**: if you do it, then I'm going to ask you to also help others. πŸ˜‰ - -## Review Pull Requests - -You can help me review pull requests from others. - -Again, please try your best to be kind. πŸ€— - ---- - -Here's what to have in mind and how to review a pull request: - -### Understand the problem - -* First, make sure you **understand the problem** that the pull request is trying to solve. It might have a longer discussion in a GitHub Discussion or issue. - -* There's also a good chance that the pull request is not actually needed because the problem can be solved in a **different way**. Then you can suggest or ask about that. - -### Don't worry about style - -* Don't worry too much about things like commit message styles, I will squash and merge customizing the commit manually. - -* Also don't worry about style rules, there are already automatized tools checking that. - -And if there's any other style or consistency need, I'll ask directly for that, or I'll add commits on top with the needed changes. - -### Check the code - -* Check and read the code, see if it makes sense, **run it locally** and see if it actually solves the problem. - -* Then **comment** saying that you did that, that's how I will know you really checked it. - -/// info - -Unfortunately, I can't simply trust PRs that just have several approvals. - -Several times it has happened that there are PRs with 3, 5 or more approvals, probably because the description is appealing, but when I check the PRs, they are actually broken, have a bug, or don't solve the problem they claim to solve. πŸ˜… - -So, it's really important that you actually read and run the code, and let me know in the comments that you did. πŸ€“ - -/// - -* If the PR can be simplified in a way, you can ask for that, but there's no need to be too picky, there might be a lot of subjective points of view (and I will have my own as well πŸ™ˆ), so it's better if you can focus on the fundamental things. - -### Tests - -* Help me check that the PR has **tests**. - -* Check that the tests **fail** before the PR. 🚨 - -* Then check that the tests **pass** after the PR. βœ… - -* Many PRs don't have tests, you can **remind** them to add tests, or you can even **suggest** some tests yourself. That's one of the things that consume most time and you can help a lot with that. - -* Then also comment what you tried, that way I'll know that you checked it. πŸ€“ - -## Create a Pull Request - -You can [contribute](contributing.md){.internal-link target=_blank} to the source code with Pull Requests, for example: - -* To fix a typo you found on the documentation. -* To propose new documentation sections. -* To fix an existing issue/bug. - * Make sure to add tests. -* To add a new feature. - * Make sure to add tests. - * Make sure to add documentation if it's relevant. - -## Help Maintain SQLModel - -Help me maintain **SQLModel**! πŸ€“ - -There's a lot of work to do, and for most of it, **YOU** can do it. - -The main tasks that you can do right now are: - -* [Help others with questions in GitHub](#help-others-with-questions-in-github){.internal-link target=_blank} (see the section above). -* [Review Pull Requests](#review-pull-requests){.internal-link target=_blank} (see the section above). - -Those two tasks are what **consume time the most**. That's the main work of maintaining SQLModel. - -If you can help me with that, **you are helping me maintain SQLModel** and making sure it keeps **advancing faster and better**. πŸš€ - -## Join the chat - -Join the πŸ‘₯ FastAPI and Friends Discord chat server πŸ‘₯ and hang out with others in the community. There's a `#sqlmodel` channel. - -/// tip - -For questions, ask them in GitHub Discussions, there's a much better chance you will receive help there. - -Use the chat only for other general conversations. - -/// - -### Don't use the chat for questions - -Have in mind that as chats allow more "free conversation", it's easy to ask questions that are too general and more difficult to answer, so, you might not receive answers. - -In GitHub, the template will guide you to write the right question so that you can more easily get a good answer, or even solve the problem yourself even before asking. And in GitHub I can make sure I always answer everything, even if it takes some time. I can't personally do that with the chat. πŸ˜… - -Conversations in the chat are also not as easily searchable as in GitHub, so questions and answers might get lost in the conversation. - -On the other side, there are thousands of users in the chat, so there's a high chance you'll find someone to talk to there, almost all the time. πŸ˜„ - -## Sponsor the author - -You can also financially support the author (me) through GitHub sponsors. - -There you could buy me a coffee β˜•οΈ to say thanks. πŸ˜„ - -## Sponsor the tools that power SQLModel - -As you have seen in the documentation, SQLModel stands on the shoulders of giants, Pydantic and SQLAlchemy. - -You can also sponsor: - -* Samuel Colvin (Pydantic) -* SQLAlchemy - ---- - -Thanks! πŸš€ diff --git a/docs/img/create-db-and-table-with-db-browser/image001.png b/docs/img/create-db-and-table-with-db-browser/image001.png deleted file mode 100644 index 89e8f96c4d..0000000000 Binary files a/docs/img/create-db-and-table-with-db-browser/image001.png and /dev/null differ diff --git a/docs/img/create-db-and-table-with-db-browser/image002.png b/docs/img/create-db-and-table-with-db-browser/image002.png deleted file mode 100644 index 0215129130..0000000000 Binary files a/docs/img/create-db-and-table-with-db-browser/image002.png and /dev/null differ diff --git a/docs/img/create-db-and-table-with-db-browser/image003.png b/docs/img/create-db-and-table-with-db-browser/image003.png deleted file mode 100644 index 97a88559e6..0000000000 Binary files a/docs/img/create-db-and-table-with-db-browser/image003.png and /dev/null differ diff --git a/docs/img/create-db-and-table-with-db-browser/image004.png b/docs/img/create-db-and-table-with-db-browser/image004.png deleted file mode 100644 index 582f82f91a..0000000000 Binary files a/docs/img/create-db-and-table-with-db-browser/image004.png and /dev/null differ diff --git a/docs/img/create-db-and-table-with-db-browser/image005.png b/docs/img/create-db-and-table-with-db-browser/image005.png deleted file mode 100644 index 517c37612d..0000000000 Binary files a/docs/img/create-db-and-table-with-db-browser/image005.png and /dev/null differ diff --git a/docs/img/create-db-and-table-with-db-browser/image006.png b/docs/img/create-db-and-table-with-db-browser/image006.png deleted file mode 100644 index 0e2c898f40..0000000000 Binary files a/docs/img/create-db-and-table-with-db-browser/image006.png and /dev/null differ diff --git a/docs/img/create-db-and-table-with-db-browser/image007.png b/docs/img/create-db-and-table-with-db-browser/image007.png deleted file mode 100644 index f26347969d..0000000000 Binary files a/docs/img/create-db-and-table-with-db-browser/image007.png and /dev/null differ diff --git a/docs/img/create-db-and-table-with-db-browser/image008.png b/docs/img/create-db-and-table-with-db-browser/image008.png deleted file mode 100644 index 91234327ca..0000000000 Binary files a/docs/img/create-db-and-table-with-db-browser/image008.png and /dev/null differ diff --git a/docs/img/create-db-and-table-with-db-browser/image009.png b/docs/img/create-db-and-table-with-db-browser/image009.png deleted file mode 100644 index 7380e1b2c8..0000000000 Binary files a/docs/img/create-db-and-table-with-db-browser/image009.png and /dev/null differ diff --git a/docs/img/create-db-and-table-with-db-browser/image010.png b/docs/img/create-db-and-table-with-db-browser/image010.png deleted file mode 100644 index 9d0961b45f..0000000000 Binary files a/docs/img/create-db-and-table-with-db-browser/image010.png and /dev/null differ diff --git a/docs/img/create-db-and-table/inline-errors01.png b/docs/img/create-db-and-table/inline-errors01.png deleted file mode 100644 index d3b7ac43e0..0000000000 Binary files a/docs/img/create-db-and-table/inline-errors01.png and /dev/null differ diff --git a/docs/img/databases/external-server.drawio.svg b/docs/img/databases/external-server.drawio.svg deleted file mode 100644 index f86b5dc1e6..0000000000 --- a/docs/img/databases/external-server.drawio.svg +++ /dev/null @@ -1,778 +0,0 @@ - - - - - - - - - - - - - - - -
-
-
- - Machine / Computer - -
-
-
-
- - Machine / Computer - -
-
-
- - - - - - - - - - - - - - - - - - - -
-
-
- - Database application - -
-
-
-
- - Database application - -
-
-
- - - - - - - - - - - - - - -
-
-
- - File - -
-
-
-
- - File - -
-
-
- - - - - - - -
-
-
- - Data - -
-
-
-
- - Data - -
-
-
- - - - - - - - - - - - - - -
-
-
- - File - -
-
-
-
- - File - -
-
-
- - - - - - - -
-
-
- - Data - -
-
-
-
- - Data - -
-
-
- - - - - - - - - - - - - - -
-
-
- - File - -
-
-
-
- - File - -
-
-
- - - - - - - -
-
-
- - Data - -
-
-
-
- - Data - -
-
-
- - - - - - - - - - - -
-
-
- - Machine / Computer - -
-
-
-
- - Machine / Computer - -
-
-
- - - - - - - -
-
-
- - Your code - -
-
-
-
- - Your code - -
-
-
- - - - -
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/databases/multiple-servers.drawio.svg b/docs/img/databases/multiple-servers.drawio.svg deleted file mode 100644 index b7370592a0..0000000000 --- a/docs/img/databases/multiple-servers.drawio.svg +++ /dev/null @@ -1,903 +0,0 @@ - - - - - - - - - - - - - - - -
-
-
- - Machine / Computer - -
-
-
-
- - Machine / Computer - -
-
-
- - - - - - - - - - - - - - - -
-
-
- - Database application - -
-
-
-
- - Database application - -
-
-
- - - - - - - - - - - - - - -
-
-
- - File - -
-
-
-
- - File - -
-
-
- - - - - - - -
-
-
- - Data - -
-
-
-
- - Data - -
-
-
- - - - - - - - - - - - - - -
-
-
- - File - -
-
-
-
- - File - -
-
-
- - - - - - - -
-
-
- - Data - -
-
-
-
- - Data - -
-
-
- - - - - - - - - - - -
-
-
- - Machine / Computer - -
-
-
-
- - Machine / Computer - -
-
-
- - - - - - - -
-
-
- - Your code - -
-
-
-
- - Your code - -
-
-
- - - - - - - - - - - - - - -
-
-
- - Machine / Computer - -
-
-
-
- - Machine / Computer - -
-
-
- - - - - - - - - - - - - - - -
-
-
- - Database application - -
-
-
-
- - Database application - -
-
-
- - - - - - - - - - - - - - -
-
-
- - File - -
-
-
-
- - File - -
-
-
- - - - - - - -
-
-
- - Data - -
-
-
-
- - Data - -
-
-
- - - - - - - - - - - - - - -
-
-
- - File - -
-
-
-
- - File - -
-
-
- - - - - - - -
-
-
- - Data - -
-
-
-
- - Data - -
-
-
- - - - - - - - - - -
-
-
- - Machine / Computer - -
-
-
-
- - Machine / Computer - -
-
-
- - - - - - - - - - - - - - - - - - - - -
-
-
- - Database application - -
-
-
-
- - Database application - -
-
-
- - - - - - - - - - - - - - -
-
-
- - File - -
-
-
-
- - File - -
-
-
- - - - - - - -
-
-
- - Data - -
-
-
-
- - Data - -
-
-
- - - - - - - - - - - - - - -
-
-
- - File - -
-
-
-
- - File - -
-
-
- - - - - - - -
-
-
- - Data - -
-
-
-
- - Data - -
-
-
- - - - - - - - - - - - - - -
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/databases/relationships.drawio.svg b/docs/img/databases/relationships.drawio.svg deleted file mode 100644 index e1cbb43e92..0000000000 --- a/docs/img/databases/relationships.drawio.svg +++ /dev/null @@ -1,1189 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - -
-
-
- - hero - -
-
-
-
- - hero - -
-
-
- - - - - - - - - - - -
-
-
- - - id - - -
-
-
-
- - id - -
-
-
- - - - - - - - -
-
-
- - - name - - -
-
-
-
- - name - -
-
-
- - - - - - - - -
-
-
- - - secret_name - - -
-
-
-
- - secret_name - -
-
-
- - - - - - - - -
-
-
- - - age - - -
-
-
-
- - age - -
-
-
- - - - - - - - -
-
-
- - - team_id - - -
-
-
-
- - team_id - -
-
-
- - - - - - - - - - - -
-
-
- - - 1 - - -
-
-
-
- - 1 - -
-
-
- - - - - - - - -
-
-
- - - Deadpond - - -
-
-
-
- - Deadpond - -
-
-
- - - - - - - - -
-
-
- - - Dive Wilson - - -
-
-
-
- - Dive Wilson - -
-
-
- - - - - - - - -
-
-
- - - null - - -
-
-
-
- - null - -
-
-
- - - - - - - - -
-
-
- - - 2 - - -
-
-
-
- - 2 - -
-
-
- - - - - - - - - - - -
-
-
- - - 2 - - -
-
-
-
- - 2 - -
-
-
- - - - - - - - -
-
-
- - - Spider-Boy - - -
-
-
-
- - Spider-Boy - -
-
-
- - - - - - - - -
-
-
- - - Pedro Parqueador - - -
-
-
-
- - Pedro Parqueador - -
-
-
- - - - - - - - -
-
-
- - - null - - -
-
-
-
- - null - -
-
-
- - - - - - - - -
-
-
- - - 1 - - -
-
-
-
- - 1 - -
-
-
- - - - - - - - - - - -
-
-
- - - 3 - - -
-
-
-
- - 3 - -
-
-
- - - - - - - - -
-
-
- - - Rusty-Man - - -
-
-
-
- - Rusty-Man - -
-
-
- - - - - - - - -
-
-
- - - Tommy Sharp - - -
-
-
-
- - Tommy Sharp - -
-
-
- - - - - - - - -
-
-
- - - 48 - - -
-
-
-
- - 48 - -
-
-
- - - - - - - - -
-
-
- - - 1 - - -
-
-
-
- - 1 - -
-
-
- - - - - - - - - - - - - -
-
-
- - team - -
-
-
-
- - team - -
-
-
- - - - - - - - - - - -
-
-
- - - id - - -
-
-
-
- - id - -
-
-
- - - - - - - - -
-
-
- - - name - - -
-
-
-
- - name - -
-
-
- - - - - - - - -
-
-
- - - headquarters - - -
-
-
-
- - headquarters - -
-
-
- - - - - - - - - - - -
-
-
- - - 1 - - -
-
-
-
- - 1 - -
-
-
- - - - - - - - -
-
-
- - - Preventers - - -
-
-
-
- - Preventers - -
-
-
- - - - - - - - -
-
-
- - - Sharp Tower - - -
-
-
-
- - Sharp Tower - -
-
-
- - - - - - - - - - - -
-
-
- - - 2 - - -
-
-
-
- - 2 - -
-
-
- - - - - - - - -
-
-
- - - Z-Force - - -
-
-
-
- - Z-Force - -
-
-
- - - - - - - - -
-
-
-

- - Sister Margaret's Bar - -

-
-
-
-
- - Sister Margaret's Bar - -
-
-
- - - - - - - - - - - - -
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/databases/same-server.drawio.svg b/docs/img/databases/same-server.drawio.svg deleted file mode 100644 index d9aabff8e9..0000000000 --- a/docs/img/databases/same-server.drawio.svg +++ /dev/null @@ -1,425 +0,0 @@ - - - - - - - - - - - - - - - -
-
-
- - Machine / Computer - -
-
-
-
- - Machine / Computer - -
-
-
- - - - - - - - - - - - - - - - - - - -
-
-
- - Database application - -
-
-
-
- - Database application - -
-
-
- - - - - - - - - - - - - - -
-
-
- - File - -
-
-
-
- - File - -
-
-
- - - - - - - -
-
-
- - Data - -
-
-
-
- - Data - -
-
-
- - - - - - - - - - - - - - -
-
-
- - File - -
-
-
-
- - File - -
-
-
- - - - - - - -
-
-
- - Data - -
-
-
-
- - Data - -
-
-
- - - - - - - - - - - - - - -
-
-
- - File - -
-
-
-
- - File - -
-
-
- - - - - - - -
-
-
- - Data - -
-
-
-
- - Data - -
-
-
- - - - - - - - - - - -
-
-
- - Your code - -
-
-
-
- - Your code - -
-
-
-
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/databases/single-file.drawio.svg b/docs/img/databases/single-file.drawio.svg deleted file mode 100644 index e92ae0ca3b..0000000000 --- a/docs/img/databases/single-file.drawio.svg +++ /dev/null @@ -1,119 +0,0 @@ - - - - - - - - - - - - - -
-
-
- - Machine / Computer - -
-
-
-
- - Machine / Computer - -
-
-
- - - - - - - - - - - -
-
-
- - Your code - -
-
-
-
- - Your code - -
-
-
- - - - - - - - - - - - - - -
-
-
- - File: heroes.db - -
-
-
-
- - File: heroes.db - -
-
-
- - - - - - - -
-
-
- - Data - -
-
-
-
- - Data - -
-
-
-
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/db-to-code/autocompletion01.png b/docs/img/db-to-code/autocompletion01.png deleted file mode 100644 index c498deaf67..0000000000 Binary files a/docs/img/db-to-code/autocompletion01.png and /dev/null differ diff --git a/docs/img/db-to-code/mapper.drawio.svg b/docs/img/db-to-code/mapper.drawio.svg deleted file mode 100644 index e402e4280a..0000000000 --- a/docs/img/db-to-code/mapper.drawio.svg +++ /dev/null @@ -1,291 +0,0 @@ - - - - - - - - - - - - - - - - - - - - -
-
-
- - Set of triangles - -
-
-
-
- - Set of triangles - -
-
-
- - - - - - - - - - - - - - - - - - - - - - - - -
-
-
- - Set of squares - -
-
-
-
- - Set of squares - -
-
-
- - - - - - - - - - - - - - - - - - - - -
-
-
- - Squares to Triangles - - Mapper - - -
-
-
-
- - Squares to Triangles Mapp... - -
-
-
- - - -
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/favicon.png b/docs/img/favicon.png deleted file mode 100755 index b83253be6c..0000000000 Binary files a/docs/img/favicon.png and /dev/null differ diff --git a/docs/img/features/autocompletion01.png b/docs/img/features/autocompletion01.png deleted file mode 100644 index f0d620b7fb..0000000000 Binary files a/docs/img/features/autocompletion01.png and /dev/null differ diff --git a/docs/img/github-social-preview.png b/docs/img/github-social-preview.png deleted file mode 100644 index 904f498278..0000000000 Binary files a/docs/img/github-social-preview.png and /dev/null differ diff --git a/docs/img/github-social-preview.svg b/docs/img/github-social-preview.svg deleted file mode 100644 index a0ce3e129d..0000000000 --- a/docs/img/github-social-preview.svg +++ /dev/null @@ -1,163 +0,0 @@ - - - - - - - - image/svg+xml - - - - - - - SQL databases in Python,designed for simplicity, compatibility, and robustness. - - SQLModel - - - - - - - - - - - - - - - - - - - - diff --git a/docs/img/icon-white.svg b/docs/img/icon-white.svg deleted file mode 100644 index 52a7c7e773..0000000000 --- a/docs/img/icon-white.svg +++ /dev/null @@ -1,68 +0,0 @@ - - - - - - - image/svg+xml - - - - - - - - - - diff --git a/docs/img/icon.svg b/docs/img/icon.svg deleted file mode 100644 index 22d28b46c8..0000000000 --- a/docs/img/icon.svg +++ /dev/null @@ -1,95 +0,0 @@ - - - - - - image/svg+xml - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/docs/img/index/autocompletion01.png b/docs/img/index/autocompletion01.png deleted file mode 100644 index cba2649fe9..0000000000 Binary files a/docs/img/index/autocompletion01.png and /dev/null differ diff --git a/docs/img/index/autocompletion02.png b/docs/img/index/autocompletion02.png deleted file mode 100644 index 5918e1d200..0000000000 Binary files a/docs/img/index/autocompletion02.png and /dev/null differ diff --git a/docs/img/index/inline-errors01.png b/docs/img/index/inline-errors01.png deleted file mode 100644 index b689c55624..0000000000 Binary files a/docs/img/index/inline-errors01.png and /dev/null differ diff --git a/docs/img/logo-margin/logo-margin-vector.svg b/docs/img/logo-margin/logo-margin-vector.svg deleted file mode 100644 index 335d27a6c6..0000000000 --- a/docs/img/logo-margin/logo-margin-vector.svg +++ /dev/null @@ -1,104 +0,0 @@ - - - - - - - image/svg+xml - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/docs/img/logo-margin/logo-margin-white-vector.svg b/docs/img/logo-margin/logo-margin-white-vector.svg deleted file mode 100644 index d87b002951..0000000000 --- a/docs/img/logo-margin/logo-margin-white-vector.svg +++ /dev/null @@ -1,100 +0,0 @@ - - - - - - - image/svg+xml - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/docs/img/logo-margin/logo-margin-white.svg b/docs/img/logo-margin/logo-margin-white.svg deleted file mode 100644 index 47ca776249..0000000000 --- a/docs/img/logo-margin/logo-margin-white.svg +++ /dev/null @@ -1,105 +0,0 @@ - - - - - - - image/svg+xml - - - - - SQLModel - - - - - - - - - - - - - - - - - - - diff --git a/docs/img/logo-margin/logo-margin.svg b/docs/img/logo-margin/logo-margin.svg deleted file mode 100644 index b6e4d00710..0000000000 --- a/docs/img/logo-margin/logo-margin.svg +++ /dev/null @@ -1,109 +0,0 @@ - - - - - - - image/svg+xml - - - - - - SQLModel - - - - - - - - - - - - - - - - - - - - diff --git a/docs/img/sponsors/govcert.png b/docs/img/sponsors/govcert.png deleted file mode 100644 index 0bb4cb934a..0000000000 Binary files a/docs/img/sponsors/govcert.png and /dev/null differ diff --git a/docs/img/tutorial/delete/image01.png b/docs/img/tutorial/delete/image01.png deleted file mode 100644 index fb994f22b3..0000000000 Binary files a/docs/img/tutorial/delete/image01.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/limit-and-offset/image01.png b/docs/img/tutorial/fastapi/limit-and-offset/image01.png deleted file mode 100644 index e9d3878505..0000000000 Binary files a/docs/img/tutorial/fastapi/limit-and-offset/image01.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/multiple-models/image01.png b/docs/img/tutorial/fastapi/multiple-models/image01.png deleted file mode 100644 index 8ceefe24d3..0000000000 Binary files a/docs/img/tutorial/fastapi/multiple-models/image01.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/multiple-models/image02.png b/docs/img/tutorial/fastapi/multiple-models/image02.png deleted file mode 100644 index 601454acd1..0000000000 Binary files a/docs/img/tutorial/fastapi/multiple-models/image02.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/multiple-models/image03.png b/docs/img/tutorial/fastapi/multiple-models/image03.png deleted file mode 100644 index 796ca5a370..0000000000 Binary files a/docs/img/tutorial/fastapi/multiple-models/image03.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/read-one/image01.png b/docs/img/tutorial/fastapi/read-one/image01.png deleted file mode 100644 index 7ef745a29b..0000000000 Binary files a/docs/img/tutorial/fastapi/read-one/image01.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/relationships/image01.png b/docs/img/tutorial/fastapi/relationships/image01.png deleted file mode 100644 index 79b8d80d63..0000000000 Binary files a/docs/img/tutorial/fastapi/relationships/image01.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/relationships/image02.png b/docs/img/tutorial/fastapi/relationships/image02.png deleted file mode 100644 index 0796793606..0000000000 Binary files a/docs/img/tutorial/fastapi/relationships/image02.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/relationships/image03.png b/docs/img/tutorial/fastapi/relationships/image03.png deleted file mode 100644 index e4e9ddc2aa..0000000000 Binary files a/docs/img/tutorial/fastapi/relationships/image03.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/relationships/image04.png b/docs/img/tutorial/fastapi/relationships/image04.png deleted file mode 100644 index 3780bbd224..0000000000 Binary files a/docs/img/tutorial/fastapi/relationships/image04.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/response-model/image01.png b/docs/img/tutorial/fastapi/response-model/image01.png deleted file mode 100644 index 38d4f04b41..0000000000 Binary files a/docs/img/tutorial/fastapi/response-model/image01.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/response-model/image02.png b/docs/img/tutorial/fastapi/response-model/image02.png deleted file mode 100644 index d6a1cfb6af..0000000000 Binary files a/docs/img/tutorial/fastapi/response-model/image02.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/simple-hero-api/db-browser-01.png b/docs/img/tutorial/fastapi/simple-hero-api/db-browser-01.png deleted file mode 100644 index d042a53234..0000000000 Binary files a/docs/img/tutorial/fastapi/simple-hero-api/db-browser-01.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/simple-hero-api/image01.png b/docs/img/tutorial/fastapi/simple-hero-api/image01.png deleted file mode 100644 index 27e8f219ab..0000000000 Binary files a/docs/img/tutorial/fastapi/simple-hero-api/image01.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/simple-hero-api/image02.png b/docs/img/tutorial/fastapi/simple-hero-api/image02.png deleted file mode 100644 index 551f5be926..0000000000 Binary files a/docs/img/tutorial/fastapi/simple-hero-api/image02.png and /dev/null differ diff --git a/docs/img/tutorial/fastapi/teams/image01.png b/docs/img/tutorial/fastapi/teams/image01.png deleted file mode 100644 index 109e7c9d66..0000000000 Binary files a/docs/img/tutorial/fastapi/teams/image01.png and /dev/null differ diff --git a/docs/img/tutorial/indexes/dictionary001.drawio.svg b/docs/img/tutorial/indexes/dictionary001.drawio.svg deleted file mode 100644 index 75ebe51f43..0000000000 --- a/docs/img/tutorial/indexes/dictionary001.drawio.svg +++ /dev/null @@ -1,660 +0,0 @@ - - - - - - - - - - - - - -
-
-
- - A - -
-
-
-
- - A - -
-
-
- - - - - - - -
-
-
- - B - -
-
-
-
- - B - -
-
-
- - - - - - - -
-
-
- - C - -
-
-
-
- - C - -
-
-
- - - - - - - -
-
-
- - D - -
-
-
-
- - D - -
-
-
- - - - - - - -
-
-
- - E - -
-
-
-
- - E - -
-
-
- - - - - - - -
-
-
- - F - -
-
-
-
- - F - -
-
-
- - - - - - - -
-
-
- - G - -
-
-
-
- - G - -
-
-
- - - - - - - -
-
-
- - H - -
-
-
-
- - H - -
-
-
- - - - - - - -
-
-
- - I - -
-
-
-
- - I - -
-
-
- - - - - - - -
-
-
- - J - -
-
-
-
- - J - -
-
-
- - - - - - - -
-
-
- - K - -
-
-
-
- - K - -
-
-
- - - - - - - -
-
-
- - L - -
-
-
-
- - L - -
-
-
- - - - - - - -
-
-
- - M - -
-
-
-
- - M - -
-
-
- - - - - - - -
-
-
- - N - -
-
-
-
- - N - -
-
-
- - - - - - - -
-
-
- - O - -
-
-
-
- - O - -
-
-
- - - - - - - -
-
-
- - P - -
-
-
-
- - P - -
-
-
- - - - - - - -
-
-
- - Q - -
-
-
-
- - Q - -
-
-
- - - - - - - -
-
-
- - R - -
-
-
-
- - R - -
-
-
- - - - - - - -
-
-
- - S - -
-
-
-
- - S - -
-
-
- - - - - - - -
-
-
- - T - -
-
-
-
- - T - -
-
-
- - - - - - - -
-
-
- - U - -
-
-
-
- - U - -
-
-
- - - - - - - -
-
-
- - V - -
-
-
-
- - V - -
-
-
- - - - - - - -
-
-
- - W - -
-
-
-
- - W - -
-
-
- - - - - - - -
-
-
- - X - -
-
-
-
- - X - -
-
-
- - - - - - - -
-
-
- - Y - -
-
-
-
- - Y - -
-
-
- - - - - - - -
-
-
- - Z - -
-
-
-
- - Z - -
-
-
- - - - - - - -
-
-
- - Dictionary - -
-
-
-
- - Dictionary - -
-
-
- - - - - - - -
-
-
- - M - -
-
-
-
- - M - -
-
-
-
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/indexes/dictionary002.drawio.svg b/docs/img/tutorial/indexes/dictionary002.drawio.svg deleted file mode 100644 index b468c90607..0000000000 --- a/docs/img/tutorial/indexes/dictionary002.drawio.svg +++ /dev/null @@ -1,660 +0,0 @@ - - - - - - - - - - - - - -
-
-
- - A - -
-
-
-
- - A - -
-
-
- - - - - - - -
-
-
- - B - -
-
-
-
- - B - -
-
-
- - - - - - - -
-
-
- - C - -
-
-
-
- - C - -
-
-
- - - - - - - -
-
-
- - D - -
-
-
-
- - D - -
-
-
- - - - - - - -
-
-
- - E - -
-
-
-
- - E - -
-
-
- - - - - - - -
-
-
- - F - -
-
-
-
- - F - -
-
-
- - - - - - - -
-
-
- - G - -
-
-
-
- - G - -
-
-
- - - - - - - -
-
-
- - H - -
-
-
-
- - H - -
-
-
- - - - - - - -
-
-
- - I - -
-
-
-
- - I - -
-
-
- - - - - - - -
-
-
- - J - -
-
-
-
- - J - -
-
-
- - - - - - - -
-
-
- - K - -
-
-
-
- - K - -
-
-
- - - - - - - -
-
-
- - L - -
-
-
-
- - L - -
-
-
- - - - - - - -
-
-
- - M - -
-
-
-
- - M - -
-
-
- - - - - - - -
-
-
- - N - -
-
-
-
- - N - -
-
-
- - - - - - - -
-
-
- - O - -
-
-
-
- - O - -
-
-
- - - - - - - -
-
-
- - P - -
-
-
-
- - P - -
-
-
- - - - - - - -
-
-
- - Q - -
-
-
-
- - Q - -
-
-
- - - - - - - -
-
-
- - R - -
-
-
-
- - R - -
-
-
- - - - - - - -
-
-
- - S - -
-
-
-
- - S - -
-
-
- - - - - - - -
-
-
- - T - -
-
-
-
- - T - -
-
-
- - - - - - - -
-
-
- - U - -
-
-
-
- - U - -
-
-
- - - - - - - -
-
-
- - V - -
-
-
-
- - V - -
-
-
- - - - - - - -
-
-
- - W - -
-
-
-
- - W - -
-
-
- - - - - - - -
-
-
- - X - -
-
-
-
- - X - -
-
-
- - - - - - - -
-
-
- - Y - -
-
-
-
- - Y - -
-
-
- - - - - - - -
-
-
- - Z - -
-
-
-
- - Z - -
-
-
- - - - - - - -
-
-
- - Dictionary - -
-
-
-
- - Dictionary - -
-
-
- - - - - - - -
-
-
- - M - -
-
-
-
- - M - -
-
-
-
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/indexes/dictionary003.drawio.svg b/docs/img/tutorial/indexes/dictionary003.drawio.svg deleted file mode 100644 index 2a036508f6..0000000000 --- a/docs/img/tutorial/indexes/dictionary003.drawio.svg +++ /dev/null @@ -1,640 +0,0 @@ - - - - - - - - - - - - - -
-
-
- - A - -
-
-
-
- - A - -
-
-
- - - - - - - -
-
-
- - B - -
-
-
-
- - B - -
-
-
- - - - - - - -
-
-
- - C - -
-
-
-
- - C - -
-
-
- - - - - - - -
-
-
- - D - -
-
-
-
- - D - -
-
-
- - - - - - - -
-
-
- - E - -
-
-
-
- - E - -
-
-
- - - - - - - -
-
-
- - F - -
-
-
-
- - F - -
-
-
- - - - - - - -
-
-
- - G - -
-
-
-
- - G - -
-
-
- - - - - - - -
-
-
- - H - -
-
-
-
- - H - -
-
-
- - - - - - - -
-
-
- - I - -
-
-
-
- - I - -
-
-
- - - - - - - -
-
-
- - J - -
-
-
-
- - J - -
-
-
- - - - - - - -
-
-
- - K - -
-
-
-
- - K - -
-
-
- - - - - - - -
-
-
- - L - -
-
-
-
- - L - -
-
-
- - - - - - - -
-
-
- - M - -
-
-
-
- - M - -
-
-
- - - - - - - -
-
-
- - N - -
-
-
-
- - N - -
-
-
- - - - - - - -
-
-
- - O - -
-
-
-
- - O - -
-
-
- - - - - - - -
-
-
- - P - -
-
-
-
- - P - -
-
-
- - - - - - - -
-
-
- - Q - -
-
-
-
- - Q - -
-
-
- - - - - - - -
-
-
- - R - -
-
-
-
- - R - -
-
-
- - - - - - - -
-
-
- - S - -
-
-
-
- - S - -
-
-
- - - - - - - -
-
-
- - T - -
-
-
-
- - T - -
-
-
- - - - - - - -
-
-
- - U - -
-
-
-
- - U - -
-
-
- - - - - - - -
-
-
- - V - -
-
-
-
- - V - -
-
-
- - - - - - - -
-
-
- - W - -
-
-
-
- - W - -
-
-
- - - - - - - -
-
-
- - X - -
-
-
-
- - X - -
-
-
- - - - - - - -
-
-
- - Y - -
-
-
-
- - Y - -
-
-
- - - - - - - -
-
-
- - Z - -
-
-
-
- - Z - -
-
-
- - - - - - - -
-
-
- - Dictionary - -
-
-
-
- - Dictionary - -
-
-
- - - -
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/indexes/dictionary004.drawio.svg b/docs/img/tutorial/indexes/dictionary004.drawio.svg deleted file mode 100644 index 76974a4bfb..0000000000 --- a/docs/img/tutorial/indexes/dictionary004.drawio.svg +++ /dev/null @@ -1,663 +0,0 @@ - - - - - - - - - - - - - -
-
-
- - A - -
-
-
-
- - A - -
-
-
- - - - - - - -
-
-
- - B - -
-
-
-
- - B - -
-
-
- - - - - - - -
-
-
- - C - -
-
-
-
- - C - -
-
-
- - - - - - - -
-
-
- - D - -
-
-
-
- - D - -
-
-
- - - - - - - -
-
-
- - E - -
-
-
-
- - E - -
-
-
- - - - - - - -
-
-
- - F - -
-
-
-
- - F - -
-
-
- - - - - - - -
-
-
- - G - -
-
-
-
- - G - -
-
-
- - - - - - - -
-
-
- - H - -
-
-
-
- - H - -
-
-
- - - - - - - -
-
-
- - I - -
-
-
-
- - I - -
-
-
- - - - - - - -
-
-
- - J - -
-
-
-
- - J - -
-
-
- - - - - - - -
-
-
- - K - -
-
-
-
- - K - -
-
-
- - - - - - - -
-
-
- - L - -
-
-
-
- - L - -
-
-
- - - - - - - -
-
-
- - M - -
-
-
-
- - M - -
-
-
- - - - - - - -
-
-
- - N - -
-
-
-
- - N - -
-
-
- - - - - - - -
-
-
- - O - -
-
-
-
- - O - -
-
-
- - - - - - - -
-
-
- - P - -
-
-
-
- - P - -
-
-
- - - - - - - -
-
-
- - Q - -
-
-
-
- - Q - -
-
-
- - - - - - - -
-
-
- - R - -
-
-
-
- - R - -
-
-
- - - - - - - -
-
-
- - S - -
-
-
-
- - S - -
-
-
- - - - - - - -
-
-
- - T - -
-
-
-
- - T - -
-
-
- - - - - - - -
-
-
- - U - -
-
-
-
- - U - -
-
-
- - - - - - - -
-
-
- - V - -
-
-
-
- - V - -
-
-
- - - - - - - -
-
-
- - W - -
-
-
-
- - W - -
-
-
- - - - - - - -
-
-
- - X - -
-
-
-
- - X - -
-
-
- - - - - - - -
-
-
- - Y - -
-
-
-
- - Y - -
-
-
- - - - - - - -
-
-
- - Z - -
-
-
-
- - Z - -
-
-
- - - - - - - -
-
-
- - Dictionary - -
-
-
-
- - Dictionary - -
-
-
- - - - - - - -
-
-
- - F - -
-
-
-
- - F - -
-
-
- - - -
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/indexes/dictionary005.drawio.svg b/docs/img/tutorial/indexes/dictionary005.drawio.svg deleted file mode 100644 index 865ad3530f..0000000000 --- a/docs/img/tutorial/indexes/dictionary005.drawio.svg +++ /dev/null @@ -1,640 +0,0 @@ - - - - - - - - - - - - - -
-
-
- - A - -
-
-
-
- - A - -
-
-
- - - - - - - -
-
-
- - B - -
-
-
-
- - B - -
-
-
- - - - - - - -
-
-
- - C - -
-
-
-
- - C - -
-
-
- - - - - - - -
-
-
- - D - -
-
-
-
- - D - -
-
-
- - - - - - - -
-
-
- - E - -
-
-
-
- - E - -
-
-
- - - - - - - -
-
-
- - F - -
-
-
-
- - F - -
-
-
- - - - - - - -
-
-
- - G - -
-
-
-
- - G - -
-
-
- - - - - - - -
-
-
- - H - -
-
-
-
- - H - -
-
-
- - - - - - - -
-
-
- - I - -
-
-
-
- - I - -
-
-
- - - - - - - -
-
-
- - J - -
-
-
-
- - J - -
-
-
- - - - - - - -
-
-
- - K - -
-
-
-
- - K - -
-
-
- - - - - - - -
-
-
- - L - -
-
-
-
- - L - -
-
-
- - - - - - - -
-
-
- - M - -
-
-
-
- - M - -
-
-
- - - - - - - -
-
-
- - N - -
-
-
-
- - N - -
-
-
- - - - - - - -
-
-
- - O - -
-
-
-
- - O - -
-
-
- - - - - - - -
-
-
- - P - -
-
-
-
- - P - -
-
-
- - - - - - - -
-
-
- - Q - -
-
-
-
- - Q - -
-
-
- - - - - - - -
-
-
- - R - -
-
-
-
- - R - -
-
-
- - - - - - - -
-
-
- - S - -
-
-
-
- - S - -
-
-
- - - - - - - -
-
-
- - T - -
-
-
-
- - T - -
-
-
- - - - - - - -
-
-
- - U - -
-
-
-
- - U - -
-
-
- - - - - - - -
-
-
- - V - -
-
-
-
- - V - -
-
-
- - - - - - - -
-
-
- - W - -
-
-
-
- - W - -
-
-
- - - - - - - -
-
-
- - X - -
-
-
-
- - X - -
-
-
- - - - - - - -
-
-
- - Y - -
-
-
-
- - Y - -
-
-
- - - - - - - -
-
-
- - Z - -
-
-
-
- - Z - -
-
-
- - - - - - - -
-
-
- - Dictionary - -
-
-
-
- - Dictionary - -
-
-
- - - -
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/indexes/dictionary006.drawio.svg b/docs/img/tutorial/indexes/dictionary006.drawio.svg deleted file mode 100644 index 2947ee7db9..0000000000 --- a/docs/img/tutorial/indexes/dictionary006.drawio.svg +++ /dev/null @@ -1,663 +0,0 @@ - - - - - - - - - - - - - -
-
-
- - A - -
-
-
-
- - A - -
-
-
- - - - - - - -
-
-
- - B - -
-
-
-
- - B - -
-
-
- - - - - - - -
-
-
- - C - -
-
-
-
- - C - -
-
-
- - - - - - - -
-
-
- - D - -
-
-
-
- - D - -
-
-
- - - - - - - -
-
-
- - E - -
-
-
-
- - E - -
-
-
- - - - - - - -
-
-
- - F - -
-
-
-
- - F - -
-
-
- - - - - - - -
-
-
- - G - -
-
-
-
- - G - -
-
-
- - - - - - - -
-
-
- - H - -
-
-
-
- - H - -
-
-
- - - - - - - -
-
-
- - I - -
-
-
-
- - I - -
-
-
- - - - - - - -
-
-
- - J - -
-
-
-
- - J - -
-
-
- - - - - - - -
-
-
- - K - -
-
-
-
- - K - -
-
-
- - - - - - - -
-
-
- - L - -
-
-
-
- - L - -
-
-
- - - - - - - -
-
-
- - M - -
-
-
-
- - M - -
-
-
- - - - - - - -
-
-
- - N - -
-
-
-
- - N - -
-
-
- - - - - - - -
-
-
- - O - -
-
-
-
- - O - -
-
-
- - - - - - - -
-
-
- - P - -
-
-
-
- - P - -
-
-
- - - - - - - -
-
-
- - Q - -
-
-
-
- - Q - -
-
-
- - - - - - - -
-
-
- - R - -
-
-
-
- - R - -
-
-
- - - - - - - -
-
-
- - S - -
-
-
-
- - S - -
-
-
- - - - - - - -
-
-
- - T - -
-
-
-
- - T - -
-
-
- - - - - - - -
-
-
- - U - -
-
-
-
- - U - -
-
-
- - - - - - - -
-
-
- - V - -
-
-
-
- - V - -
-
-
- - - - - - - -
-
-
- - W - -
-
-
-
- - W - -
-
-
- - - - - - - -
-
-
- - X - -
-
-
-
- - X - -
-
-
- - - - - - - -
-
-
- - Y - -
-
-
-
- - Y - -
-
-
- - - - - - - -
-
-
- - Z - -
-
-
-
- - Z - -
-
-
- - - - - - - -
-
-
- - Dictionary - -
-
-
-
- - Dictionary - -
-
-
- - - - - - - - - - -
-
-
- - C - -
-
-
-
- - C - -
-
-
-
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/indexes/dictionary007.drawio.svg b/docs/img/tutorial/indexes/dictionary007.drawio.svg deleted file mode 100644 index 651ad3287c..0000000000 --- a/docs/img/tutorial/indexes/dictionary007.drawio.svg +++ /dev/null @@ -1,643 +0,0 @@ - - - - - - - - - - - - - -
-
-
- - A - -
-
-
-
- - A - -
-
-
- - - - - - - -
-
-
- - B - -
-
-
-
- - B - -
-
-
- - - - - - - -
-
-
- - C - -
-
-
-
- - C - -
-
-
- - - - - - - -
-
-
- - D - -
-
-
-
- - D - -
-
-
- - - - - - - -
-
-
- - E - -
-
-
-
- - E - -
-
-
- - - - - - - -
-
-
- - F - -
-
-
-
- - F - -
-
-
- - - - - - - -
-
-
- - G - -
-
-
-
- - G - -
-
-
- - - - - - - -
-
-
- - H - -
-
-
-
- - H - -
-
-
- - - - - - - -
-
-
- - I - -
-
-
-
- - I - -
-
-
- - - - - - - -
-
-
- - J - -
-
-
-
- - J - -
-
-
- - - - - - - -
-
-
- - K - -
-
-
-
- - K - -
-
-
- - - - - - - -
-
-
- - L - -
-
-
-
- - L - -
-
-
- - - - - - - -
-
-
- - M - -
-
-
-
- - M - -
-
-
- - - - - - - -
-
-
- - N - -
-
-
-
- - N - -
-
-
- - - - - - - -
-
-
- - O - -
-
-
-
- - O - -
-
-
- - - - - - - -
-
-
- - P - -
-
-
-
- - P - -
-
-
- - - - - - - -
-
-
- - Q - -
-
-
-
- - Q - -
-
-
- - - - - - - -
-
-
- - R - -
-
-
-
- - R - -
-
-
- - - - - - - -
-
-
- - S - -
-
-
-
- - S - -
-
-
- - - - - - - -
-
-
- - T - -
-
-
-
- - T - -
-
-
- - - - - - - -
-
-
- - U - -
-
-
-
- - U - -
-
-
- - - - - - - -
-
-
- - V - -
-
-
-
- - V - -
-
-
- - - - - - - -
-
-
- - W - -
-
-
-
- - W - -
-
-
- - - - - - - -
-
-
- - X - -
-
-
-
- - X - -
-
-
- - - - - - - -
-
-
- - Y - -
-
-
-
- - Y - -
-
-
- - - - - - - -
-
-
- - Z - -
-
-
-
- - Z - -
-
-
- - - - - - - -
-
-
- - Dictionary - -
-
-
-
- - Dictionary - -
-
-
- - - - - - -
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/indexes/dictionary008.drawio.svg b/docs/img/tutorial/indexes/dictionary008.drawio.svg deleted file mode 100644 index 611c33177b..0000000000 --- a/docs/img/tutorial/indexes/dictionary008.drawio.svg +++ /dev/null @@ -1,666 +0,0 @@ - - - - - - - - - - - - - -
-
-
- - A - -
-
-
-
- - A - -
-
-
- - - - - - - -
-
-
- - B - -
-
-
-
- - B - -
-
-
- - - - - - - -
-
-
- - C - -
-
-
-
- - C - -
-
-
- - - - - - - -
-
-
- - D - -
-
-
-
- - D - -
-
-
- - - - - - - -
-
-
- - E - -
-
-
-
- - E - -
-
-
- - - - - - - -
-
-
- - F - -
-
-
-
- - F - -
-
-
- - - - - - - -
-
-
- - G - -
-
-
-
- - G - -
-
-
- - - - - - - -
-
-
- - H - -
-
-
-
- - H - -
-
-
- - - - - - - -
-
-
- - I - -
-
-
-
- - I - -
-
-
- - - - - - - -
-
-
- - J - -
-
-
-
- - J - -
-
-
- - - - - - - -
-
-
- - K - -
-
-
-
- - K - -
-
-
- - - - - - - -
-
-
- - L - -
-
-
-
- - L - -
-
-
- - - - - - - -
-
-
- - M - -
-
-
-
- - M - -
-
-
- - - - - - - -
-
-
- - N - -
-
-
-
- - N - -
-
-
- - - - - - - -
-
-
- - O - -
-
-
-
- - O - -
-
-
- - - - - - - -
-
-
- - P - -
-
-
-
- - P - -
-
-
- - - - - - - -
-
-
- - Q - -
-
-
-
- - Q - -
-
-
- - - - - - - -
-
-
- - R - -
-
-
-
- - R - -
-
-
- - - - - - - -
-
-
- - S - -
-
-
-
- - S - -
-
-
- - - - - - - -
-
-
- - T - -
-
-
-
- - T - -
-
-
- - - - - - - -
-
-
- - U - -
-
-
-
- - U - -
-
-
- - - - - - - -
-
-
- - V - -
-
-
-
- - V - -
-
-
- - - - - - - -
-
-
- - W - -
-
-
-
- - W - -
-
-
- - - - - - - -
-
-
- - X - -
-
-
-
- - X - -
-
-
- - - - - - - -
-
-
- - Y - -
-
-
-
- - Y - -
-
-
- - - - - - - -
-
-
- - Z - -
-
-
-
- - Z - -
-
-
- - - - - - - -
-
-
- - Dictionary - -
-
-
-
- - Dictionary - -
-
-
- - - - - - - - - - - - - -
-
-
- - D - -
-
-
-
- - D - -
-
-
-
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/indexes/techbook001.drawio.svg b/docs/img/tutorial/indexes/techbook001.drawio.svg deleted file mode 100644 index cb022465f6..0000000000 --- a/docs/img/tutorial/indexes/techbook001.drawio.svg +++ /dev/null @@ -1,346 +0,0 @@ - - - - - - - - - - - - - -
-
-
- - Technical Book - -
-
-
-
- - Technical Book - -
-
-
- - - - - - - -
-
-
- - Chapter 1 - -
-
-
-
- - Chapter 1 - -
-
-
- - - - - - - -
-
-
- - Chapter 2 - -
-
-
-
- - Chapter 2 - -
-
-
- - - - - - - -
-
-
- - Chapter 3 - -
-
-
-
- - Chapter 3 - -
-
-
- - - - - - - -
-
-
- - Chapter 4 - -
-
-
-
- - Chapter 4 - -
-
-
- - - - - - - -
-
-
- - Chapter 5 - -
-
-
-
- - Chapter 5 - -
-
-
- - - - - - - -
-
-
- - Chapter 6 - -
-
-
-
- - Chapter 6 - -
-
-
- - - - - - - -
-
-
- - Chapter 7 - -
-
-
-
- - Chapter 7 - -
-
-
- - - - - - - - - - -
-
-
- - Book Index - -
-
-
-
- - Book Index - -
-
-
- - - - - - - - - - - - - - - -
-
-
- - Database - -
-
-
-
- - Database - -
-
-
- - - - - - - - - - - - - - - -
-
-
- - Python - -
-
-
-
- - Python - -
-
-
- - - - - - - - - - - - - - - -
-
-
- - Files - -
-
-
-
- - Files - -
-
-
- - - - - - - - - - - -
-
-
- - Editors - -
-
-
-
- - Editors - -
-
-
-
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/insert/image01.png b/docs/img/tutorial/insert/image01.png deleted file mode 100644 index 7d98d889ed..0000000000 Binary files a/docs/img/tutorial/insert/image01.png and /dev/null differ diff --git a/docs/img/tutorial/insert/image02.png b/docs/img/tutorial/insert/image02.png deleted file mode 100644 index 4960fbce36..0000000000 Binary files a/docs/img/tutorial/insert/image02.png and /dev/null differ diff --git a/docs/img/tutorial/insert/image03.png b/docs/img/tutorial/insert/image03.png deleted file mode 100644 index 800f006a0e..0000000000 Binary files a/docs/img/tutorial/insert/image03.png and /dev/null differ diff --git a/docs/img/tutorial/many-to-many/many-to-many.drawio.svg b/docs/img/tutorial/many-to-many/many-to-many.drawio.svg deleted file mode 100644 index d362455cec..0000000000 --- a/docs/img/tutorial/many-to-many/many-to-many.drawio.svg +++ /dev/null @@ -1,1206 +0,0 @@ - - - - - - - - - - - - - - - - - - - - -
-
-
- - hero - -
-
-
-
- - hero - -
-
-
- - - - - - - - - - - -
-
-
- - - id - - -
-
-
-
- - id - -
-
-
- - - - - - - - -
-
-
- - - name - - -
-
-
-
- - name - -
-
-
- - - - - - - - -
-
-
- - - secret_name - - -
-
-
-
- - secret_name - -
-
-
- - - - - - - - -
-
-
- - - age - - -
-
-
-
- - age - -
-
-
- - - - - - - - - - - -
-
-
- - - 1 - - -
-
-
-
- - 1 - -
-
-
- - - - - - - - -
-
-
- - Deadpond - -
-
-
-
- - Deadpond - -
-
-
- - - - - - - - -
-
-
- - Dive Wilson - -
-
-
-
- - Dive Wilson - -
-
-
- - - - - - - - -
-
-
- - null - -
-
-
-
- - null - -
-
-
- - - - - - - - - - - -
-
-
- - 2 - -
-
-
-
- - 2 - -
-
-
- - - - - - - - -
-
-
- - Spider-Boy - -
-
-
-
- - Spider-Boy - -
-
-
- - - - - - - - -
-
-
- - Pedro Parqueador - -
-
-
-
- - Pedro Parqueador - -
-
-
- - - - - - - - -
-
-
- - null - -
-
-
-
- - null - -
-
-
- - - - - - - - - - - -
-
-
- - 3 - -
-
-
-
- - 3 - -
-
-
- - - - - - - - -
-
-
- - Rusty-Man - -
-
-
-
- - Rusty-Man - -
-
-
- - - - - - - - -
-
-
- - Tommy Sharp - -
-
-
-
- - Tommy Sharp - -
-
-
- - - - - - - - -
-
-
- - 48 - -
-
-
-
- - 48 - -
-
-
- - - - - - - - - - - - - -
-
-
- - team - -
-
-
-
- - team - -
-
-
- - - - - - - - - - - -
-
-
- - - id - - -
-
-
-
- - id - -
-
-
- - - - - - - - -
-
-
- - - name - - -
-
-
-
- - name - -
-
-
- - - - - - - - -
-
-
- - - headquarters - - -
-
-
-
- - headquarters - -
-
-
- - - - - - - - - - - -
-
-
- - - 1 - - -
-
-
-
- - 1 - -
-
-
- - - - - - - - -
-
-
- - Preventers - -
-
-
-
- - Preventers - -
-
-
- - - - - - - - -
-
-
- - - Sharp Tower - - -
-
-
-
- - Sharp Tower - -
-
-
- - - - - - - - - - - -
-
-
- - 2 - -
-
-
-
- - 2 - -
-
-
- - - - - - - - -
-
-
- - Z-Force - -
-
-
-
- - Z-Force - -
-
-
- - - - - - - - -
-
-
-

- - Sister Margaret's Bar - -

-
-
-
-
- - Sister Margaret's Bar - -
-
-
- - - - - - - - - - - - - - -
-
-
- - heroteamlink - -
-
-
-
- - heroteamlink - -
-
-
- - - - - - - - - - - -
-
-
- - - hero_id - - -
-
-
-
- - hero_id - -
-
-
- - - - - - - - -
-
-
- - - team_id - - -
-
-
-
- - team_id - -
-
-
- - - - - - - - - - - -
-
-
- - - 1 - - -
-
-
-
- - 1 - -
-
-
- - - - - - - - -
-
-
- - 1 - -
-
-
-
- - 1 - -
-
-
- - - - - - - - - - - -
-
-
- - 1 - -
-
-
-
- - 1 - -
-
-
- - - - - - - - -
-
-
- - 2 - -
-
-
-
- - 2 - -
-
-
- - - - - - - - - - - -
-
-
- - 2 - -
-
-
-
- - 2 - -
-
-
- - - - - - - - -
-
-
- - 1 - -
-
-
-
- - 1 - -
-
-
- - - - - - - - - - - -
-
-
- - 3 - -
-
-
-
- - 3 - -
-
-
- - - - - - - - -
-
-
- - - 1 - - -
-
-
-
- - 1 - -
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/offset-and-limit/db-browser.png b/docs/img/tutorial/offset-and-limit/db-browser.png deleted file mode 100644 index f6f0d77a32..0000000000 Binary files a/docs/img/tutorial/offset-and-limit/db-browser.png and /dev/null differ diff --git a/docs/img/tutorial/offset-and-limit/limit.drawio.svg b/docs/img/tutorial/offset-and-limit/limit.drawio.svg deleted file mode 100644 index 41ec69a3cd..0000000000 --- a/docs/img/tutorial/offset-and-limit/limit.drawio.svg +++ /dev/null @@ -1,1031 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
- - hero - -
-
-
-
- - hero - -
-
-
- - - - - - - - - - - -
-
-
- - - id - - -
-
-
-
- - id - -
-
-
- - - - - - - - -
-
-
- - - name - - -
-
-
-
- - name - -
-
-
- - - - - - - - -
-
-
- - - secret_name - - -
-
-
-
- - secret_name - -
-
-
- - - - - - - - -
-
-
- - - age - - -
-
-
-
- - age - -
-
-
- - - - - - - - - - - -
-
-
- - - 1 - - -
-
-
-
- - 1 - -
-
-
- - - - - - - - -
-
-
- - Deadpond - -
-
-
-
- - Deadpond - -
-
-
- - - - - - - - -
-
-
- - Dive Wilson - -
-
-
-
- - Dive Wilson - -
-
-
- - - - - - - - -
-
-
- - null - -
-
-
-
- - null - -
-
-
- - - - - - - - - - - -
-
-
- - 2 - -
-
-
-
- - 2 - -
-
-
- - - - - - - - -
-
-
- - Spider-Boy - -
-
-
-
- - Spider-Boy - -
-
-
- - - - - - - - -
-
-
- - Pedro Parqueador - -
-
-
-
- - Pedro Parqueador - -
-
-
- - - - - - - - -
-
-
- - null - -
-
-
-
- - null - -
-
-
- - - - - - - - - - - -
-
-
- - 3 - -
-
-
-
- - 3 - -
-
-
- - - - - - - - -
-
-
- - Rusty-Man - -
-
-
-
- - Rusty-Man - -
-
-
- - - - - - - - -
-
-
- - Tommy Sharp - -
-
-
-
- - Tommy Sharp - -
-
-
- - - - - - - - -
-
-
- - 48 - -
-
-
-
- - 48 - -
-
-
- - - - - - - - - - - -
-
-
- - - 4 - - -
-
-
-
- - 4 - -
-
-
- - - - - - - - -
-
-
- - Tarantula - -
-
-
-
- - Tarantula - -
-
-
- - - - - - - - -
-
-
- - - Natalia Roman-on - - -
-
-
-
- - Natalia Roman-on - -
-
-
- - - - - - - - -
-
-
- - 32 - -
-
-
-
- - 32 - -
-
-
- - - - - - - - - - - -
-
-
- - 5 - -
-
-
-
- - 5 - -
-
-
- - - - - - - - -
-
-
- - - Black Lion - - -
-
-
-
- - Black Lion - -
-
-
- - - - - - - - -
-
-
- - - Trevor Challa - - -
-
-
-
- - Trevor Challa - -
-
-
- - - - - - - - -
-
-
- - 35 - -
-
-
-
- - 35 - -
-
-
- - - - - - - - - - - -
-
-
- - 6 - -
-
-
-
- - 6 - -
-
-
- - - - - - - - -
-
-
- - - Dr. Weird - - -
-
-
-
- - Dr. Weird - -
-
-
- - - - - - - - -
-
-
- - - Steve Weird - - -
-
-
-
- - Steve Weird - -
-
-
- - - - - - - - -
-
-
- - 36 - -
-
-
-
- - 36 - -
-
-
- - - - - - - - - - - -
-
-
- - 7 - -
-
-
-
- - 7 - -
-
-
- - - - - - - - -
-
-
- - - Captain North America - - -
-
-
-
- - Captain North America - -
-
-
- - - - - - - - -
-
-
- - - Esteban Rogelios - - -
-
-
-
- - Esteban Rogelios - -
-
-
- - - - - - - - -
-
-
- - 93 - -
-
-
-
- - 93 - -
-
-
-
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/offset-and-limit/limit2.drawio.svg b/docs/img/tutorial/offset-and-limit/limit2.drawio.svg deleted file mode 100644 index 1f23e8682b..0000000000 --- a/docs/img/tutorial/offset-and-limit/limit2.drawio.svg +++ /dev/null @@ -1,1031 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
- - hero - -
-
-
-
- - hero - -
-
-
- - - - - - - - - - - -
-
-
- - - id - - -
-
-
-
- - id - -
-
-
- - - - - - - - -
-
-
- - - name - - -
-
-
-
- - name - -
-
-
- - - - - - - - -
-
-
- - - secret_name - - -
-
-
-
- - secret_name - -
-
-
- - - - - - - - -
-
-
- - - age - - -
-
-
-
- - age - -
-
-
- - - - - - - - - - - -
-
-
- - - 1 - - -
-
-
-
- - 1 - -
-
-
- - - - - - - - -
-
-
- - Deadpond - -
-
-
-
- - Deadpond - -
-
-
- - - - - - - - -
-
-
- - Dive Wilson - -
-
-
-
- - Dive Wilson - -
-
-
- - - - - - - - -
-
-
- - null - -
-
-
-
- - null - -
-
-
- - - - - - - - - - - -
-
-
- - 2 - -
-
-
-
- - 2 - -
-
-
- - - - - - - - -
-
-
- - Spider-Boy - -
-
-
-
- - Spider-Boy - -
-
-
- - - - - - - - -
-
-
- - Pedro Parqueador - -
-
-
-
- - Pedro Parqueador - -
-
-
- - - - - - - - -
-
-
- - null - -
-
-
-
- - null - -
-
-
- - - - - - - - - - - -
-
-
- - 3 - -
-
-
-
- - 3 - -
-
-
- - - - - - - - -
-
-
- - Rusty-Man - -
-
-
-
- - Rusty-Man - -
-
-
- - - - - - - - -
-
-
- - Tommy Sharp - -
-
-
-
- - Tommy Sharp - -
-
-
- - - - - - - - -
-
-
- - 48 - -
-
-
-
- - 48 - -
-
-
- - - - - - - - - - - -
-
-
- - - 4 - - -
-
-
-
- - 4 - -
-
-
- - - - - - - - -
-
-
- - Tarantula - -
-
-
-
- - Tarantula - -
-
-
- - - - - - - - -
-
-
- - - Natalia Roman-on - - -
-
-
-
- - Natalia Roman-on - -
-
-
- - - - - - - - -
-
-
- - 32 - -
-
-
-
- - 32 - -
-
-
- - - - - - - - - - - -
-
-
- - 5 - -
-
-
-
- - 5 - -
-
-
- - - - - - - - -
-
-
- - - Black Lion - - -
-
-
-
- - Black Lion - -
-
-
- - - - - - - - -
-
-
- - - Trevor Challa - - -
-
-
-
- - Trevor Challa - -
-
-
- - - - - - - - -
-
-
- - 35 - -
-
-
-
- - 35 - -
-
-
- - - - - - - - - - - -
-
-
- - 6 - -
-
-
-
- - 6 - -
-
-
- - - - - - - - -
-
-
- - - Dr. Weird - - -
-
-
-
- - Dr. Weird - -
-
-
- - - - - - - - -
-
-
- - - Steve Weird - - -
-
-
-
- - Steve Weird - -
-
-
- - - - - - - - -
-
-
- - 36 - -
-
-
-
- - 36 - -
-
-
- - - - - - - - - - - -
-
-
- - 7 - -
-
-
-
- - 7 - -
-
-
- - - - - - - - -
-
-
- - - Captain North America - - -
-
-
-
- - Captain North America - -
-
-
- - - - - - - - -
-
-
- - - Esteban Rogelios - - -
-
-
-
- - Esteban Rogelios - -
-
-
- - - - - - - - -
-
-
- - 93 - -
-
-
-
- - 93 - -
-
-
-
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/offset-and-limit/limit3.drawio.svg b/docs/img/tutorial/offset-and-limit/limit3.drawio.svg deleted file mode 100644 index 30b1c9f85a..0000000000 --- a/docs/img/tutorial/offset-and-limit/limit3.drawio.svg +++ /dev/null @@ -1,1031 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
- - hero - -
-
-
-
- - hero - -
-
-
- - - - - - - - - - - -
-
-
- - - id - - -
-
-
-
- - id - -
-
-
- - - - - - - - -
-
-
- - - name - - -
-
-
-
- - name - -
-
-
- - - - - - - - -
-
-
- - - secret_name - - -
-
-
-
- - secret_name - -
-
-
- - - - - - - - -
-
-
- - - age - - -
-
-
-
- - age - -
-
-
- - - - - - - - - - - -
-
-
- - - 1 - - -
-
-
-
- - 1 - -
-
-
- - - - - - - - -
-
-
- - Deadpond - -
-
-
-
- - Deadpond - -
-
-
- - - - - - - - -
-
-
- - Dive Wilson - -
-
-
-
- - Dive Wilson - -
-
-
- - - - - - - - -
-
-
- - null - -
-
-
-
- - null - -
-
-
- - - - - - - - - - - -
-
-
- - 2 - -
-
-
-
- - 2 - -
-
-
- - - - - - - - -
-
-
- - Spider-Boy - -
-
-
-
- - Spider-Boy - -
-
-
- - - - - - - - -
-
-
- - Pedro Parqueador - -
-
-
-
- - Pedro Parqueador - -
-
-
- - - - - - - - -
-
-
- - null - -
-
-
-
- - null - -
-
-
- - - - - - - - - - - -
-
-
- - 3 - -
-
-
-
- - 3 - -
-
-
- - - - - - - - -
-
-
- - Rusty-Man - -
-
-
-
- - Rusty-Man - -
-
-
- - - - - - - - -
-
-
- - Tommy Sharp - -
-
-
-
- - Tommy Sharp - -
-
-
- - - - - - - - -
-
-
- - 48 - -
-
-
-
- - 48 - -
-
-
- - - - - - - - - - - -
-
-
- - - 4 - - -
-
-
-
- - 4 - -
-
-
- - - - - - - - -
-
-
- - Tarantula - -
-
-
-
- - Tarantula - -
-
-
- - - - - - - - -
-
-
- - - Natalia Roman-on - - -
-
-
-
- - Natalia Roman-on - -
-
-
- - - - - - - - -
-
-
- - 32 - -
-
-
-
- - 32 - -
-
-
- - - - - - - - - - - -
-
-
- - 5 - -
-
-
-
- - 5 - -
-
-
- - - - - - - - -
-
-
- - - Black Lion - - -
-
-
-
- - Black Lion - -
-
-
- - - - - - - - -
-
-
- - - Trevor Challa - - -
-
-
-
- - Trevor Challa - -
-
-
- - - - - - - - -
-
-
- - 35 - -
-
-
-
- - 35 - -
-
-
- - - - - - - - - - - -
-
-
- - 6 - -
-
-
-
- - 6 - -
-
-
- - - - - - - - -
-
-
- - - Dr. Weird - - -
-
-
-
- - Dr. Weird - -
-
-
- - - - - - - - -
-
-
- - - Steve Weird - - -
-
-
-
- - Steve Weird - -
-
-
- - - - - - - - -
-
-
- - 36 - -
-
-
-
- - 36 - -
-
-
- - - - - - - - - - - -
-
-
- - 7 - -
-
-
-
- - 7 - -
-
-
- - - - - - - - -
-
-
- - - Captain North America - - -
-
-
-
- - Captain North America - -
-
-
- - - - - - - - -
-
-
- - - Esteban Rogelios - - -
-
-
-
- - Esteban Rogelios - -
-
-
- - - - - - - - -
-
-
- - 93 - -
-
-
-
- - 93 - -
-
-
-
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/relationships/attributes/back-populates.drawio.svg b/docs/img/tutorial/relationships/attributes/back-populates.drawio.svg deleted file mode 100644 index 434eab1c32..0000000000 --- a/docs/img/tutorial/relationships/attributes/back-populates.drawio.svg +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/docs/img/tutorial/relationships/attributes/back-populates2.drawio.svg b/docs/img/tutorial/relationships/attributes/back-populates2.drawio.svg deleted file mode 100644 index b94723dc19..0000000000 --- a/docs/img/tutorial/relationships/attributes/back-populates2.drawio.svg +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/docs/img/tutorial/relationships/select/image01.png b/docs/img/tutorial/relationships/select/image01.png deleted file mode 100644 index bf7c3bd657..0000000000 Binary files a/docs/img/tutorial/relationships/select/image01.png and /dev/null differ diff --git a/docs/img/tutorial/relationships/select/image02.png b/docs/img/tutorial/relationships/select/image02.png deleted file mode 100644 index baeaff8d0a..0000000000 Binary files a/docs/img/tutorial/relationships/select/image02.png and /dev/null differ diff --git a/docs/img/tutorial/relationships/select/image03.png b/docs/img/tutorial/relationships/select/image03.png deleted file mode 100644 index 565cd1f44c..0000000000 Binary files a/docs/img/tutorial/relationships/select/image03.png and /dev/null differ diff --git a/docs/img/tutorial/relationships/select/relationships2.drawio.svg b/docs/img/tutorial/relationships/select/relationships2.drawio.svg deleted file mode 100644 index 131c74dc15..0000000000 --- a/docs/img/tutorial/relationships/select/relationships2.drawio.svg +++ /dev/null @@ -1,988 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - -
-
-
- - hero - -
-
-
-
- - hero - -
-
-
- - - - - - - - - - - -
-
-
- - - id - - -
-
-
-
- - id - -
-
-
- - - - - - - - -
-
-
- - - name - - -
-
-
-
- - name - -
-
-
- - - - - - - - -
-
-
- - - secret_name - - -
-
-
-
- - secret_name - -
-
-
- - - - - - - - -
-
-
- - - age - - -
-
-
-
- - age - -
-
-
- - - - - - - - -
-
-
- - - team_id - - -
-
-
-
- - team_id - -
-
-
- - - - - - - - - - - -
-
-
- - - 1 - - -
-
-
-
- - 1 - -
-
-
- - - - - - - - -
-
-
- - Deadpond - -
-
-
-
- - Deadpond - -
-
-
- - - - - - - - -
-
-
- - Dive Wilson - -
-
-
-
- - Dive Wilson - -
-
-
- - - - - - - - -
-
-
- - null - -
-
-
-
- - null - -
-
-
- - - - - - - - -
-
-
- - 2 - -
-
-
-
- - 2 - -
-
-
- - - - - - - - - - - -
-
-
- - 2 - -
-
-
-
- - 2 - -
-
-
- - - - - - - - -
-
-
- - Spider-Boy - -
-
-
-
- - Spider-Boy - -
-
-
- - - - - - - - -
-
-
- - Pedro Parqueador - -
-
-
-
- - Pedro Parqueador - -
-
-
- - - - - - - - -
-
-
- - null - -
-
-
-
- - null - -
-
-
- - - - - - - - -
-
-
- - null - -
-
-
-
- - null - -
-
-
- - - - - - - - - - - -
-
-
- - 3 - -
-
-
-
- - 3 - -
-
-
- - - - - - - - -
-
-
- - Rusty-Man - -
-
-
-
- - Rusty-Man - -
-
-
- - - - - - - - -
-
-
- - Tommy Sharp - -
-
-
-
- - Tommy Sharp - -
-
-
- - - - - - - - -
-
-
- - 48 - -
-
-
-
- - 48 - -
-
-
- - - - - - - - -
-
-
- - 1 - -
-
-
-
- - 1 - -
-
-
- - - - - - - - - - - - - -
-
-
- - team - -
-
-
-
- - team - -
-
-
- - - - - - - - - - - -
-
-
- - - id - - -
-
-
-
- - id - -
-
-
- - - - - - - - -
-
-
- - - name - - -
-
-
-
- - name - -
-
-
- - - - - - - - -
-
-
- - - headquarters - - -
-
-
-
- - headquarters - -
-
-
- - - - - - - - - - - -
-
-
- - - 1 - - -
-
-
-
- - 1 - -
-
-
- - - - - - - - -
-
-
- - Preventers - -
-
-
-
- - Preventers - -
-
-
- - - - - - - - -
-
-
- - - Sharp Tower - - -
-
-
-
- - Sharp Tower - -
-
-
- - - - - - - - - - - -
-
-
- - 2 - -
-
-
-
- - 2 - -
-
-
- - - - - - - - -
-
-
- - Z-Force - -
-
-
-
- - Z-Force - -
-
-
- - - - - - - - -
-
-
-

- - Sister Margaret's Bar - -

-
-
-
-
- - Sister Margaret's Bar - -
-
-
- - - - - - - - -
- - - - - Text is not SVG - cannot display - - - -
diff --git a/docs/img/tutorial/select/image01.png b/docs/img/tutorial/select/image01.png deleted file mode 100644 index 3415ecf378..0000000000 Binary files a/docs/img/tutorial/select/image01.png and /dev/null differ diff --git a/docs/img/tutorial/update/image01.png b/docs/img/tutorial/update/image01.png deleted file mode 100644 index 3f943a40dd..0000000000 Binary files a/docs/img/tutorial/update/image01.png and /dev/null differ diff --git a/docs/img/tutorial/where/image01.png b/docs/img/tutorial/where/image01.png deleted file mode 100644 index 554d4fad14..0000000000 Binary files a/docs/img/tutorial/where/image01.png and /dev/null differ diff --git a/docs/index.md b/docs/index.md deleted file mode 100644 index cd48b91385..0000000000 --- a/docs/index.md +++ /dev/null @@ -1,232 +0,0 @@ - - -

- SQLModel - - SQLModel - -

-

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

-

- - Test - - - Publish - - - Coverage - - Package version - -

- ---- - -**Documentation**: https://sqlmodel.tiangolo.com - -**Source Code**: https://github.com/fastapi/sqlmodel - ---- - -SQLModel is a library for interacting with SQL databases from Python code, with Python objects. It is designed to be intuitive, easy to use, highly compatible, and robust. - -**SQLModel** is based on Python type annotations, and powered by Pydantic and SQLAlchemy. - -The key features are: - -* **Intuitive to write**: Great editor support. Completion everywhere. Less time debugging. Designed to be easy to use and learn. Less time reading docs. -* **Easy to use**: It has sensible defaults and does a lot of work underneath to simplify the code you write. -* **Compatible**: It is designed to be compatible with **FastAPI**, Pydantic, and SQLAlchemy. -* **Extensible**: You have all the power of SQLAlchemy and Pydantic underneath. -* **Short**: Minimize code duplication. A single type annotation does a lot of work. No need to duplicate models in SQLAlchemy and Pydantic. - -## Sponsors - - - -{% if sponsors %} -{% for sponsor in sponsors.gold -%} - -{% endfor -%} -{%- for sponsor in sponsors.silver -%} - -{% endfor %} -{% endif %} - - - -## SQL Databases in FastAPI - - - -**SQLModel** is designed to simplify interacting with SQL databases in FastAPI applications, it was created by the same author. 😁 - -It combines SQLAlchemy and Pydantic and tries to simplify the code you write as much as possible, allowing you to reduce the **code duplication to a minimum**, but while getting the **best developer experience** possible. - -**SQLModel** is, in fact, a thin layer on top of **Pydantic** and **SQLAlchemy**, carefully designed to be compatible with both. - -## Requirements - -A recent and currently supported version of Python. - -As **SQLModel** is based on **Pydantic** and **SQLAlchemy**, it requires them. They will be automatically installed when you install SQLModel. - -## Installation - -Make sure you create a virtual environment, activate it, and then install SQLModel, for example with: - -
- -```console -$ pip install sqlmodel ----> 100% -Successfully installed sqlmodel -``` - -
- -## Example - -For an introduction to databases, SQL, and everything else, see the SQLModel documentation. - -Here's a quick example. ✨ - -### A SQL Table - -Imagine you have a SQL table called `hero` with: - -* `id` -* `name` -* `secret_name` -* `age` - -And you want it to have this data: - -| id | name | secret_name | age | ------|------|-------------|------| -| 1 | Deadpond | Dive Wilson | null | -| 2 | Spider-Boy | Pedro Parqueador | null | -| 3 | Rusty-Man | Tommy Sharp | 48 | - -### Create a SQLModel Model - -Then you could create a **SQLModel** model like this: - -```Python -from sqlmodel import Field, SQLModel - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None -``` - -That class `Hero` is a **SQLModel** model, the equivalent of a SQL table in Python code. - -And each of those class attributes is equivalent to each **table column**. - -### Create Rows - -Then you could **create each row** of the table as an **instance** of the model: - -```Python -hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") -hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") -hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) -``` - -This way, you can use conventional Python code with **classes** and **instances** that represent **tables** and **rows**, and that way communicate with the **SQL database**. - -### Editor Support - -Everything is designed for you to get the best developer experience possible, with the best editor support. - -Including **autocompletion**: - - - -And **inline errors**: - - - -### Write to the Database - -You can learn a lot more about **SQLModel** by quickly following the **tutorial**, but if you need a taste right now of how to put all that together and save to the database, you can do this: - -```Python hl_lines="16 19 21-25" -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = 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) - - -engine = create_engine("sqlite:///database.db") - - -SQLModel.metadata.create_all(engine) - -with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.commit() -``` - -That will save a **SQLite** database with the 3 heroes. - -### Select from the Database - -Then you could write queries to select from that same database, for example with: - -```Python hl_lines="13-17" -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -engine = create_engine("sqlite:///database.db") - -with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - hero = session.exec(statement).first() - print(hero) -``` - -### Editor Support Everywhere - -**SQLModel** was carefully designed to give you the best developer experience and editor support, **even after selecting data** from the database: - - - -## SQLAlchemy and Pydantic - -That class `Hero` is a **SQLModel** model. - -But at the same time, ✨ it is a **SQLAlchemy** model ✨. So, you can combine it and use it with other SQLAlchemy models, or you could easily migrate applications with SQLAlchemy to **SQLModel**. - -And at the same time, ✨ it is also a **Pydantic** model ✨. You can use inheritance with it to define all your **data models** while avoiding code duplication. That makes it very easy to use with **FastAPI**. - -## License - -This project is licensed under the terms of the [MIT license](https://github.com/fastapi/sqlmodel/blob/main/LICENSE). diff --git a/docs/install.md b/docs/install.md deleted file mode 100644 index 129df3512b..0000000000 --- a/docs/install.md +++ /dev/null @@ -1,39 +0,0 @@ -# Install **SQLModel** - -Create a project directory, create a [virtual environment](virtual-environments.md){.internal-link target=_blank}, activate it, and then install **SQLModel**, for example with: - -
- -```console -$ pip install sqlmodel ----> 100% -Successfully installed sqlmodel pydantic sqlalchemy -``` - -
- -As **SQLModel** is built on top of SQLAlchemy and Pydantic, when you install `sqlmodel` they will also be automatically installed. - -## Install DB Browser for SQLite - -Remember that [SQLite is a simple database in a single file](databases.md#a-single-file-database){.internal-link target=_blank}? - -For most of the tutorial I'll use SQLite for the examples. - -Python has integrated support for SQLite, it is a single file read and processed from Python. And it doesn't need an [External Database Server](databases.md#a-server-database){.internal-link target=_blank}, so it will be perfect for learning. - -In fact, SQLite is perfectly capable of handling quite big applications. At some point you might want to migrate to a server-based database like PostgreSQL (which is also free). But for now we'll stick to SQLite. - -Through the tutorial I will show you SQL fragments, and Python examples. And I hope (and expect 🧐) you to actually run them, and verify that the database is working as expected and showing you the same data. - -To be able to explore the SQLite file yourself, independent of Python code (and probably at the same time), I recommend you use DB Browser for SQLite. - -It's a great and simple program to interact with SQLite databases (SQLite files) in a nice user interface. - - - -Go ahead and Install DB Browser for SQLite, it's free. - -## Next Steps - -Okay, let's get going! On the next section we'll start the [Tutorial - User Guide](tutorial/index.md). πŸš€ diff --git a/docs/js/custom.js b/docs/js/custom.js deleted file mode 100644 index ef64c612a9..0000000000 --- a/docs/js/custom.js +++ /dev/null @@ -1,115 +0,0 @@ -function setupTermynal() { - document.querySelectorAll(".use-termynal").forEach(node => { - node.style.display = "block"; - new Termynal(node, { - lineDelay: 500 - }); - }); - const progressLiteralStart = "---> 100%"; - const promptLiteralStart = "$ "; - const customPromptLiteralStart = "# "; - const termynalActivateClass = "termy"; - let termynals = []; - - function createTermynals() { - document - .querySelectorAll(`.${termynalActivateClass} .highlight code`) - .forEach(node => { - const text = node.textContent; - const lines = text.split("\n"); - const useLines = []; - let buffer = []; - function saveBuffer() { - if (buffer.length) { - let isBlankSpace = true; - buffer.forEach(line => { - if (line) { - isBlankSpace = false; - } - }); - dataValue = {}; - if (isBlankSpace) { - dataValue["delay"] = 0; - } - if (buffer[buffer.length - 1] === "") { - // A last single
won't have effect - // so put an additional one - buffer.push(""); - } - const bufferValue = buffer.join("
"); - dataValue["value"] = bufferValue; - useLines.push(dataValue); - buffer = []; - } - } - for (let line of lines) { - if (line === progressLiteralStart) { - saveBuffer(); - useLines.push({ - type: "progress" - }); - } else if (line.startsWith(promptLiteralStart)) { - saveBuffer(); - const value = line.replace(promptLiteralStart, "").trimEnd(); - useLines.push({ - type: "input", - value: value - }); - } else if (line.startsWith("// ")) { - saveBuffer(); - const value = "πŸ’¬ " + line.replace("// ", "").trimEnd(); - useLines.push({ - value: value, - class: "termynal-comment", - delay: 0 - }); - } else if (line.startsWith(customPromptLiteralStart)) { - saveBuffer(); - const promptStart = line.indexOf(promptLiteralStart); - if (promptStart === -1) { - console.error("Custom prompt found but no end delimiter", line) - } - const prompt = line.slice(0, promptStart).replace(customPromptLiteralStart, "") - let value = line.slice(promptStart + promptLiteralStart.length); - useLines.push({ - type: "input", - value: value, - prompt: prompt - }); - } else { - buffer.push(line); - } - } - saveBuffer(); - const div = document.createElement("div"); - node.replaceWith(div); - const termynal = new Termynal(div, { - lineData: useLines, - noInit: true, - lineDelay: 500 - }); - termynals.push(termynal); - }); - } - - function loadVisibleTermynals() { - termynals = termynals.filter(termynal => { - if (termynal.container.getBoundingClientRect().top - innerHeight <= 0) { - termynal.init(); - return false; - } - return true; - }); - } - window.addEventListener("scroll", loadVisibleTermynals); - createTermynals(); - loadVisibleTermynals(); -} - -async function main() { - setupTermynal() -} - -document$.subscribe(() => { - main() -}) diff --git a/docs/js/termynal.js b/docs/js/termynal.js deleted file mode 100644 index 45bb371c83..0000000000 --- a/docs/js/termynal.js +++ /dev/null @@ -1,263 +0,0 @@ -/** - * termynal.js - * A lightweight, modern and extensible animated terminal window, using - * async/await. - * - * @author Ines Montani - * @version 0.0.1 - * @license MIT - */ - -'use strict'; - -/** Generate a terminal widget. */ -class Termynal { - /** - * Construct the widget's settings. - * @param {(string|Node)=} container - Query selector or container element. - * @param {Object=} options - Custom settings. - * @param {string} options.prefix - Prefix to use for data attributes. - * @param {number} options.startDelay - Delay before animation, in ms. - * @param {number} options.typeDelay - Delay between each typed character, in ms. - * @param {number} options.lineDelay - Delay between each line, in ms. - * @param {number} options.progressLength - Number of characters displayed as progress bar. - * @param {string} options.progressChar – Character to use for progress bar, defaults to β–ˆ. - * @param {number} options.progressPercent - Max percent of progress. - * @param {string} options.cursor – Character to use for cursor, defaults to β–‹. - * @param {Object[]} lineData - Dynamically loaded line data objects. - * @param {boolean} options.noInit - Don't initialise the animation. - */ - constructor(container = '#termynal', options = {}) { - this.container = (typeof container === 'string') ? document.querySelector(container) : container; - this.pfx = `data-${options.prefix || 'ty'}`; - this.originalStartDelay = this.startDelay = options.startDelay - || parseFloat(this.container.getAttribute(`${this.pfx}-startDelay`)) || 600; - this.originalTypeDelay = this.typeDelay = options.typeDelay - || parseFloat(this.container.getAttribute(`${this.pfx}-typeDelay`)) || 90; - this.originalLineDelay = this.lineDelay = options.lineDelay - || parseFloat(this.container.getAttribute(`${this.pfx}-lineDelay`)) || 1500; - this.progressLength = options.progressLength - || parseFloat(this.container.getAttribute(`${this.pfx}-progressLength`)) || 40; - this.progressChar = options.progressChar - || this.container.getAttribute(`${this.pfx}-progressChar`) || 'β–ˆ'; - this.progressPercent = options.progressPercent - || parseFloat(this.container.getAttribute(`${this.pfx}-progressPercent`)) || 100; - this.cursor = options.cursor - || this.container.getAttribute(`${this.pfx}-cursor`) || 'β–‹'; - this.lineData = this.lineDataToElements(options.lineData || []); - this.loadLines() - if (!options.noInit) this.init() - } - - loadLines() { - // Load all the lines and create the container so that the size is fixed - // Otherwise it would be changing and the user viewport would be constantly - // moving as she/he scrolls - const finish = this.generateFinish() - finish.style.visibility = 'hidden' - this.container.appendChild(finish) - // Appends dynamically loaded lines to existing line elements. - this.lines = [...this.container.querySelectorAll(`[${this.pfx}]`)].concat(this.lineData); - for (let line of this.lines) { - line.style.visibility = 'hidden' - this.container.appendChild(line) - } - const restart = this.generateRestart() - restart.style.visibility = 'hidden' - this.container.appendChild(restart) - this.container.setAttribute('data-termynal', ''); - } - - /** - * Initialise the widget, get lines, clear container and start animation. - */ - init() { - /** - * Calculates width and height of Termynal container. - * If container is empty and lines are dynamically loaded, defaults to browser `auto` or CSS. - */ - const containerStyle = getComputedStyle(this.container); - this.container.style.width = containerStyle.width !== '0px' ? - containerStyle.width : undefined; - this.container.style.minHeight = containerStyle.height !== '0px' ? - containerStyle.height : undefined; - - this.container.setAttribute('data-termynal', ''); - this.container.innerHTML = ''; - for (let line of this.lines) { - line.style.visibility = 'visible' - } - this.start(); - } - - /** - * Start the animation and rener the lines depending on their data attributes. - */ - async start() { - this.addFinish() - await this._wait(this.startDelay); - - for (let line of this.lines) { - const type = line.getAttribute(this.pfx); - const delay = line.getAttribute(`${this.pfx}-delay`) || this.lineDelay; - - if (type == 'input') { - line.setAttribute(`${this.pfx}-cursor`, this.cursor); - await this.type(line); - await this._wait(delay); - } - - else if (type == 'progress') { - await this.progress(line); - await this._wait(delay); - } - - else { - this.container.appendChild(line); - await this._wait(delay); - } - - line.removeAttribute(`${this.pfx}-cursor`); - } - this.addRestart() - this.finishElement.style.visibility = 'hidden' - this.lineDelay = this.originalLineDelay - this.typeDelay = this.originalTypeDelay - this.startDelay = this.originalStartDelay - } - - generateRestart() { - const restart = document.createElement('a') - restart.onclick = (e) => { - e.preventDefault() - this.container.innerHTML = '' - this.init() - } - restart.href = '#' - restart.setAttribute('data-terminal-control', '') - restart.innerHTML = "restart ↻" - return restart - } - - generateFinish() { - const finish = document.createElement('a') - finish.onclick = (e) => { - e.preventDefault() - this.lineDelay = 0 - this.typeDelay = 0 - this.startDelay = 0 - } - finish.href = '#' - finish.setAttribute('data-terminal-control', '') - finish.innerHTML = "fast β†’" - this.finishElement = finish - return finish - } - - addRestart() { - const restart = this.generateRestart() - this.container.appendChild(restart) - } - - addFinish() { - const finish = this.generateFinish() - this.container.appendChild(finish) - } - - /** - * Animate a typed line. - * @param {Node} line - The line element to render. - */ - async type(line) { - const chars = [...line.textContent]; - line.textContent = ''; - this.container.appendChild(line); - - for (let char of chars) { - const delay = line.getAttribute(`${this.pfx}-typeDelay`) || this.typeDelay; - await this._wait(delay); - line.textContent += char; - } - } - - /** - * Animate a progress bar. - * @param {Node} line - The line element to render. - */ - async progress(line) { - const progressLength = line.getAttribute(`${this.pfx}-progressLength`) - || this.progressLength; - const progressChar = line.getAttribute(`${this.pfx}-progressChar`) - || this.progressChar; - const chars = progressChar.repeat(progressLength); - const progressPercent = line.getAttribute(`${this.pfx}-progressPercent`) - || this.progressPercent; - line.textContent = ''; - this.container.appendChild(line); - - for (let i = 1; i < chars.length + 1; i++) { - await this._wait(this.typeDelay); - const percent = Math.round(i / chars.length * 100); - line.textContent = `${chars.slice(0, i)} ${percent}%`; - if (percent>progressPercent) { - break; - } - } - } - - /** - * Helper function for animation delays, called with `await`. - * @param {number} time - Timeout, in ms. - */ - _wait(time) { - return new Promise(resolve => setTimeout(resolve, time)); - } - - /** - * Converts line data objects into line elements. - * - * @param {Object[]} lineData - Dynamically loaded lines. - * @param {Object} line - Line data object. - * @returns {Element[]} - Array of line elements. - */ - lineDataToElements(lineData) { - return lineData.map(line => { - let div = document.createElement('div'); - div.innerHTML = `${line.value || ''}`; - - return div.firstElementChild; - }); - } - - /** - * Helper function for generating attributes string. - * - * @param {Object} line - Line data object. - * @returns {string} - String of attributes. - */ - _attributes(line) { - let attrs = ''; - for (let prop in line) { - // Custom add class - if (prop === 'class') { - attrs += ` class=${line[prop]} ` - continue - } - if (prop === 'type') { - attrs += `${this.pfx}="${line[prop]}" ` - } else if (prop !== 'value') { - attrs += `${this.pfx}-${prop}="${line[prop]}" ` - } - } - return attrs; - } -} - -/** -* HTML API: If current script has container(s) specified, initialise Termynal. -*/ -if (document.currentScript.hasAttribute('data-termynal-container')) { - const containers = document.currentScript.getAttribute('data-termynal-container'); - containers.split('|') - .forEach(container => new Termynal(container)) -} diff --git a/docs/learn/index.md b/docs/learn/index.md deleted file mode 100644 index bcf8a0b0f5..0000000000 --- a/docs/learn/index.md +++ /dev/null @@ -1,7 +0,0 @@ -# Learn - -Learn how to use **SQLModel** here. - -This includes an introduction to **databases**, **SQL**, how to interact with databases from **code** and more. - -You could consider this a **book**, a **course**, and the **official** recommended way to learn **SQLModel**. 😎 diff --git a/docs/management-tasks.md b/docs/management-tasks.md deleted file mode 100644 index f8deb992f0..0000000000 --- a/docs/management-tasks.md +++ /dev/null @@ -1,115 +0,0 @@ -# Repository Management Tasks - -These are the tasks that can be performed to manage the SQLModel repository by [team members](./management.md#team){.internal-link target=_blank}. - -/// tip - -This section is useful only to a handful of people, team members with permissions to manage the repository. You can probably skip it. πŸ˜‰ - -/// - -...so, you are a [team member of SQLModel](./management.md#team){.internal-link target=_blank}? Wow, you are so cool! 😎 - -You can help with everything on [Help SQLModel - Get Help](./help.md){.internal-link target=_blank} the same ways as external contributors. But additionally, there are some tasks that only you (as part of the team) can perform. - -Here are the general instructions for the tasks you can perform. - -Thanks a lot for your help. πŸ™‡ - -## Be Nice - -First of all, be nice. 😊 - -You probably are super nice if you were added to the team, but it's worth mentioning it. πŸ€“ - -### When Things are Difficult - -When things are great, everything is easier, so that doesn't need much instructions. But when things are difficult, here are some guidelines. - -Try to find the good side. In general, if people are not being unfriendly, try to thank their effort and interest, even if you disagree with the main subject (discussion, PR), just thank them for being interested in the project, or for having dedicated some time to try to do something. - -It's difficult to convey emotion in text, use emojis to help. πŸ˜… - -In discussions and PRs, in many cases, people bring their frustration and show it without filter, in many cases exaggerating, complaining, being entitled, etc. That's really not nice, and when it happens, it lowers our priority to solve their problems. But still, try to breath, and be gentle with your answers. - -Try to avoid using bitter sarcasm or potentially passive-aggressive comments. If something is wrong, it's better to be direct (try to be gentle) than sarcastic. - -Try to be as specific and objective as possible, avoid generalizations. - -For conversations that are more difficult, for example to reject a PR, you can ask me (@tiangolo) to handle it directly. - -## Edit PR Titles - -* Edit the PR title to start with an emoji from gitmoji. - * Use the emoji character, not the GitHub code. So, use `πŸ›` instead of `:bug:`. This is so that it shows up correctly outside of GitHub, for example in the release notes. -* Start the title with a verb. For example `Add`, `Refactor`, `Fix`, etc. This way the title will say the action that the PR does. Like `Add support for teleporting`, instead of `Teleporting wasn't working, so this PR fixes it`. -* Edit the text of the PR title to start in "imperative", like giving an order. So, instead of `Adding support for teleporting` use `Add support for teleporting`. -* Try to make the title descriptive about what it achieves. If it's a feature, try to describe it, for example `Add support for teleporting` instead of `Create TeleportAdapter class`. -* Do not finish the title with a period (`.`). - -Once the PR is merged, a GitHub Action (latest-changes) will use the PR title to update the latest changes automatically. - -So, having a nice PR title will not only look nice in GitHub, but also in the release notes. πŸ“ - -## Add Labels to PRs - -The same GitHub Action latest-changes uses one label in the PR to decide the section in the release notes to put this PR in. - -Make sure you use a supported label from the latest-changes list of labels: - -* `breaking`: Breaking Changes - * Existing code will break if they update the version without changing their code. This rarely happens, so this label is not frequently used. -* `security`: Security Fixes - * This is for security fixes, like vulnerabilities. It would almost never be used. -* `feature`: Features - * New features, adding support for things that didn't exist before. -* `bug`: Fixes - * Something that was supported didn't work, and this fixes it. There are many PRs that claim to be bug fixes because the user is doing something in an unexpected way that is not supported, but they considered it what should be supported by default. Many of these are actually features or refactors. But in some cases there's an actual bug. -* `refactor`: Refactors - * This is normally for changes to the internal code that don't change the behavior. Normally it improves maintainability, or enables future features, etc. -* `upgrade`: Upgrades - * This is for upgrades to direct dependencies from the project, or extra optional dependencies, normally in `pyproject.toml`. So, things that would affect final users, they would end up receiving the upgrade in their code base once they update. But this is not for upgrades to internal dependencies used for development, testing, docs, etc. Those internal dependencies, normally in `requirements.txt` files or GitHub Action versions should be marked as `internal`, not `upgrade`. -* `docs`: Docs - * Changes in docs. This includes updating the docs, fixing typos. But it doesn't include changes to translations. - * You can normally quickly detect it by going to the "Files changed" tab in the PR and checking if the updated file(s) starts with `docs/en/docs`. The original version of the docs is always in English, so in `docs/en/docs`. -* `internal`: Internal - * Use this for changes that only affect how the repo is managed. For example upgrades to internal dependencies, changes in GitHub Actions or scripts, etc. - -/// tip - -Some tools like Dependabot, will add some labels, like `dependencies`, but have in mind that this label is not used by the `latest-changes` GitHub Action, so it won't be used in the release notes. Please make sure one of the labels above is added. - -/// - -## Review PRs - -If a PR doesn't explain what it does or why, ask for more information. - -A PR should have a specific use case that it is solving. - -* If the PR is for a feature, it should have docs. - * Unless it's a feature we want to discourage, like support for a corner case that we don't want users to use. -* The docs should include a source example file, not write Python directly in Markdown. -* If the source example(s) file can have different syntax for Python 3.8, 3.9, 3.10, there should be different versions of the file, and they should be shown in tabs in the docs. -* There should be tests testing the source example. -* Before the PR is applied, the new tests should fail. -* After applying the PR, the new tests should pass. -* Coverage should stay at 100%. -* If you see the PR makes sense, or we discussed it and considered it should be accepted, you can add commits on top of the PR to tweak it, to add docs, tests, format, refactor, remove extra files, etc. -* Feel free to comment in the PR to ask for more information, to suggest changes, etc. -* Once you think the PR is ready, move it in the internal GitHub project for me to review it. - -## Dependabot PRs - -Dependabot will create PRs to update dependencies for several things, and those PRs all look similar, but some are way more delicate than others. - -* If the PR is for a direct dependency, so, Dependabot is modifying `pyproject.toml`, **don't merge it**. 😱 Let me check it first. There's a good chance that some additional tweaks or updates are needed. -* If the PR updates one of the internal dependencies, for example it's modifying `requirements.txt` files, or GitHub Action versions, if the tests are passing, the release notes (shown in a summary in the PR) don't show any obvious potential breaking change, you can merge it. 😎 - -## Mark GitHub Discussions Answers - -When a question in GitHub Discussions has been answered, mark the answer by clicking "Mark as answer". - -Many of the current Discussion Questions were migrated from old issues. Many have the label `answered`, that means they were answered when they were issues, but now in GitHub Discussions, it's not known what is the actual response from the messages. - -You can filter discussions by `Questions` that are `Unanswered`. diff --git a/docs/management.md b/docs/management.md deleted file mode 100644 index 0078e364c3..0000000000 --- a/docs/management.md +++ /dev/null @@ -1,45 +0,0 @@ -# Repository Management - -Here's a short description of how the SQLModel repository is managed and maintained. - -## Owner - -I, @tiangolo, am the creator and owner of the SQLModel repository. πŸ€“ - -I normally give the final review to each PR before merging them. I make the final decisions on the project, I'm the BDFL. πŸ˜… - -## Team - -There's a team of people that help manage and maintain the project. 😎 - -They have different levels of permissions and [specific instructions](./management-tasks.md){.internal-link target=_blank}. - -Some of the tasks they can perform include: - -* Adding labels to PRs. -* Editing PR titles. -* Adding commits on top of PRs to tweak them. -* Mark answers in GitHub Discussions questions, etc. -* Merge some specific types of PRs. - -Joining the team is by invitation only, and I could update or remove permissions, instructions, or membership. - -### Team Members - -This is the current list of team members. 😎 - -
-{% for user in members["members"] %} - - -{% endfor %} - -
- -Additional to them, there's a large community of people helping each other and getting involved in the projects in different ways. - -## External Contributions - -External contributions are very welcome and appreciated, including answering questions, submitting PRs, etc. πŸ™‡β€β™‚οΈ - -There are many ways to [help maintain SQLModel](./help.md){.internal-link target=_blank}. diff --git a/docs/overrides/main.html b/docs/overrides/main.html deleted file mode 100644 index 94d9808cc7..0000000000 --- a/docs/overrides/main.html +++ /dev/null @@ -1 +0,0 @@ -{% extends "base.html" %} diff --git a/docs/release-notes.md b/docs/release-notes.md deleted file mode 100644 index 4f4be12503..0000000000 --- a/docs/release-notes.md +++ /dev/null @@ -1,708 +0,0 @@ -# Release Notes - -## Latest Changes - -### Docs - -* πŸ“ Fix broken links in docs. PR [#1601](https://github.com/fastapi/sqlmodel/pull/1601) by [@YuriiMotov](https://github.com/YuriiMotov). - -### Internal - -* ⬆ Bump ruff from 0.14.0 to 0.14.1. PR [#1614](https://github.com/fastapi/sqlmodel/pull/1614) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump ruff from 0.13.2 to 0.14.0. PR [#1592](https://github.com/fastapi/sqlmodel/pull/1592) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1605](https://github.com/fastapi/sqlmodel/pull/1605) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump astral-sh/setup-uv from 6 to 7. PR [#1593](https://github.com/fastapi/sqlmodel/pull/1593) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump mkdocs-material from 9.6.21 to 9.6.22. PR [#1608](https://github.com/fastapi/sqlmodel/pull/1608) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ”§ Configure reminder for `waiting` label in `issue-manager`. PR [#1609](https://github.com/fastapi/sqlmodel/pull/1609) by [@YuriiMotov](https://github.com/YuriiMotov). -* ⬆ Bump typer from 0.19.2 to 0.20.0. PR [#1612](https://github.com/fastapi/sqlmodel/pull/1612) by [@dependabot[bot]](https://github.com/apps/dependabot). -* βœ… Remove unused type ignores since SQLAlchemy 2.0.44. PR [#1613](https://github.com/fastapi/sqlmodel/pull/1613) by [@svlandeg](https://github.com/svlandeg). - -## 0.0.27 - -### Upgrades - -* ⬆️ Add support for Python 3.14. PR [#1578](https://github.com/fastapi/sqlmodel/pull/1578) by [@svlandeg](https://github.com/svlandeg). - -## 0.0.26 - -### Fixes - -* πŸ› Fix attribute handling in `model_dump` for compatibility with the latest Pydantic versions. PR [#1595](https://github.com/fastapi/sqlmodel/pull/1595) by [@spazm](https://github.com/spazm). - -### Docs - -* πŸ“ Fix typo in `docs/tutorial/fastapi/simple-hero-api.md`. PR [#1583](https://github.com/fastapi/sqlmodel/pull/1583) by [@kofi-kusi](https://github.com/kofi-kusi). - -### Internal - -* ⬆ Bump mypy from 1.4.1 to 1.18.2. PR [#1560](https://github.com/fastapi/sqlmodel/pull/1560) by [@dependabot[bot]](https://github.com/apps/dependabot). -* βœ… Add test that runs select with 3 or 4 arguments. PR [#1590](https://github.com/fastapi/sqlmodel/pull/1590) by [@svlandeg](https://github.com/svlandeg). -* ⬆ Bump mkdocs-macros-plugin from 1.3.9 to 1.4.0. PR [#1581](https://github.com/fastapi/sqlmodel/pull/1581) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump mkdocs-material from 9.6.20 to 9.6.21. PR [#1588](https://github.com/fastapi/sqlmodel/pull/1588) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1584](https://github.com/fastapi/sqlmodel/pull/1584) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump tiangolo/issue-manager from 0.5.1 to 0.6.0. PR [#1589](https://github.com/fastapi/sqlmodel/pull/1589) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ‘· Update docs previews comment, single comment, add failure status. PR [#1586](https://github.com/fastapi/sqlmodel/pull/1586) by [@tiangolo](https://github.com/tiangolo). -* ⬆ Bump markdown-include-variants from 0.0.4 to 0.0.5. PR [#1582](https://github.com/fastapi/sqlmodel/pull/1582) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump typing-extensions from 4.13.2 to 4.15.0 for Python 3.9+. PR [#1580](https://github.com/fastapi/sqlmodel/pull/1580) by [@svlandeg](https://github.com/svlandeg). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1571](https://github.com/fastapi/sqlmodel/pull/1571) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump typer from 0.17.4 to 0.19.2. PR [#1573](https://github.com/fastapi/sqlmodel/pull/1573) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump ruff from 0.13.0 to 0.13.2. PR [#1576](https://github.com/fastapi/sqlmodel/pull/1576) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ’š Fix CI test suite for Windows and MacOS. PR [#1307](https://github.com/fastapi/sqlmodel/pull/1307) by [@svlandeg](https://github.com/svlandeg). - -## 0.0.25 - -### Features - -* ✨ Add overload for `exec` method to support `insert`, `update`, `delete` statements. PR [#1342](https://github.com/fastapi/sqlmodel/pull/1342) by [@seriaati](https://github.com/seriaati). - -### Upgrades - -* ⬆️ Drop support for Python 3.7, require Python 3.8 or above. PR [#1316](https://github.com/fastapi/sqlmodel/pull/1316) by [@svlandeg](https://github.com/svlandeg). - -### Docs - -* ✏️ Fix typos in `docs/tutorial/relationship-attributes/cascade-delete-relationships.md`. PR [#1543](https://github.com/fastapi/sqlmodel/pull/1543) by [@YuriiMotov](https://github.com/YuriiMotov). -* 🍱 Update SVG files, a single file per diagram, sans-serif fonts. PR [#1373](https://github.com/fastapi/sqlmodel/pull/1373) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Grammar tweak in `docs/tutorial/insert.md`. PR [#1368](https://github.com/fastapi/sqlmodel/pull/1368) by [@brettcannon](https://github.com/brettcannon). -* πŸ“ Update `docs/tutorial/fastapi/relationships.md`. PR [#1365](https://github.com/fastapi/sqlmodel/pull/1365) by [@Foxerine](https://github.com/Foxerine). -* ✏️ Tweak the grammar in `docs/learn/index.md`. PR [#1363](https://github.com/fastapi/sqlmodel/pull/1363) by [@brettcannon](https://github.com/brettcannon). -* πŸ“ Update all docs references to `Optional` to use the new syntax in Python 3.10, e.g. `int | None`. PR [#1351](https://github.com/fastapi/sqlmodel/pull/1351) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Update install and usage with FastAPI CLI in FastAPI tutorial. PR [#1350](https://github.com/fastapi/sqlmodel/pull/1350) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Update FastAPI tutorial docs to use the new `model.sqlmodel_update()` instead of old `setattr()`. PR [#1117](https://github.com/fastapi/sqlmodel/pull/1117) by [@jpizquierdo](https://github.com/jpizquierdo). -* ✏️ Update `docs/virtual-environments.md`. PR [#1321](https://github.com/fastapi/sqlmodel/pull/1321) by [@sylvainHellin](https://github.com/sylvainHellin). - -### Internal - -* ⬆ Bump griffe-typingdoc from 0.2.8 to 0.2.9. PR [#1553](https://github.com/fastapi/sqlmodel/pull/1553) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump mkdocs-material from 9.6.17 to 9.6.20. PR [#1565](https://github.com/fastapi/sqlmodel/pull/1565) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump actions/setup-python from 5 to 6. PR [#1551](https://github.com/fastapi/sqlmodel/pull/1551) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump ruff from 0.12.12 to 0.13.0. PR [#1559](https://github.com/fastapi/sqlmodel/pull/1559) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1564](https://github.com/fastapi/sqlmodel/pull/1564) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump actions/labeler from 5 to 6. PR [#1549](https://github.com/fastapi/sqlmodel/pull/1549) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1556](https://github.com/fastapi/sqlmodel/pull/1556) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump typer from 0.17.3 to 0.17.4. PR [#1554](https://github.com/fastapi/sqlmodel/pull/1554) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1546](https://github.com/fastapi/sqlmodel/pull/1546) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump ruff from 0.12.10 to 0.12.12. PR [#1548](https://github.com/fastapi/sqlmodel/pull/1548) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump typer from 0.16.1 to 0.17.3. PR [#1547](https://github.com/fastapi/sqlmodel/pull/1547) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump pypa/gh-action-pypi-publish from 1.12.4 to 1.13.0. PR [#1550](https://github.com/fastapi/sqlmodel/pull/1550) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ‘· Detect and label merge conflicts on PRs automatically. PR [#1552](https://github.com/fastapi/sqlmodel/pull/1552) by [@svlandeg](https://github.com/svlandeg). -* ⬆ Bump ruff from 0.12.9 to 0.12.10. PR [#1532](https://github.com/fastapi/sqlmodel/pull/1532) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1534](https://github.com/fastapi/sqlmodel/pull/1534) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump typer from 0.16.0 to 0.16.1. PR [#1531](https://github.com/fastapi/sqlmodel/pull/1531) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump actions/download-artifact from 4 to 5. PR [#1451](https://github.com/fastapi/sqlmodel/pull/1451) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump actions/checkout from 4 to 5. PR [#1488](https://github.com/fastapi/sqlmodel/pull/1488) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1479](https://github.com/fastapi/sqlmodel/pull/1479) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump mkdocs-macros-plugin from 1.3.7 to 1.3.9. PR [#1507](https://github.com/fastapi/sqlmodel/pull/1507) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump ruff from 0.12.7 to 0.12.9. PR [#1521](https://github.com/fastapi/sqlmodel/pull/1521) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump mkdocs-material from 9.6.16 to 9.6.17. PR [#1528](https://github.com/fastapi/sqlmodel/pull/1528) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1444](https://github.com/fastapi/sqlmodel/pull/1444) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump mkdocs-material from 9.6.15 to 9.6.16. PR [#1446](https://github.com/fastapi/sqlmodel/pull/1446) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump ruff from 0.12.4 to 0.12.7. PR [#1447](https://github.com/fastapi/sqlmodel/pull/1447) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump tiangolo/latest-changes from 0.3.2 to 0.4.0. PR [#1448](https://github.com/fastapi/sqlmodel/pull/1448) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1437](https://github.com/fastapi/sqlmodel/pull/1437) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump ruff from 0.12.3 to 0.12.4. PR [#1436](https://github.com/fastapi/sqlmodel/pull/1436) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1428](https://github.com/fastapi/sqlmodel/pull/1428) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump ruff from 0.12.2 to 0.12.3. PR [#1432](https://github.com/fastapi/sqlmodel/pull/1432) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1418](https://github.com/fastapi/sqlmodel/pull/1418) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump pillow from 11.2.1 to 11.3.0. PR [#1423](https://github.com/fastapi/sqlmodel/pull/1423) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump mkdocs-material from 9.6.14 to 9.6.15. PR [#1424](https://github.com/fastapi/sqlmodel/pull/1424) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump ruff from 0.12.0 to 0.12.2. PR [#1425](https://github.com/fastapi/sqlmodel/pull/1425) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1374](https://github.com/fastapi/sqlmodel/pull/1374) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump ruff from 0.11.13 to 0.12.0. PR [#1403](https://github.com/fastapi/sqlmodel/pull/1403) by [@dependabot[bot]](https://github.com/apps/dependabot). -* βœ… Simplify tests for `tests/test_tutorial/test_code_structure/test_tutorial001.py`, one test file for multiple variants. PR [#1408](https://github.com/fastapi/sqlmodel/pull/1408) by [@tiangolo](https://github.com/tiangolo). -* βœ… Simplify tests setup, one test file for multiple source variants. PR [#1407](https://github.com/fastapi/sqlmodel/pull/1407) by [@tiangolo](https://github.com/tiangolo). -* βœ… Refactor tests to use autouse `clear_sqlmodel`. PR [#1406](https://github.com/fastapi/sqlmodel/pull/1406) by [@tiangolo](https://github.com/tiangolo). -* ⬆ Bump mkdocs-material from 9.5.18 to 9.6.14. PR [#1378](https://github.com/fastapi/sqlmodel/pull/1378) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump typer from 0.15.3 to 0.16.0. PR [#1393](https://github.com/fastapi/sqlmodel/pull/1393) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump cairosvg from 2.7.1 to 2.8.2. PR [#1383](https://github.com/fastapi/sqlmodel/pull/1383) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump ruff from 0.11.7 to 0.11.13. PR [#1397](https://github.com/fastapi/sqlmodel/pull/1397) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ”§ Remove Google Analytics. PR [#1386](https://github.com/fastapi/sqlmodel/pull/1386) by [@tiangolo](https://github.com/tiangolo). -* ⬆ Bump mkdocs-macros-plugin from 1.0.5 to 1.3.7. PR [#1354](https://github.com/fastapi/sqlmodel/pull/1354) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump griffe-typingdoc from 0.2.5 to 0.2.8. PR [#1359](https://github.com/fastapi/sqlmodel/pull/1359) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Update pre-commit requirement from <4.0.0,>=2.17.0 to >=2.17.0,<5.0.0. PR [#1360](https://github.com/fastapi/sqlmodel/pull/1360) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump pillow from 11.0.0 to 11.2.1. PR [#1361](https://github.com/fastapi/sqlmodel/pull/1361) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1367](https://github.com/fastapi/sqlmodel/pull/1367) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump ruff from 0.9.6 to 0.11.7. PR [#1355](https://github.com/fastapi/sqlmodel/pull/1355) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1353](https://github.com/fastapi/sqlmodel/pull/1353) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump typing-extensions from 4.12.2 to 4.13.2. PR [#1356](https://github.com/fastapi/sqlmodel/pull/1356) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump typer from 0.15.2 to 0.15.3. PR [#1357](https://github.com/fastapi/sqlmodel/pull/1357) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1339](https://github.com/fastapi/sqlmodel/pull/1339) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump typer from 0.12.3 to 0.15.2. PR [#1325](https://github.com/fastapi/sqlmodel/pull/1325) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump httpx from 0.24.1 to 0.28.1. PR [#1238](https://github.com/fastapi/sqlmodel/pull/1238) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump astral-sh/setup-uv from 5 to 6. PR [#1348](https://github.com/fastapi/sqlmodel/pull/1348) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Update pytest requirement from <8.0.0,>=7.0.1 to >=7.0.1,<9.0.0. PR [#1022](https://github.com/fastapi/sqlmodel/pull/1022) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ♻️ Update `tests/test_select_gen.py`, pass environment variables, needed for NixOS nixpkgs. PR [#969](https://github.com/fastapi/sqlmodel/pull/969) by [@pbsds](https://github.com/pbsds). -* πŸ’š Fix linting in CI. PR [#1340](https://github.com/fastapi/sqlmodel/pull/1340) by [@svlandeg](https://github.com/svlandeg). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1327](https://github.com/fastapi/sqlmodel/pull/1327) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump jinja2 from 3.1.4 to 3.1.6. PR [#1317](https://github.com/fastapi/sqlmodel/pull/1317) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1319](https://github.com/fastapi/sqlmodel/pull/1319) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). - -## 0.0.24 - -### Upgrades - -* ⬆️ Add support for Python 3.13. PR [#1289](https://github.com/fastapi/sqlmodel/pull/1289) by [@svlandeg](https://github.com/svlandeg). - -### Internal - -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1114](https://github.com/fastapi/sqlmodel/pull/1114) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump ruff from 0.6.2 to 0.9.6. PR [#1294](https://github.com/fastapi/sqlmodel/pull/1294) by [@dependabot[bot]](https://github.com/apps/dependabot). - -## 0.0.23 - -### Fixes - -* πŸ› Fix type annotation in `Field` constructor. PR [#1304](https://github.com/fastapi/sqlmodel/pull/1304) by [@AlanBogarin](https://github.com/AlanBogarin). -* πŸ› Fix Pydantic version check for version 2.10.x onwards. PR [#1255](https://github.com/fastapi/sqlmodel/pull/1255) by [@asiunov](https://github.com/asiunov). - -### Refactors - -* 🚨 Fix types for new Pydantic. PR [#1131](https://github.com/fastapi/sqlmodel/pull/1131) by [@tiangolo](https://github.com/tiangolo). - -### Docs - -* 🩺 Take the GH badge only from pushes to the `main` branch. PR [#1291](https://github.com/fastapi/sqlmodel/pull/1291) by [@svlandeg](https://github.com/svlandeg). -* πŸ“ Update documentation to refer to `list` instead of `List`. PR [#1147](https://github.com/fastapi/sqlmodel/pull/1147) by [@bubbletroubles](https://github.com/bubbletroubles). -* ✏️ Fix typo in `databases.md`. PR [#1113](https://github.com/fastapi/sqlmodel/pull/1113) by [@radi-dev](https://github.com/radi-dev). -* ✏️ Fix typo in `docs/tutorial/create-db-and-table.md`. PR [#1252](https://github.com/fastapi/sqlmodel/pull/1252) by [@ArianHamdi](https://github.com/ArianHamdi). -* ✏️ Fix typo in `insert.md`. PR [#1256](https://github.com/fastapi/sqlmodel/pull/1256) by [@Noushadaliam](https://github.com/Noushadaliam). -* πŸ“ Update markdown includes format. PR [#1254](https://github.com/fastapi/sqlmodel/pull/1254) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Update fenced code in Decimal docs for consistency. PR [#1251](https://github.com/fastapi/sqlmodel/pull/1251) by [@tiangolo](https://github.com/tiangolo). -* ✏️ Fix typo in the release notes of v0.0.22. PR [#1195](https://github.com/fastapi/sqlmodel/pull/1195) by [@PipeKnight](https://github.com/PipeKnight). -* πŸ“ Update includes for `docs/advanced/uuid.md`. PR [#1151](https://github.com/fastapi/sqlmodel/pull/1151) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Update includes for `docs/tutorial/create-db-and-table.md`. PR [#1149](https://github.com/fastapi/sqlmodel/pull/1149) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Fix internal links in docs. PR [#1148](https://github.com/fastapi/sqlmodel/pull/1148) by [@tiangolo](https://github.com/tiangolo). -* ✏️ Fix typo in documentation. PR [#1106](https://github.com/fastapi/sqlmodel/pull/1106) by [@Solipsistmonkey](https://github.com/Solipsistmonkey). -* πŸ“ Remove highlights in `indexes.md` . PR [#1100](https://github.com/fastapi/sqlmodel/pull/1100) by [@alejsdev](https://github.com/alejsdev). - -### Internal - -* ⬆ Bump pypa/gh-action-pypi-publish from 1.12.3 to 1.12.4. PR [#1277](https://github.com/fastapi/sqlmodel/pull/1277) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ’š Fix CI test suite for Python 3.7. PR [#1309](https://github.com/fastapi/sqlmodel/pull/1309) by [@svlandeg](https://github.com/svlandeg). -* πŸ‘· Revert "Add Codecov to CI, Smokeshow/Cloudflare has been flaky lately (#1303)". PR [#1306](https://github.com/fastapi/sqlmodel/pull/1306) by [@svlandeg](https://github.com/svlandeg). -* πŸ‘· Add Codecov to CI, Smokeshow/Cloudflare has been flaky lately. PR [#1303](https://github.com/fastapi/sqlmodel/pull/1303) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Add retries to Smokeshow. PR [#1302](https://github.com/fastapi/sqlmodel/pull/1302) by [@svlandeg](https://github.com/svlandeg). -* ⬆ Bump astral-sh/setup-uv from 4 to 5. PR [#1249](https://github.com/fastapi/sqlmodel/pull/1249) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump pillow from 10.3.0 to 11.0.0. PR [#1139](https://github.com/fastapi/sqlmodel/pull/1139) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump pypa/gh-action-pypi-publish from 1.9.0 to 1.12.3. PR [#1240](https://github.com/fastapi/sqlmodel/pull/1240) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump astral-sh/setup-uv from 3 to 4. PR [#1225](https://github.com/fastapi/sqlmodel/pull/1225) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump tiangolo/latest-changes from 0.3.1 to 0.3.2. PR [#1207](https://github.com/fastapi/sqlmodel/pull/1207) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ”¨ Update docs previews script. PR [#1236](https://github.com/fastapi/sqlmodel/pull/1236) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Update build-docs filter paths. PR [#1235](https://github.com/fastapi/sqlmodel/pull/1235) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Update team members. PR [#1234](https://github.com/fastapi/sqlmodel/pull/1234) by [@tiangolo](https://github.com/tiangolo). -* ⬆️ Upgrade markdown-include-variants to version 0.0.3. PR [#1152](https://github.com/fastapi/sqlmodel/pull/1152) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update issue manager workflow. PR [#1137](https://github.com/fastapi/sqlmodel/pull/1137) by [@alejsdev](https://github.com/alejsdev). -* πŸ‘· Fix smokeshow, checkout files on CI. PR [#1136](https://github.com/fastapi/sqlmodel/pull/1136) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Use uv in CI. PR [#1135](https://github.com/fastapi/sqlmodel/pull/1135) by [@tiangolo](https://github.com/tiangolo). -* βž• Add docs dependency markdown-include-variants. PR [#1129](https://github.com/fastapi/sqlmodel/pull/1129) by [@tiangolo](https://github.com/tiangolo). -* πŸ”¨ Update script to standardize format. PR [#1130](https://github.com/fastapi/sqlmodel/pull/1130) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update `labeler.yml`. PR [#1128](https://github.com/fastapi/sqlmodel/pull/1128) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update worfkow deploy-docs-notify URL. PR [#1126](https://github.com/fastapi/sqlmodel/pull/1126) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Upgrade Cloudflare GitHub Action. PR [#1124](https://github.com/fastapi/sqlmodel/pull/1124) by [@tiangolo](https://github.com/tiangolo). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1097](https://github.com/fastapi/sqlmodel/pull/1097) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump tiangolo/issue-manager from 0.5.0 to 0.5.1. PR [#1107](https://github.com/fastapi/sqlmodel/pull/1107) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ‘· Update `issue-manager.yml`. PR [#1103](https://github.com/fastapi/sqlmodel/pull/1103) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Fix coverage processing in CI, one name per matrix run. PR [#1104](https://github.com/fastapi/sqlmodel/pull/1104) by [@tiangolo](https://github.com/tiangolo). -* πŸ’š Set `include-hidden-files` to `True` when using the `upload-artifact` GH action. PR [#1098](https://github.com/fastapi/sqlmodel/pull/1098) by [@svlandeg](https://github.com/svlandeg). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1088](https://github.com/fastapi/sqlmodel/pull/1088) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). - -## 0.0.22 - -### Fixes - -* πŸ› Fix support for types with `Optional[Annotated[x, f()]]`, e.g. `id: Optional[pydantic.UUID4]`. PR [#1093](https://github.com/fastapi/sqlmodel/pull/1093) by [@tiangolo](https://github.com/tiangolo). - -### Docs - -* ✏️ Fix a typo in `docs/virtual-environments.md`. PR [#1085](https://github.com/fastapi/sqlmodel/pull/1085) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Add docs for virtual environments and environment variables, update contributing. PR [#1082](https://github.com/fastapi/sqlmodel/pull/1082) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Add docs about repo management and team. PR [#1059](https://github.com/tiangolo/sqlmodel/pull/1059) by [@tiangolo](https://github.com/tiangolo). -* ✏️ Fix typo in `cascade_delete` docs. PR [#1030](https://github.com/tiangolo/sqlmodel/pull/1030) by [@tiangolo](https://github.com/tiangolo). - -### Internal - -* βœ… Refactor test_enums to make them independent of previous imports. PR [#1095](https://github.com/fastapi/sqlmodel/pull/1095) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update `latest-changes` GitHub Action. PR [#1087](https://github.com/fastapi/sqlmodel/pull/1087) by [@tiangolo](https://github.com/tiangolo). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#1028](https://github.com/fastapi/sqlmodel/pull/1028) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump ruff from 0.4.7 to 0.6.2. PR [#1081](https://github.com/fastapi/sqlmodel/pull/1081) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ”§ Update lint script. PR [#1084](https://github.com/fastapi/sqlmodel/pull/1084) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update Python version for coverage. PR [#1083](https://github.com/fastapi/sqlmodel/pull/1083) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Update coverage config files. PR [#1077](https://github.com/fastapi/sqlmodel/pull/1077) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Add URLs to `pyproject.toml`, show up in PyPI. PR [#1074](https://github.com/fastapi/sqlmodel/pull/1074) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Do not sync labels as it overrides manually added labels. PR [#1073](https://github.com/fastapi/sqlmodel/pull/1073) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update configs for GitHub Action labeler, to add only one label. PR [#1072](https://github.com/fastapi/sqlmodel/pull/1072) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update labeler GitHub Actions permissions and dependencies. PR [#1071](https://github.com/fastapi/sqlmodel/pull/1071) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Add GitHub Action label-checker. PR [#1069](https://github.com/fastapi/sqlmodel/pull/1069) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Add GitHub Action labeler. PR [#1068](https://github.com/fastapi/sqlmodel/pull/1068) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update GitHub Action add-to-project. PR [#1067](https://github.com/fastapi/sqlmodel/pull/1067) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Add GitHub Action add-to-project. PR [#1066](https://github.com/fastapi/sqlmodel/pull/1066) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Update admonitions in annotations. PR [#1065](https://github.com/fastapi/sqlmodel/pull/1065) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Update links from github.com/tiangolo/sqlmodel to github.com/fastapi/sqlmodel. PR [#1064](https://github.com/fastapi/sqlmodel/pull/1064) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Update members. PR [#1063](https://github.com/tiangolo/sqlmodel/pull/1063) by [@tiangolo](https://github.com/tiangolo). -* πŸ’„ Add dark-mode logo. PR [#1061](https://github.com/tiangolo/sqlmodel/pull/1061) by [@tiangolo](https://github.com/tiangolo). -* πŸ”¨ Update docs.py script to enable dirty reload conditionally. PR [#1060](https://github.com/tiangolo/sqlmodel/pull/1060) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Update MkDocs previews. PR [#1058](https://github.com/tiangolo/sqlmodel/pull/1058) by [@tiangolo](https://github.com/tiangolo). -* πŸ’„ Update Termynal line-height. PR [#1057](https://github.com/tiangolo/sqlmodel/pull/1057) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Upgrade build docs configs. PR [#1047](https://github.com/tiangolo/sqlmodel/pull/1047) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Add alls-green for test-redistribute. PR [#1055](https://github.com/tiangolo/sqlmodel/pull/1055) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update docs-previews to handle no docs changes. PR [#1056](https://github.com/tiangolo/sqlmodel/pull/1056) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘·πŸ» Show docs deployment status and preview URLs in comment. PR [#1054](https://github.com/tiangolo/sqlmodel/pull/1054) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Enable auto dark mode. PR [#1046](https://github.com/tiangolo/sqlmodel/pull/1046) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update issue-manager. PR [#1045](https://github.com/tiangolo/sqlmodel/pull/1045) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update issue-manager.yml GitHub Action permissions. PR [#1040](https://github.com/tiangolo/sqlmodel/pull/1040) by [@tiangolo](https://github.com/tiangolo). -* ♻️ Refactor Deploy Docs GitHub Action to be a script and update token preparing for org. PR [#1039](https://github.com/tiangolo/sqlmodel/pull/1039) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.21 - -### Features - -* ✨ Add support for cascade delete relationships: `cascade_delete`, `ondelete`, and `passive_deletes`. Initial PR [#983](https://github.com/tiangolo/sqlmodel/pull/983) by [@estebanx64](https://github.com/estebanx64). - * New docs at: [Cascade Delete Relationships](https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/cascade-delete-relationships/). - -### Docs - -* πŸ“ Update docs . PR [#1003](https://github.com/tiangolo/sqlmodel/pull/1003) by [@alejsdev](https://github.com/alejsdev). - -### Internal - -* ⬆ Bump actions/cache from 3 to 4. PR [#783](https://github.com/tiangolo/sqlmodel/pull/783) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump cairosvg from 2.7.0 to 2.7.1. PR [#919](https://github.com/tiangolo/sqlmodel/pull/919) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump jinja2 from 3.1.3 to 3.1.4. PR [#974](https://github.com/tiangolo/sqlmodel/pull/974) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump pypa/gh-action-pypi-publish from 1.8.11 to 1.9.0. PR [#987](https://github.com/tiangolo/sqlmodel/pull/987) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump mkdocstrings[python] from 0.23.0 to 0.25.1. PR [#927](https://github.com/tiangolo/sqlmodel/pull/927) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump dorny/paths-filter from 2 to 3. PR [#972](https://github.com/tiangolo/sqlmodel/pull/972) by [@dependabot[bot]](https://github.com/apps/dependabot). - -## 0.0.20 - -### Features - -* ✨ Add official UUID support, docs and tests, internally using new SQLAlchemy 2.0 types. Initial PR [#992](https://github.com/tiangolo/sqlmodel/pull/992) by [@estebanx64](https://github.com/estebanx64). - * New docs in the [Advanced User Guide: UUID (Universally Unique Identifiers)](https://sqlmodel.tiangolo.com/advanced/uuid/). - -### Docs - -* ✏️ Fix internal link in `docs/tutorial/create-db-and-table.md`. PR [#911](https://github.com/tiangolo/sqlmodel/pull/911) by [@tfpgh](https://github.com/tfpgh). -* ✏️ Add missing step in `create-db-and-table-with-db-browser.md`. PR [#976](https://github.com/tiangolo/sqlmodel/pull/976) by [@alejsdev](https://github.com/alejsdev). -* ✏️ Fix typo in `docs/tutorial`. PR [#943](https://github.com/tiangolo/sqlmodel/pull/943) by [@luco17](https://github.com/luco17). -* ✏️ Fix typo in `sqlmodel/_compat.py`. PR [#950](https://github.com/tiangolo/sqlmodel/pull/950) by [@Highfire1](https://github.com/Highfire1). -* ✏️ Update pip installation command in tutorial. PR [#975](https://github.com/tiangolo/sqlmodel/pull/975) by [@alejsdev](https://github.com/alejsdev). -* ✏️ Fix typo in `docs/tutorial/relationship-attributes/index.md`. PR [#880](https://github.com/tiangolo/sqlmodel/pull/880) by [@UncleGoogle](https://github.com/UncleGoogle). - -### Internal - -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#979](https://github.com/tiangolo/sqlmodel/pull/979) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* πŸ”¨ Update docs Termynal scripts to not include line nums for local dev. PR [#1018](https://github.com/tiangolo/sqlmodel/pull/1018) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.19 - -### Fixes - -* πŸ› Fix pydantic `EmailStr` support and `max_length` in several String subclasses. PR [#966](https://github.com/tiangolo/sqlmodel/pull/966) by [@estebanx64](https://github.com/estebanx64). -* πŸ› Fix set varchar limit when `max_length` is set on Pydantic models using Pydantic v2. PR [#963](https://github.com/tiangolo/sqlmodel/pull/963) by [@estebanx64](https://github.com/estebanx64). - -### Refactors - -* ♻️ Refactor generate select template to isolate templated code to the minimum. PR [#967](https://github.com/tiangolo/sqlmodel/pull/967) by [@tiangolo](https://github.com/tiangolo). - -### Upgrades - -* ⬆️ Update minimum SQLAlchemy version to 2.0.14 as that one includes `TryCast` used internally. PR [#964](https://github.com/tiangolo/sqlmodel/pull/964) by [@tiangolo](https://github.com/tiangolo). - -### Docs - -* ✏️ Fix broken link to `@dataclass_transform` (now PEP 681) in `docs/features.md`. PR [#753](https://github.com/tiangolo/sqlmodel/pull/753) by [@soof-golan](https://github.com/soof-golan). - -### Internal - -* ⬆️ Upgrade Ruff and Black. PR [#968](https://github.com/tiangolo/sqlmodel/pull/968) by [@tiangolo](https://github.com/tiangolo). -* ⬆ Bump tiangolo/issue-manager from 0.4.1 to 0.5.0. PR [#922](https://github.com/tiangolo/sqlmodel/pull/922) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ“Œ Pin typing-extensions in tests for compatiblity with Python 3.8, dirty-equals, Pydantic. PR [#965](https://github.com/tiangolo/sqlmodel/pull/965) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update GitHub Actions to download and upload artifacts. PR [#936](https://github.com/tiangolo/sqlmodel/pull/936) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Tweak CI for test-redistribute, add needed env vars for slim. PR [#929](https://github.com/tiangolo/sqlmodel/pull/929) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.18 - -### Internal - -* ✨ Add `sqlmodel-slim` setup. PR [#916](https://github.com/tiangolo/sqlmodel/pull/916) by [@tiangolo](https://github.com/tiangolo). - -In the future SQLModel will include the standard default recommended packages, and `sqlmodel-slim` will come without those recommended standard packages and with a group of optional dependencies `sqlmodel-slim[standard]`, equivalent to `sqlmodel`, for those that want to opt out of those packages. - -* πŸ”§ Re-enable MkDocs Material Social plugin. PR [#915](https://github.com/tiangolo/sqlmodel/pull/915) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.17 - -### Refactors - -* ♻️ Refactor types to properly support Pydantic 2.7. PR [#913](https://github.com/tiangolo/sqlmodel/pull/913) by [@tiangolo](https://github.com/tiangolo). - -### Docs - -* πŸ“ Update ModelRead to ModelPublic documentation and examples. PR [#885](https://github.com/tiangolo/sqlmodel/pull/885) by [@estebanx64](https://github.com/estebanx64). -* ✨ Add source examples for Python 3.10 and 3.9 with updated syntax. PR [#842](https://github.com/tiangolo/sqlmodel/pull/842) by [@tiangolo](https://github.com/tiangolo) and [@estebanx64](https://github.com/estebanx64). - -### Internal - -* ⬆ Bump actions/setup-python from 4 to 5. PR [#733](https://github.com/tiangolo/sqlmodel/pull/733) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ”¨ Update internal scripts and remove unused ones. PR [#914](https://github.com/tiangolo/sqlmodel/pull/914) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Migrate from Poetry to PDM for the internal build config. PR [#912](https://github.com/tiangolo/sqlmodel/pull/912) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Update MkDocs, disable cards while I can upgrade to the latest MkDocs Material, that fixes an issue with social cards. PR [#888](https://github.com/tiangolo/sqlmodel/pull/888) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Add cron to run test once a week on monday. PR [#869](https://github.com/tiangolo/sqlmodel/pull/869) by [@estebanx64](https://github.com/estebanx64). -* ⬆️ Upgrade Ruff version and configs. PR [#859](https://github.com/tiangolo/sqlmodel/pull/859) by [@tiangolo](https://github.com/tiangolo). -* πŸ”₯ Remove Jina QA Bot as it has been discontinued. PR [#840](https://github.com/tiangolo/sqlmodel/pull/840) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.16 - -### Features - -* ✨ Add new method `.sqlmodel_update()` to update models in place, including an `update` parameter for extra data. And fix implementation for the (now documented) `update` parameter for `.model_validate()`. PR [#804](https://github.com/tiangolo/sqlmodel/pull/804) by [@tiangolo](https://github.com/tiangolo). - * Updated docs: [Update Data with FastAPI](https://sqlmodel.tiangolo.com/tutorial/fastapi/update/). - * New docs: [Update with Extra Data (Hashed Passwords) with FastAPI](https://sqlmodel.tiangolo.com/tutorial/fastapi/update-extra-data/). - -## 0.0.15 - -### Fixes - -* πŸ› Fix class initialization compatibility with Pydantic and SQLModel, fixing errors revealed by the latest Pydantic. PR [#807](https://github.com/tiangolo/sqlmodel/pull/807) by [@tiangolo](https://github.com/tiangolo). - -### Internal - -* ⬆ Bump tiangolo/issue-manager from 0.4.0 to 0.4.1. PR [#775](https://github.com/tiangolo/sqlmodel/pull/775) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ‘· Fix GitHub Actions build docs filter paths for GitHub workflows. PR [#738](https://github.com/tiangolo/sqlmodel/pull/738) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.14 - -### Features - -* ✨ Add support for Pydantic v2 (while keeping support for v1 if v2 is not available). PR [#722](https://github.com/tiangolo/sqlmodel/pull/722) by [@tiangolo](https://github.com/tiangolo) including initial work in PR [#699](https://github.com/tiangolo/sqlmodel/pull/699) by [@AntonDeMeester](https://github.com/AntonDeMeester). - -## 0.0.13 - -### Fixes - -* ♻️ Refactor type generation of selects re-order to prioritize models to optimize editor support. PR [#718](https://github.com/tiangolo/sqlmodel/pull/718) by [@tiangolo](https://github.com/tiangolo). - -### Refactors - -* πŸ”‡ Do not raise deprecation warnings for execute as it's automatically used internally. PR [#716](https://github.com/tiangolo/sqlmodel/pull/716) by [@tiangolo](https://github.com/tiangolo). -* βœ… Move OpenAPI tests inline to simplify updating them with Pydantic v2. PR [#709](https://github.com/tiangolo/sqlmodel/pull/709) by [@tiangolo](https://github.com/tiangolo). - -### Upgrades - -* ⬆️ Add support for Python 3.11 and Python 3.12. PR [#710](https://github.com/tiangolo/sqlmodel/pull/710) by [@tiangolo](https://github.com/tiangolo). - -### Docs - -* ✏️ Fix typo, simplify single quote/apostrophe character in "Sister Margaret's" everywhere in the docs. PR [#721](https://github.com/tiangolo/sqlmodel/pull/721) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Update docs for Decimal, use proper types. PR [#719](https://github.com/tiangolo/sqlmodel/pull/719) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Add source examples for Python 3.9 and 3.10. PR [#715](https://github.com/tiangolo/sqlmodel/pull/715) by [@tiangolo](https://github.com/tiangolo). - -### Internal - -* πŸ™ˆ Update gitignore, include all coverage files. PR [#711](https://github.com/tiangolo/sqlmodel/pull/711) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Update config with new pymdown extensions. PR [#712](https://github.com/tiangolo/sqlmodel/pull/712) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Update docs build setup, add support for sponsors, add sponsor GOVCERT.LU. PR [#720](https://github.com/tiangolo/sqlmodel/pull/720) by [@tiangolo](https://github.com/tiangolo). -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#697](https://github.com/tiangolo/sqlmodel/pull/697) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* πŸ”§ Show line numbers in docs during local development. PR [#714](https://github.com/tiangolo/sqlmodel/pull/714) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Update details syntax with new pymdown extensions format. PR [#713](https://github.com/tiangolo/sqlmodel/pull/713) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.12 - -### Features - -* ✨ Upgrade SQLAlchemy to 2.0. PR [#700](https://github.com/tiangolo/sqlmodel/pull/700) by [@tiangolo](https://github.com/tiangolo) including initial work in PR [#563](https://github.com/tiangolo/sqlmodel/pull/563) by [@farahats9](https://github.com/farahats9). - -### Internal - -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#686](https://github.com/tiangolo/sqlmodel/pull/686) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* πŸ‘· Upgrade latest-changes GitHub Action. PR [#693](https://github.com/tiangolo/sqlmodel/pull/693) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.11 - -### Features - -* ✨ Add support for passing a custom SQLAlchemy type to `Field()` with `sa_type`. PR [#505](https://github.com/tiangolo/sqlmodel/pull/505) by [@maru0123-2004](https://github.com/maru0123-2004). - * You might consider this a breaking change if you were using an incompatible combination of arguments, those arguments were not taking effect and now you will have a type error and runtime error telling you that. -* ✨ Do not allow invalid combinations of field parameters for columns and relationships, `sa_column` excludes `sa_column_args`, `primary_key`, `nullable`, etc. PR [#681](https://github.com/tiangolo/sqlmodel/pull/681) by [@tiangolo](https://github.com/tiangolo). - -### Docs - -* 🎨 Update inline source examples, hide `#` in annotations (from MkDocs Material). PR [#677](https://github.com/tiangolo/sqlmodel/pull/677) by [@Matthieu-LAURENT39](https://github.com/Matthieu-LAURENT39). - -### Internal - -* ⬆ Update coverage requirement from ^6.2 to >=6.2,<8.0. PR [#663](https://github.com/tiangolo/sqlmodel/pull/663) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Update mkdocs-material requirement from 9.1.21 to 9.2.7. PR [#675](https://github.com/tiangolo/sqlmodel/pull/675) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆️ Upgrade mypy manually. PR [#684](https://github.com/tiangolo/sqlmodel/pull/684) by [@tiangolo](https://github.com/tiangolo). -* ⬆ Update black requirement from ^22.10.0 to >=22.10,<24.0. PR [#664](https://github.com/tiangolo/sqlmodel/pull/664) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ‘· Update CI to build MkDocs Insiders only when the secrets are available, for Dependabot. PR [#683](https://github.com/tiangolo/sqlmodel/pull/683) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.10 - -### Features - -* ✨ Add support for all `Field` parameters from Pydantic `1.9.0` and above, make Pydantic `1.9.0` the minimum required version. PR [#440](https://github.com/tiangolo/sqlmodel/pull/440) by [@daniil-berg](https://github.com/daniil-berg). - -### Internal - -* πŸ”§ Adopt Ruff for formatting. PR [#679](https://github.com/tiangolo/sqlmodel/pull/679) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.9 - -### Breaking Changes - -* πŸ—‘οΈ Deprecate Python 3.6 and upgrade Poetry and Poetry Version Plugin. PR [#627](https://github.com/tiangolo/sqlmodel/pull/627) by [@tiangolo](https://github.com/tiangolo). - -### Features - -* ✨ Raise a more clear error when a type is not valid. PR [#425](https://github.com/tiangolo/sqlmodel/pull/425) by [@ddanier](https://github.com/ddanier). - -### Fixes - -* πŸ› Fix `AsyncSession` type annotations for `exec()`. PR [#58](https://github.com/tiangolo/sqlmodel/pull/58) by [@Bobronium](https://github.com/Bobronium). -* πŸ› Fix allowing using a `ForeignKey` directly, remove repeated column construction from `SQLModelMetaclass.__init__` and upgrade minimum SQLAlchemy to `>=1.4.36`. PR [#443](https://github.com/tiangolo/sqlmodel/pull/443) by [@daniil-berg](https://github.com/daniil-berg). -* πŸ› Fix enum type checks ordering in `get_sqlalchemy_type`. PR [#669](https://github.com/tiangolo/sqlmodel/pull/669) by [@tiangolo](https://github.com/tiangolo). -* πŸ› Fix SQLAlchemy version 1.4.36 breaks SQLModel relationships (#315). PR [#461](https://github.com/tiangolo/sqlmodel/pull/461) by [@byrman](https://github.com/byrman). - -### Upgrades - -* ⬆️ Upgrade support for SQLAlchemy 1.4.49, update tests. PR [#519](https://github.com/tiangolo/sqlmodel/pull/519) by [@sandrotosi](https://github.com/sandrotosi). -* ⬆ Raise SQLAlchemy version requirement to at least `1.4.29` (related to #434). PR [#439](https://github.com/tiangolo/sqlmodel/pull/439) by [@daniil-berg](https://github.com/daniil-berg). - -### Docs - -* πŸ“ Clarify description of in-memory SQLite database in `docs/tutorial/create-db-and-table.md`. PR [#601](https://github.com/tiangolo/sqlmodel/pull/601) by [@SimonCW](https://github.com/SimonCW). -* πŸ“ Tweak wording in `docs/tutorial/fastapi/multiple-models.md`. PR [#674](https://github.com/tiangolo/sqlmodel/pull/674) by [@tiangolo](https://github.com/tiangolo). -* ✏️ Fix contributing instructions to run tests, update script name. PR [#634](https://github.com/tiangolo/sqlmodel/pull/634) by [@PookieBuns](https://github.com/PookieBuns). -* πŸ“ Update link to docs for intro to databases. PR [#593](https://github.com/tiangolo/sqlmodel/pull/593) by [@abenezerBelachew](https://github.com/abenezerBelachew). -* πŸ“ Update docs, use `offset` in example with `limit` and `where`. PR [#273](https://github.com/tiangolo/sqlmodel/pull/273) by [@jbmchuck](https://github.com/jbmchuck). -* πŸ“ Fix docs for Pydantic's fields using `le` (`lte` is invalid, use `le` ). PR [#207](https://github.com/tiangolo/sqlmodel/pull/207) by [@jrycw](https://github.com/jrycw). -* πŸ“ Update outdated link in `docs/db-to-code.md`. PR [#649](https://github.com/tiangolo/sqlmodel/pull/649) by [@MatveyF](https://github.com/MatveyF). -* ✏️ Fix typos found with codespell. PR [#520](https://github.com/tiangolo/sqlmodel/pull/520) by [@kianmeng](https://github.com/kianmeng). -* πŸ“ Fix typos (duplication) in main page. PR [#631](https://github.com/tiangolo/sqlmodel/pull/631) by [@Mr-DRP](https://github.com/Mr-DRP). -* πŸ“ Update release notes, add second author to PR. PR [#429](https://github.com/tiangolo/sqlmodel/pull/429) by [@br-follow](https://github.com/br-follow). -* πŸ“ Update instructions about how to make a foreign key required in `docs/tutorial/relationship-attributes/define-relationships-attributes.md`. PR [#474](https://github.com/tiangolo/sqlmodel/pull/474) by [@jalvaradosegura](https://github.com/jalvaradosegura). -* πŸ“ Update help SQLModel docs. PR [#548](https://github.com/tiangolo/sqlmodel/pull/548) by [@tiangolo](https://github.com/tiangolo). -* ✏️ Fix typo in internal function name `get_sqlachemy_type()`. PR [#496](https://github.com/tiangolo/sqlmodel/pull/496) by [@cmarqu](https://github.com/cmarqu). -* ✏️ Fix typo in docs. PR [#446](https://github.com/tiangolo/sqlmodel/pull/446) by [@davidbrochart](https://github.com/davidbrochart). -* ✏️ Fix typo in `docs/tutorial/create-db-and-table.md`. PR [#477](https://github.com/tiangolo/sqlmodel/pull/477) by [@FluffyDietEngine](https://github.com/FluffyDietEngine). -* ✏️ Fix small typos in docs. PR [#481](https://github.com/tiangolo/sqlmodel/pull/481) by [@micuffaro](https://github.com/micuffaro). - -### Internal - -* ⬆ [pre-commit.ci] pre-commit autoupdate. PR [#672](https://github.com/tiangolo/sqlmodel/pull/672) by [@pre-commit-ci[bot]](https://github.com/apps/pre-commit-ci). -* ⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.28.0. PR [#660](https://github.com/tiangolo/sqlmodel/pull/660) by [@dependabot[bot]](https://github.com/apps/dependabot). -* βœ… Refactor OpenAPI FastAPI tests to simplify updating them later, this moves things around without changes. PR [#671](https://github.com/tiangolo/sqlmodel/pull/671) by [@tiangolo](https://github.com/tiangolo). -* ⬆ Bump actions/checkout from 3 to 4. PR [#670](https://github.com/tiangolo/sqlmodel/pull/670) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ”§ Update mypy config, use `strict = true` instead of manual configs. PR [#428](https://github.com/tiangolo/sqlmodel/pull/428) by [@michaeloliverx](https://github.com/michaeloliverx). -* ⬆️ Upgrade MkDocs Material. PR [#668](https://github.com/tiangolo/sqlmodel/pull/668) by [@tiangolo](https://github.com/tiangolo). -* 🎨 Update docs format and references with pre-commit and Ruff. PR [#667](https://github.com/tiangolo/sqlmodel/pull/667) by [@tiangolo](https://github.com/tiangolo). -* 🎨 Run pre-commit on all files and autoformat. PR [#666](https://github.com/tiangolo/sqlmodel/pull/666) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Move to Ruff and add pre-commit. PR [#661](https://github.com/tiangolo/sqlmodel/pull/661) by [@tiangolo](https://github.com/tiangolo). -* πŸ› οΈ Add `CITATION.cff` file for academic citations. PR [#13](https://github.com/tiangolo/sqlmodel/pull/13) by [@sugatoray](https://github.com/sugatoray). -* πŸ‘· Update docs deployments to Cloudflare. PR [#630](https://github.com/tiangolo/sqlmodel/pull/630) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘·β€β™‚οΈ Upgrade CI for docs. PR [#628](https://github.com/tiangolo/sqlmodel/pull/628) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update CI debug mode with Tmate. PR [#629](https://github.com/tiangolo/sqlmodel/pull/629) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Update latest changes token. PR [#616](https://github.com/tiangolo/sqlmodel/pull/616) by [@tiangolo](https://github.com/tiangolo). -* ⬆️ Upgrade analytics. PR [#558](https://github.com/tiangolo/sqlmodel/pull/558) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Update new issue chooser to point to GitHub Discussions. PR [#546](https://github.com/tiangolo/sqlmodel/pull/546) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Add template for GitHub Discussion questions and update issues template. PR [#544](https://github.com/tiangolo/sqlmodel/pull/544) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Refactor CI artifact upload/download for docs previews. PR [#514](https://github.com/tiangolo/sqlmodel/pull/514) by [@tiangolo](https://github.com/tiangolo). -* ⬆ Bump actions/cache from 2 to 3. PR [#497](https://github.com/tiangolo/sqlmodel/pull/497) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump dawidd6/action-download-artifact from 2.24.0 to 2.24.2. PR [#493](https://github.com/tiangolo/sqlmodel/pull/493) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ”§ Update Smokeshow coverage threshold. PR [#487](https://github.com/tiangolo/sqlmodel/pull/487) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Move from Codecov to Smokeshow. PR [#486](https://github.com/tiangolo/sqlmodel/pull/486) by [@tiangolo](https://github.com/tiangolo). -* ⬆ Bump actions/setup-python from 2 to 4. PR [#411](https://github.com/tiangolo/sqlmodel/pull/411) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Update black requirement from ^21.5-beta.1 to ^22.10.0. PR [#460](https://github.com/tiangolo/sqlmodel/pull/460) by [@dependabot[bot]](https://github.com/apps/dependabot). -* βž• Add extra dev dependencies for MkDocs Material. PR [#485](https://github.com/tiangolo/sqlmodel/pull/485) by [@tiangolo](https://github.com/tiangolo). -* ⬆ Update mypy requirement from 0.930 to 0.971. PR [#380](https://github.com/tiangolo/sqlmodel/pull/380) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Update coverage requirement from ^5.5 to ^6.2. PR [#171](https://github.com/tiangolo/sqlmodel/pull/171) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump codecov/codecov-action from 2 to 3. PR [#415](https://github.com/tiangolo/sqlmodel/pull/415) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump actions/upload-artifact from 2 to 3. PR [#412](https://github.com/tiangolo/sqlmodel/pull/412) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Update flake8 requirement from ^3.9.2 to ^5.0.4. PR [#396](https://github.com/tiangolo/sqlmodel/pull/396) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Update pytest requirement from ^6.2.4 to ^7.0.1. PR [#242](https://github.com/tiangolo/sqlmodel/pull/242) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump actions/checkout from 2 to 3.1.0. PR [#458](https://github.com/tiangolo/sqlmodel/pull/458) by [@dependabot[bot]](https://github.com/apps/dependabot). -* ⬆ Bump dawidd6/action-download-artifact from 2.9.0 to 2.24.0. PR [#470](https://github.com/tiangolo/sqlmodel/pull/470) by [@dependabot[bot]](https://github.com/apps/dependabot). -* πŸ‘· Update Dependabot config. PR [#484](https://github.com/tiangolo/sqlmodel/pull/484) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.8 - -### Fixes - -* πŸ› Fix auto detecting and setting `nullable`, allowing overrides in field. PR [#423](https://github.com/tiangolo/sqlmodel/pull/423) by [@JonasKs](https://github.com/JonasKs) and [@br-follow](https://github.com/br-follow). -* ♻️ Update `expresion.py`, sync from Jinja2 template, implement `inherit_cache` to solve errors like: `SAWarning: Class SelectOfScalar will not make use of SQL compilation caching`. PR [#422](https://github.com/tiangolo/sqlmodel/pull/422) by [@tiangolo](https://github.com/tiangolo). - -### Docs - -* πŸ“ Adjust and clarify docs for `docs/tutorial/create-db-and-table.md`. PR [#426](https://github.com/tiangolo/sqlmodel/pull/426) by [@tiangolo](https://github.com/tiangolo). -* ✏ Fix typo in `docs/tutorial/connect/remove-data-connections.md`. PR [#421](https://github.com/tiangolo/sqlmodel/pull/421) by [@VerdantFox](https://github.com/VerdantFox). - -## 0.0.7 - -### Features - -* ✨ Allow setting `unique` in `Field()` for a column. PR [#83](https://github.com/tiangolo/sqlmodel/pull/83) by [@raphaelgibson](https://github.com/raphaelgibson). -* ✨ Update GUID handling to use stdlib `UUID.hex` instead of an `int`. PR [#26](https://github.com/tiangolo/sqlmodel/pull/26) by [@andrewbolster](https://github.com/andrewbolster). -* ✨ Raise an exception when using a Pydantic field type with no matching SQLAlchemy type. PR [#18](https://github.com/tiangolo/sqlmodel/pull/18) by [@elben10](https://github.com/elben10). -* ⬆ Upgrade constrain for SQLAlchemy = ">=1.4.17,<=1.4.41". PR [#371](https://github.com/tiangolo/sqlmodel/pull/371) by [@RobertRosca](https://github.com/RobertRosca). -* ✨ Add new `Session.get()` parameter `execution_options`. PR [#302](https://github.com/tiangolo/sqlmodel/pull/302) by [@tiangolo](https://github.com/tiangolo). - -### Fixes - -* πŸ› Fix type annotations for `Model.parse_obj()`, and `Model.validate()`. PR [#321](https://github.com/tiangolo/sqlmodel/pull/321) by [@phi-friday](https://github.com/phi-friday). -* πŸ› Fix `Select` and `SelectOfScalar` to inherit cache to avoid warning: `SAWarning: Class SelectOfScalar will not make use of SQL compilation caching`. PR [#234](https://github.com/tiangolo/sqlmodel/pull/234) by [@rabinadk1](https://github.com/rabinadk1). -* πŸ› Fix handling validators for non-default values. PR [#253](https://github.com/tiangolo/sqlmodel/pull/253) by [@byrman](https://github.com/byrman). -* πŸ› Fix fields marked as "set" in models. PR [#117](https://github.com/tiangolo/sqlmodel/pull/117) by [@statt8900](https://github.com/statt8900). -* πŸ› Fix Enum handling in SQLAlchemy. PR [#165](https://github.com/tiangolo/sqlmodel/pull/165) by [@chriswhite199](https://github.com/chriswhite199). -* πŸ› Fix setting nullable property of Fields that don't accept `None`. PR [#79](https://github.com/tiangolo/sqlmodel/pull/79) by [@van51](https://github.com/van51). -* πŸ› Fix SQLAlchemy version 1.4.36 breaks SQLModel relationships (#315). PR [#322](https://github.com/tiangolo/sqlmodel/pull/322) by [@byrman](https://github.com/byrman). - -### Docs - -* πŸ“ Update docs for models for updating, `id` should not be updatable. PR [#335](https://github.com/tiangolo/sqlmodel/pull/335) by [@kurtportelli](https://github.com/kurtportelli). -* ✏ Fix broken variable/typo in docs for Read Relationships, `hero_spider_boy.id` => `hero_spider_boy.team_id`. PR [#106](https://github.com/tiangolo/sqlmodel/pull/106) by [@yoannmos](https://github.com/yoannmos). -* 🎨 Remove unwanted highlight in the docs. PR [#233](https://github.com/tiangolo/sqlmodel/pull/233) by [@jalvaradosegura](https://github.com/jalvaradosegura). -* ✏ Fix typos in `docs/databases.md` and `docs/tutorial/index.md`. PR [#35](https://github.com/tiangolo/sqlmodel/pull/35) by [@prrao87](https://github.com/prrao87). -* ✏ Fix typo in `docs/tutorial/relationship-attributes/define-relationships-attributes.md`. PR [#239](https://github.com/tiangolo/sqlmodel/pull/239) by [@jalvaradosegura](https://github.com/jalvaradosegura). -* ✏ Fix typo in `docs/tutorial/fastapi/simple-hero-api.md`. PR [#80](https://github.com/tiangolo/sqlmodel/pull/80) by [@joemudryk](https://github.com/joemudryk). -* ✏ Fix typos in multiple files in the docs. PR [#400](https://github.com/tiangolo/sqlmodel/pull/400) by [@VictorGambarini](https://github.com/VictorGambarini). -* ✏ Fix typo in `docs/tutorial/code-structure.md`. PR [#344](https://github.com/tiangolo/sqlmodel/pull/344) by [@marciomazza](https://github.com/marciomazza). -* ✏ Fix typo in `docs/db-to-code.md`. PR [#155](https://github.com/tiangolo/sqlmodel/pull/155) by [@gr8jam](https://github.com/gr8jam). -* ✏ Fix typo in `docs/contributing.md`. PR [#323](https://github.com/tiangolo/sqlmodel/pull/323) by [@Fardad13](https://github.com/Fardad13). -* ✏ Fix typo in `docs/tutorial/fastapi/tests.md`. PR [#265](https://github.com/tiangolo/sqlmodel/pull/265) by [@johnhoman](https://github.com/johnhoman). -* ✏ Fix typo in `docs/tutorial/where.md`. PR [#286](https://github.com/tiangolo/sqlmodel/pull/286) by [@jalvaradosegura](https://github.com/jalvaradosegura). -* ✏ Fix typos in `docs/tutorial/fastapi/update.md`. PR [#268](https://github.com/tiangolo/sqlmodel/pull/268) by [@cirrusj](https://github.com/cirrusj). -* ✏ Fix typo in `docs/tutorial/fastapi/simple-hero-api.md`. PR [#247](https://github.com/tiangolo/sqlmodel/pull/247) by [@hao-wang](https://github.com/hao-wang). -* ✏ Fix typos in `docs/tutorial/automatic-id-none-refresh.md`, `docs/tutorial/fastapi/update.md`, `docs/tutorial/select.md`. PR [#185](https://github.com/tiangolo/sqlmodel/pull/185) by [@rootux](https://github.com/rootux). -* ✏ Fix typo in `docs/databases.md`. PR [#177](https://github.com/tiangolo/sqlmodel/pull/177) by [@seandlg](https://github.com/seandlg). -* ✏ Fix typos in `docs/tutorial/fastapi/update.md`. PR [#162](https://github.com/tiangolo/sqlmodel/pull/162) by [@wmcgee3](https://github.com/wmcgee3). -* ✏ Fix typos in `docs/tutorial/code-structure.md`, `docs/tutorial/fastapi/multiple-models.md`, `docs/tutorial/fastapi/simple-hero-api.md`, `docs/tutorial/many-to-many/index.md`. PR [#116](https://github.com/tiangolo/sqlmodel/pull/116) by [@moonso](https://github.com/moonso). -* ✏ Fix typo in `docs/tutorial/fastapi/teams.md`. PR [#154](https://github.com/tiangolo/sqlmodel/pull/154) by [@chrisgoddard](https://github.com/chrisgoddard). -* ✏ Fix typo variable in example about relationships and `back_populates`, always use `hero` instead of `owner`. PR [#120](https://github.com/tiangolo/sqlmodel/pull/120) by [@onionj](https://github.com/onionj). -* ✏ Fix typo in `docs/tutorial/fastapi/tests.md`. PR [#113](https://github.com/tiangolo/sqlmodel/pull/113) by [@feanil](https://github.com/feanil). -* ✏ Fix typo in `docs/tutorial/where.md`. PR [#72](https://github.com/tiangolo/sqlmodel/pull/72) by [@ZettZet](https://github.com/ZettZet). -* ✏ Fix typo in `docs/tutorial/code-structure.md`. PR [#91](https://github.com/tiangolo/sqlmodel/pull/91) by [@dhiraj](https://github.com/dhiraj). -* ✏ Fix broken link to newsletter sign-up in `docs/help.md`. PR [#84](https://github.com/tiangolo/sqlmodel/pull/84) by [@mborus](https://github.com/mborus). -* ✏ Fix typos in `docs/tutorial/many-to-many/create-models-with-link.md`. PR [#45](https://github.com/tiangolo/sqlmodel/pull/45) by [@xginn8](https://github.com/xginn8). -* ✏ Fix typo in `docs/tutorial/index.md`. PR [#398](https://github.com/tiangolo/sqlmodel/pull/398) by [@ryangrose](https://github.com/ryangrose). - -### Internal - -* β™» Refactor internal statements to simplify code. PR [#53](https://github.com/tiangolo/sqlmodel/pull/53) by [@yezz123](https://github.com/yezz123). -* β™» Refactor internal imports to reduce redundancy. PR [#272](https://github.com/tiangolo/sqlmodel/pull/272) by [@aminalaee](https://github.com/aminalaee). -* ⬆ Update development requirement for FastAPI from `^0.68.0` to `^0.68.1`. PR [#48](https://github.com/tiangolo/sqlmodel/pull/48) by [@alucarddelta](https://github.com/alucarddelta). -* βͺ Revert upgrade Poetry, to make a release that supports Python 3.6 first. PR [#417](https://github.com/tiangolo/sqlmodel/pull/417) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Add dependabot for GitHub Actions. PR [#410](https://github.com/tiangolo/sqlmodel/pull/410) by [@tiangolo](https://github.com/tiangolo). -* ⬆️ Upgrade Poetry to version `==1.2.0b1`. PR [#303](https://github.com/tiangolo/sqlmodel/pull/303) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Add CI for Python 3.10. PR [#305](https://github.com/tiangolo/sqlmodel/pull/305) by [@tiangolo](https://github.com/tiangolo). -* πŸ“ Add Jina's QA Bot to the docs to help people that want to ask quick questions. PR [#263](https://github.com/tiangolo/sqlmodel/pull/263) by [@tiangolo](https://github.com/tiangolo). -* πŸ‘· Upgrade Codecov GitHub Action. PR [#304](https://github.com/tiangolo/sqlmodel/pull/304) by [@tiangolo](https://github.com/tiangolo). -* πŸ’š Only run CI on push when on master, to avoid duplicate runs on PRs. PR [#244](https://github.com/tiangolo/sqlmodel/pull/244) by [@tiangolo](https://github.com/tiangolo). -* πŸ”§ Upgrade MkDocs Material and update configs. PR [#217](https://github.com/tiangolo/sqlmodel/pull/217) by [@tiangolo](https://github.com/tiangolo). -* ⬆ Upgrade mypy, fix type annotations. PR [#218](https://github.com/tiangolo/sqlmodel/pull/218) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.6 - -### Breaking Changes - -**SQLModel** no longer creates indexes by default for every column, indexes are now opt-in. You can read more about it in PR [#205](https://github.com/tiangolo/sqlmodel/pull/205). - -Before this change, if you had a model like this: - -```Python -from typing import Optional - -from sqlmodel import Field, SQLModel - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str - secret_name: str - age: Optional[int] = None -``` - -...when creating the tables, SQLModel version `0.0.5` and below, would also create an index for `name`, one for `secret_name`, and one for `age` (`id` is the primary key, so it doesn't need an additional index). - -If you depended on having an index for each one of those columns, now you can (and would have to) define them explicitly: - -```Python -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str = Field(index=True) - age: Optional[int] = Field(default=None, index=True) -``` - -There's a high chance you don't need indexes for all the columns. For example, you might only need indexes for `name` and `age`, but not for `secret_name`. In that case, you could define the model as: - -```Python -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) -``` - -If you already created your database tables with SQLModel using versions `0.0.5` or below, it would have also created those indexes in the database. In that case, you might want to manually drop (remove) some of those indexes, if they are unnecessary, to avoid the extra cost in performance and space. - -Depending on the database you are using, there will be a different way to find the available indexes. - -For example, let's say you no longer need the index for `secret_name`. You could check the current indexes in the database and find the one for `secret_name`, it could be named `ix_hero_secret_name`. Then you can remove it with SQL: - -```SQL -DROP INDEX ix_hero_secret_name -``` - -or - -```SQL -DROP INDEX ix_hero_secret_name ON hero; -``` - -Here's the new, extensive documentation explaining indexes and how to use them: [Indexes - Optimize Queries](https://sqlmodel.tiangolo.com/tutorial/indexes/). - -### Docs - -* ✨ Document indexes and make them opt-in. Here's the new documentation: [Indexes - Optimize Queries](https://sqlmodel.tiangolo.com/tutorial/indexes/). This is the same change described above in **Breaking Changes**. PR [#205](https://github.com/tiangolo/sqlmodel/pull/205) by [@tiangolo](https://github.com/tiangolo). -* ✏ Fix typo in FastAPI tutorial. PR [#192](https://github.com/tiangolo/sqlmodel/pull/192) by [@yaquelinehoyos](https://github.com/yaquelinehoyos). -* πŸ“ Add links to the license file. PR [#29](https://github.com/tiangolo/sqlmodel/pull/29) by [@sobolevn](https://github.com/sobolevn). -* ✏ Fix typos in docs titles. PR [#28](https://github.com/tiangolo/sqlmodel/pull/28) by [@Batalex](https://github.com/Batalex). -* ✏ Fix multiple typos and some rewording. PR [#22](https://github.com/tiangolo/sqlmodel/pull/22) by [@egrim](https://github.com/egrim). -* ✏ Fix typo in `docs/tutorial/automatic-id-none-refresh.md`. PR [#14](https://github.com/tiangolo/sqlmodel/pull/14) by [@leynier](https://github.com/leynier). -* ✏ Fix typos in `docs/tutorial/index.md` and `docs/databases.md`. PR [#5](https://github.com/tiangolo/sqlmodel/pull/5) by [@sebastianmarines](https://github.com/sebastianmarines). - -## 0.0.5 - -### Features - -* ✨ Add support for Decimal fields from Pydantic and SQLAlchemy. Original PR [#103](https://github.com/tiangolo/sqlmodel/pull/103) by [@robcxyz](https://github.com/robcxyz). New docs: [Advanced User Guide - Decimal Numbers](https://sqlmodel.tiangolo.com/advanced/decimal/). - -### Docs - -* ✏ Update decimal tutorial source for consistency. PR [#188](https://github.com/tiangolo/sqlmodel/pull/188) by [@tiangolo](https://github.com/tiangolo). - -### Internal - -* πŸ”§ Split MkDocs insiders build in CI to support building from PRs. PR [#186](https://github.com/tiangolo/sqlmodel/pull/186) by [@tiangolo](https://github.com/tiangolo). -* 🎨 Format `expression.py` and expression template, currently needed by CI. PR [#187](https://github.com/tiangolo/sqlmodel/pull/187) by [@tiangolo](https://github.com/tiangolo). -* πŸ›Fix docs light/dark theme switcher. PR [#1](https://github.com/tiangolo/sqlmodel/pull/1) by [@Lehoczky](https://github.com/Lehoczky). -* πŸ”§ Add MkDocs Material social cards. PR [#90](https://github.com/tiangolo/sqlmodel/pull/90) by [@tiangolo](https://github.com/tiangolo). -* ✨ Update type annotations and upgrade mypy. PR [#173](https://github.com/tiangolo/sqlmodel/pull/173) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.4 - -* 🎨 Fix type detection of select results in PyCharm. PR [#15](https://github.com/tiangolo/sqlmodel/pull/15) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.3 - -* ⬆️ Update and relax specification range for `sqlalchemy-stubs`. PR [#4](https://github.com/tiangolo/sqlmodel/pull/4) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.2 - -* This includes several small bug fixes detected during the first CI runs. -* πŸ’š Fix CI installs and tests. PR [#2](https://github.com/tiangolo/sqlmodel/pull/2) by [@tiangolo](https://github.com/tiangolo). - -## 0.0.1 - -* First release. πŸŽ‰ diff --git a/docs/resources/index.md b/docs/resources/index.md deleted file mode 100644 index d233a7833b..0000000000 --- a/docs/resources/index.md +++ /dev/null @@ -1,3 +0,0 @@ -# Resources - -Additional resources, how to **help** and get help, how to **contribute**, and more. ✈️ diff --git a/docs/tutorial/automatic-id-none-refresh.md b/docs/tutorial/automatic-id-none-refresh.md deleted file mode 100644 index 0e67633dee..0000000000 --- a/docs/tutorial/automatic-id-none-refresh.md +++ /dev/null @@ -1,455 +0,0 @@ -# Automatic IDs, None Defaults, and Refreshing Data - -In the previous chapter, we saw how to add rows to the database using **SQLModel**. - -Now let's talk a bit about why the `id` field **can't be `NULL`** on the database because it's a **primary key**, and we declare it using `Field(primary_key=True)`. - -But the same `id` field actually **can be `None`** in the Python code, so we declare the type with `int | None`, and set the default value to `Field(default=None)`: - -{* ./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[4:8] hl[5] *} - -Next, I'll show you a bit more about the synchronization of data between the database and the Python code. - -When do we get an actual `int` from the database in that `id` field? Let's see all that. πŸ‘‡ - -## Create a New `Hero` Instance - -When we create a new `Hero` instance, we don't set the `id`: - -{* ./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[21:24] hl[21:24] *} - -### How `int | None` Helps - -Because we don't set the `id`, it takes the Python's default value of `None` that we set in `Field(default=None)`. - -This is the only reason why we define it with `int | None` and with a default value of `None`. - -Because at this point in the code, **before interacting with the database**, the Python value could actually be `None`. - -If we assumed that the `id` was *always* an `int` and added the type annotation without `int | None`, we could end up writing broken code, like: - -```Python -next_hero_id = hero_1.id + 1 -``` - -If we ran this code before saving the hero to the database and the `hero_1.id` was still `None`, we would get an error like: - -``` -TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' -``` - -But by declaring it with `int | None`, the editor will help us to avoid writing broken code by showing us a warning telling us that the code could be invalid if `hero_1.id` is `None`. πŸ” - -## Print the Default `id` Values - -We can confirm that by printing our heroes before adding them to the database: - -{* ./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[21:29] hl[27:29] *} - -That will output: - -
- -```console -$ python app.py - -// Output above omitted πŸ‘† - -Before interacting with the database -Hero 1: id=None name='Deadpond' secret_name='Dive Wilson' age=None -Hero 2: id=None name='Spider-Boy' secret_name='Pedro Parqueador' age=None -Hero 3: id=None name='Rusty-Man' secret_name='Tommy Sharp' age=48 -``` - -
- -Notice they all have `id=None`. - -That's the default value we defined in the `Hero` model class. - -What happens when we `add` these objects to the **session**? - -## Add the Objects to the Session - -After we add the `Hero` instance objects to the **session**, the IDs are *still* `None`. - -We can verify by creating a session using a `with` block and adding the objects. And then printing them again: - -{* ./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[21:39] hl[37:39] *} - -This will, again, output the `id`s of the objects as `None`: - -
- -```console -$ python app.py - -// Output above omitted πŸ‘† - -After adding to the session -Hero 1: id=None name='Deadpond' secret_name='Dive Wilson' age=None -Hero 2: id=None name='Spider-Boy' secret_name='Pedro Parqueador' age=None -Hero 3: id=None name='Rusty-Man' secret_name='Tommy Sharp' age=48 -``` - -
- -As we saw before, the **session** is smart and doesn't talk to the database every time we prepare something to be changed, only after we are ready and tell it to `commit` the changes it goes and sends all the SQL to the database to store the data. - -## Commit the Changes to the Database - -Then we can `commit` the changes in the session, and print again: - -{* ./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[31:46] hl[41,44:46] *} - -And now, something unexpected happens, look at the output, it seems as if the `Hero` instance objects had no data at all: - -
- -```console -$ python app.py - -// Output above omitted πŸ‘† - -// Here the engine talks to the database, the SQL part -INFO Engine BEGIN (implicit) -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [generated in 0.00018s] ('Deadpond', 'Dive Wilson', None) -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [cached since 0.0008968s ago] ('Spider-Boy', 'Pedro Parqueador', None) -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [cached since 0.001143s ago] ('Rusty-Man', 'Tommy Sharp', 48) -INFO Engine COMMIT - -// And now our prints -After committing the session -Hero 1: -Hero 2: -Hero 3: - -// What is happening here? 😱 -``` - -
- -What happens is that SQLModel (actually SQLAlchemy) is internally marking those objects as "expired", they **don't have the latest version of their data**. This is because we could have some fields updated in the database, for example, imagine a field `updated_at: datetime` that was automatically updated when we saved changes. - -The same way, other values could have changed, so the option the **session** has to be sure and safe is to just internally mark the objects as expired. - -And then, next time we access each attribute, for example with: - -```Python -current_hero_name = hero_1.name -``` - -...SQLModel (actually SQLAlchemy) will make sure to contact the database and **get the most recent version of the data**, updating that field `name` in our object and then making it available for the rest of the Python expression. In the example above, at that point, Python would be able to continue executing and use that `hero_1.name` value (just updated) to put it in the variable `current_hero_name`. - -All this happens automatically and behind the scenes. ✨ - -And here's the funny and strange thing with our example: - -```Python -print("Hero 1:", hero_1) -``` - -We didn't access the object's attributes, like `hero.name`. We only accessed the entire object and printed it, so **SQLAlchemy has no way of knowing** that we want to access this object's data. - -## Print a Single Field - -To confirm and understand how this **automatic expiration and refresh** of data when accessing attributes work, we can print some individual fields (instance attributes): - -{* ./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[31:56] hl[49:51,54:56] *} - -Now we are actually accessing the attributes, because instead of printing the whole object `hero_1`: - -```Python -print("Hero 1:", hero_1) -``` - -...we are now printing the `id` attribute in `hero.id`: - -```Python -print("Hero 1 ID:", hero_1.id) -``` - -By accessing the attribute, that **triggers** a lot of work done by SQLModel (actually SQLAlchemy) underneath to **refresh the data from the database**, set it in the object's `id` attribute, and make it available for the Python expression (in this case just to print it). - -Let's see how it works: - -
- -```console -$ python app.py - -// Output above omitted πŸ‘† - -// After committing, the objects are expired and have no values -After committing the session -Hero 1: -Hero 2: -Hero 3: - -// Now we will access an attribute like the ID, this is the first print -After committing the session, show IDs - -// Notice that before printing the first ID, the Session makes the Engine go to the database to refresh the data πŸ€“ -INFO Engine BEGIN (implicit) -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [generated in 0.00017s] (1,) - -// Here's our first print, now we have the database-generated ID -Hero 1 ID: 1 - -// Before the next print, refresh the data for the second object -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.001245s ago] (2,) - -// Here's our print for the second hero with its auto-generated ID -Hero 2 ID: 2 - -// Before the third print, refresh its data -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.002215s ago] (3,) - -// And here's our print for the third hero -Hero 3 ID: 3 - -// What if we print another attribute like the name? -After committing the session, show names -Hero 1 name: Deadpond -Hero 2 name: Spider-Boy -Hero 3 name: Rusty-Man - -// Because the Session already refreshed these objects with all their data and the session knows they are not expired, it doesn't have to go again to the database for the names πŸ€“ -``` - -
- -## Refresh Objects Explicitly - -You just learnt how the **session** refreshes the data automatically behind the scenes, as a side effect, when you access an attribute. - -But what if you want to **explicitly refresh** the data? - -You can do that too with `session.refresh(object)`: - -{* ./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[31:65] hl[58:60,63:65] *} - -When Python executes this code: - -```Python -session.refresh(hero_1) -``` - -...the **session** goes and makes the **engine** communicate with the database to get the recent data for this object `hero_1`, and then the **session** puts the data in the `hero_1` object and marks it as "fresh" or "not expired". - -Here's how the output would look like: - -
- -```console -$ python app.py - -// Output above omitted πŸ‘† - -// The first refresh -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.id = ? -INFO Engine [generated in 0.00024s] (1,) - -// The second refresh -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.001487s ago] (2,) - -// The third refresh -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.002377s ago] (3,) - -// Now print the data, as it's already refreshed there's no need for the Session to go and refresh it again -After refreshing the heroes -Hero 1: age=None id=1 name='Deadpond' secret_name='Dive Wilson' -Hero 2: age=None id=2 name='Spider-Boy' secret_name='Pedro Parqueador' -Hero 3: age=48 id=3 name='Rusty-Man' secret_name='Tommy Sharp' -``` - -
- -This could be useful, for example, if you are building a web API to create heroes. And once a hero is created with some data, you return it to the client. - -You wouldn't want to return an object that looks empty because the automatic magic to refresh the data was not triggered. - -In this case, after committing the object to the database with the **session**, you could refresh it, and then return it to the client. This would ensure that the object has its fresh data. - -## Print Data After Closing the Session - -Now, as a final experiment, we can also print data after the **session** is closed. - -There are no surprises here, it still works: - -{* ./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[31:70] hl[68:70] *} - -And the output shows again the same data: - -
- -```console -$ python app.py - -// Output above omitted πŸ‘† - -// By finishing the with block, the Session is closed, including a rollback of any pending transaction that could have been there and was not committed -INFO Engine ROLLBACK - -// Then we print the data, it works normally -After the session closes -Hero 1: age=None id=1 name='Deadpond' secret_name='Dive Wilson' -Hero 2: age=None id=2 name='Spider-Boy' secret_name='Pedro Parqueador' -Hero 3: age=48 id=3 name='Rusty-Man' secret_name='Tommy Sharp' -``` - -
- -## Review All the Code - -Now let's review all this code once again. - -/// tip - -Each one of the numbered bubbles shows what each line will print in the output. - -And as we created the **engine** with `echo=True`, we can see the SQL statements being executed at each step. - -/// - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial002_py310.py!} -``` - -{!./docs_src/tutorial/automatic_id_none_refresh/annotations/en/tutorial002.md!} - -//// - -//// tab | Python 3.8+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial002.py!} -``` - -{!./docs_src/tutorial/automatic_id_none_refresh/annotations/en/tutorial002.md!} - -//// - -And here's all the output generated by running this program, all together: - -
- -```console -$ python app.py - -INFO Engine BEGIN (implicit) -INFO Engine PRAGMA main.table_info("hero") -INFO Engine [raw sql] () -INFO Engine PRAGMA temp.table_info("hero") -INFO Engine [raw sql] () -INFO Engine -CREATE TABLE hero ( - id INTEGER, - name VARCHAR NOT NULL, - secret_name VARCHAR NOT NULL, - age INTEGER, - PRIMARY KEY (id) -) - - -INFO Engine [no key 0.00018s] () -INFO Engine COMMIT -Before interacting with the database -Hero 1: id=None name='Deadpond' secret_name='Dive Wilson' age=None -Hero 2: id=None name='Spider-Boy' secret_name='Pedro Parqueador' age=None -Hero 3: id=None name='Rusty-Man' secret_name='Tommy Sharp' age=48 -After adding to the session -Hero 1: id=None name='Deadpond' secret_name='Dive Wilson' age=None -Hero 2: id=None name='Spider-Boy' secret_name='Pedro Parqueador' age=None -Hero 3: id=None name='Rusty-Man' secret_name='Tommy Sharp' age=48 -INFO Engine BEGIN (implicit) -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [generated in 0.00022s] ('Deadpond', 'Dive Wilson', None) -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [cached since 0.001127s ago] ('Spider-Boy', 'Pedro Parqueador', None) -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [cached since 0.001483s ago] ('Rusty-Man', 'Tommy Sharp', 48) -INFO Engine COMMIT -After committing the session -Hero 1: -Hero 2: -Hero 3: -After committing the session, show IDs -INFO Engine BEGIN (implicit) -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [generated in 0.00029s] (1,) -Hero 1 ID: 1 -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.002132s ago] (2,) -Hero 2 ID: 2 -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.003367s ago] (3,) -Hero 3 ID: 3 -After committing the session, show names -Hero 1 name: Deadpond -Hero 2 name: Spider-Boy -Hero 3 name: Rusty-Man -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.id = ? -INFO Engine [generated in 0.00025s] (1,) -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.001583s ago] (2,) -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.002722s ago] (3,) -After refreshing the heroes -Hero 1: age=None id=1 name='Deadpond' secret_name='Dive Wilson' -Hero 2: age=None id=2 name='Spider-Boy' secret_name='Pedro Parqueador' -Hero 3: age=48 id=3 name='Rusty-Man' secret_name='Tommy Sharp' -INFO Engine ROLLBACK -After the session closes -Hero 1: age=None id=1 name='Deadpond' secret_name='Dive Wilson' -Hero 2: age=None id=2 name='Spider-Boy' secret_name='Pedro Parqueador' -Hero 3: age=48 id=3 name='Rusty-Man' secret_name='Tommy Sharp' -``` - -
- -## Recap - -You read all that! That was a lot! Have some cake, you earned it. 🍰 - -We discussed how the **session** uses the **engine** to send SQL to the database, to create data and to fetch data too. How it keeps track of "**expired**" and "**fresh**" data. At which moments it **fetches data automatically** (when accessing instance attributes) and how that data is synchronized between objects in memory and the database via the **session**. - -If you understood all that, now you know a lot about **SQLModel**, SQLAlchemy, and how the interactions from Python with databases work in general. - -If you didn't get all that, it's fine, you can always come back later to `refresh` the concepts. - -I think this might be one of the main types of bugs that cause problems and makes you scratch your head. So, good job studying it! πŸ’ͺ diff --git a/docs/tutorial/code-structure.md b/docs/tutorial/code-structure.md deleted file mode 100644 index 6e377b89e4..0000000000 --- a/docs/tutorial/code-structure.md +++ /dev/null @@ -1,255 +0,0 @@ -# Code Structure and Multiple Files - -Let's stop for a second to think about how to structure the code, particularly in **large projects** with multiple files. - -## Circular Imports - -The class `Hero` has a reference to the class `Team` internally. - -But the class `Team` also has a reference to the class `Hero`. - -So, if those two classes were in separate files and you tried to import the classes in each other's file directly, it would result in a **circular import**. πŸ”„ - -And Python will not be able to handle it and will throw an error. 🚨 - -But we actually want to *mean* that **circular reference**, because in our code, we would be able to do crazy things like: - -```Python -hero.team.heroes[0].team.heroes[1].team.heroes[2].name -``` - -And that circular reference is what we are expressing with these **relationship attributes**, that: - -* A hero can have a team - * That team can have a list of heroes - * Each of those heroes can have a team - * ...and so on. - -Let's see different strategies to **structure the code** accounting for this. - -## Single Module for Models - -This is the simplest way. ✨ - -In this solution we are still using **multiple files**, for the `models`, for the `database`, and for the `app`. - -And we could have any **other files** necessary. - -But in this first case, all the models would live in a **single file**. - -The file structure of the project could be: - -``` -. -β”œβ”€β”€ project - β”œβ”€β”€ __init__.py - β”œβ”€β”€ app.py - β”œβ”€β”€ database.py - └── models.py -``` - -We have 3 **Python modules** (or files): - -* `app` -* `database` -* `models` - -And we also have an empty `__init__.py` file to make this project a "**Python package**" (a collection of Python modules). This way we can use **relative imports** in the `app.py` file/module, like: - -```Python -from .models import Hero, Team -from .database import engine -``` - -We can use these relative imports because, for example, in the file `app.py` (the `app` module) Python knows that it is **part of our Python package** because it is in the same directory as the file `__init__.py`. And all the Python files on the same directory are part of the same Python package too. - -### Models File - -You could put all the database Models in a single Python module (a single Python file), for example `models.py`: - -```Python -{!./docs_src/tutorial/code_structure/tutorial001/models.py!} -``` - -This way, you wouldn't have to deal with circular imports for other models. - -And then you could import the models from this file/module in any other file/module in your application. - -### Database File - -Then you could put the code creating the **engine** and the function to create all the tables (if you are not using migrations) in another file `database.py`: - -```Python -{!./docs_src/tutorial/code_structure/tutorial001/database.py!} -``` - -This file would also be imported by your application code, to use the shared **engine** and to get and call the function `create_db_and_tables()`. - -### Application File - -Finally, you could put the code to create the **app** in another file `app.py`: - -```Python hl_lines="3-4" -{!./docs_src/tutorial/code_structure/tutorial001/app.py!} -``` - -Here we import the models, the engine, and the function to create all the tables and then we can use them all internally. - -### Order Matters - -Remember that [Order Matters](create-db-and-table.md#sqlmodel-metadata-order-matters){.internal-link target=_blank} when calling `SQLModel.metadata.create_all()`? - -The point of that section in the docs is that you have to import the module that has the models **before** calling `SQLModel.metadata.create_all()`. - -We are doing that here, we import the models in `app.py` and **after** that we create the database and tables, so we are fine and everything works correctly. πŸ‘Œ - -### Run It in the Command Line - -Because now this is a larger project with a **Python package** and not a single Python file, we **cannot** call it just passing a single file name as we did before with: - -```console -$ python app.py -``` - -Now we have to tell Python that we want it to execute a *module* that is part of a package: - -```console -$ python -m project.app -``` - -The `-m` is to tell Python to call a *module*. And the next thing we pass is a string with `project.app`, that is the same format we would use in an **import**: - -```Python -import project.app -``` - -Then Python will execute that module *inside* of that package, and because Python is executing it directly, the same trick with the **main block** that we have in `app.py` will still work: - -```Python -if __name__ == '__main__': - main() -``` - -So, the output would be: - -
- -```console -$ python -m project.app - -Created hero: id=1 secret_name='Dive Wilson' team_id=1 name='Deadpond' age=None -Hero's team: name='Z-Force' headquarters='Sister Margaret's Bar' id=1 -``` - -
- -## Make Circular Imports Work - -Let's say that for some reason you hate the idea of having all the database models together in a single file, and you really want to have **separate files** a `hero_model.py` file and a `team_model.py` file. - -You can also do it. 😎 There's a couple of things to keep in mind. πŸ€“ - -/// warning - -This is a bit more advanced. - -If the solution above already worked for you, that might be enough for you, and you can continue in the next chapter. πŸ€“ - -/// - -Let's assume that now the file structure is: - -``` -. -β”œβ”€β”€ project - β”œβ”€β”€ __init__.py - β”œβ”€β”€ app.py - β”œβ”€β”€ database.py - β”œβ”€β”€ hero_model.py - └── team_model.py -``` - -### Circular Imports and Type Annotations - -The problem with circular imports is that Python can't resolve them at *runtime*. - -But when using Python **type annotations** it's very common to need to declare the type of some variables with classes imported from other files. - -And the files with those classes might **also need to import** more things from the first files. - -And this ends up *requiring* the same **circular imports** that are not supported in Python at *runtime*. - -### Type Annotations and Runtime - -But these **type annotations** we want to declare are not needed at *runtime*. - -In fact, remember that we used `list["Hero"]`, with a `"Hero"` in a string? - -For Python, at runtime, that is **just a string**. - -So, if we could add the type annotations we need using the **string versions**, Python wouldn't have a problem. - -But if we just put strings in the type annotations, without importing anything, the editor wouldn't know what we mean, and wouldn't be able to help us with **autocompletion** and **inline errors**. - -So, if there was a way to "import" some things that act as "imported" only while editing the code but not at *runtime*, that would solve it... And it exists! Exactly that. πŸŽ‰ - -### Import Only While Editing with `TYPE_CHECKING` - -To solve it, there's a special trick with a special variable `TYPE_CHECKING` in the `typing` module. - -It has a value of `True` for editors and tools that analyze the code with the type annotations. - -But when Python is executing, its value is `False`. - -So, we can use it in an `if` block and import things inside the `if` block. And they will be "imported" only for editors, but not at runtime. - -### Hero Model File - -Using that trick of `TYPE_CHECKING` we can "import" the `Team` in `hero_model.py`: - -```Python hl_lines="1 5-6 16" -{!./docs_src/tutorial/code_structure/tutorial002/hero_model.py!} -``` - -Have in mind that now we *have* to put the annotation of `Team` as a string: `"Team"`, so that Python doesn't have errors at runtime. - -### Team Model File - -We use the same trick in the `team_model.py` file: - -```Python hl_lines="1 5-6 14" -{!./docs_src/tutorial/code_structure/tutorial002/team_model.py!} -``` - -Now we get editor support, autocompletion, inline errors, and **SQLModel** keeps working. πŸŽ‰ - -### App File - -Now, just for completeness, the `app.py` file would import the models from both modules: - -```Python hl_lines="4-5 10 12-14" -{!./docs_src/tutorial/code_structure/tutorial002/app.py!} -``` - -And of course, all the tricks with `TYPE_CHECKING` and type annotations in strings are **only needed in the files with circular imports**. - -As there are no circular imports with `app.py`, we can just use normal imports and use the classes as normally here. - -And running that achieves the same result as before: - -
- -```console -$ python -m project.app - -Created hero: id=1 age=None name='Deadpond' secret_name='Dive Wilson' team_id=1 -Hero's team: id=1 name='Z-Force' headquarters='Sister Margaret's Bar' -``` - -
- -## Recap - -For the **simplest cases** (for most of the cases) you can just keep all the models in a single file, and structure the rest of the application (including setting up the **engine**) in as many files as you want. - -And for the **complex cases** that really need separating all the models in different files, you can use the `TYPE_CHECKING` to make it all work and still have the best developer experience with the best editor support. ✨ diff --git a/docs/tutorial/connect/create-connected-rows.md b/docs/tutorial/connect/create-connected-rows.md deleted file mode 100644 index 2f952bf706..0000000000 --- a/docs/tutorial/connect/create-connected-rows.md +++ /dev/null @@ -1,210 +0,0 @@ -# Create and Connect Rows - -We will now **create rows** for each table. ✨ - -The `team` table will look like this: - - - - - - - - - - - -
idnameheadquarters
1PreventersSharp Tower
2Z-ForceSister Margaret's Bar
- -And after we finish working with the data in this chapter, the `hero` table will look like this: - - - - - - - - - - - - - - -
idnamesecret_nameageteam_id
1DeadpondDive Wilsonnull2
2Rusty-ManTommy Sharp481
3Spider-BoyPedro Parqueadornullnull
- -Each row in the table `hero` will point to a row in the table `team`: - -table relationships - -/// info - -We will later update **Spider-Boy** to add him to the **Preventers** team too, but not yet. - -/// - -We will continue with the code in the previous example and we will add more things to it. - -{* ./docs_src/tutorial/connect/create_tables/tutorial001_py310.py ln[0] *} - -Make sure you remove the `database.db` file before running the examples to get the same results. - -## Create Rows for Teams with **SQLModel** - -Let's do the same we did before and define a `create_heroes()` function where we create our heroes. - -And now we will also create the teams there. πŸŽ‰ - -Let's start by creating two teams: - -{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[29:35] hl[29:35] *} - -This would hopefully look already familiar. - -We start a **session** in a `with` block using the same **engine** we created above. - -Then we create two instances of the model class (in this case `Team`). - -Next we add those objects to the **session**. - -And finally we **commit** the session to save the changes to the database. - -## Add It to Main - -Let's not forget to add this function `create_heroes()` to the `main()` function so that we run it when calling the program from the command line: - -{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[61:63] hl[63] *} - -## Run it - -If we run that code we have up to now, it will output: - -
- -```console -$ python app.py - -// Previous output omitted πŸ˜‰ - -// Automatically start a transaction -INFO Engine BEGIN (implicit) -// Add the teams to the database -INFO Engine INSERT INTO team (name, headquarters) VALUES (?, ?) -INFO Engine [generated in 0.00050s] ('Preventers', 'Sharp Tower') -INFO Engine INSERT INTO team (name, headquarters) VALUES (?, ?) -INFO Engine [cached since 0.002324s ago] ('Z-Force', 'Sister Margaret's Bar') -INFO Engine COMMIT -``` - -
- -You can see in the output that it uses common SQL `INSERT` statements to create the rows. - -## Create Rows for Heroes in Code - -Now let's create one hero object to start. - -As the `Hero` class model now has a field (column, attribute) `team_id`, we can set it by using the ID field from the `Team` objects we just created before: - -{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[29:39] hl[38] *} - -We haven't committed this hero to the database yet, but there are already a couple of things to pay **attention** to. - -If the database already had some teams, we wouldn't even know **what is the ID** that is going to be automatically assigned to each team by the database, for example, we couldn't just guess `1` or `2`. - -But once the team is created and committed to the database, we can access the object's `id` field to get that ID. - -Accessing an attribute in a model that was just committed, for example with `team_z_force.id`, automatically **triggers a refresh** of the data from the DB in the object, and then exposes the value for that field. - -So, even though we are not committing this hero yet, just because we are using `team_z_force.id`, that will trigger some SQL sent to the database to fetch the data for this team. - -That line alone would generate an output of: - -``` -INFO Engine BEGIN (implicit) -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team -WHERE team.id = ? -INFO Engine [generated in 0.00025s] (2,) -``` - -Let's now create two more heroes: - -{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[29:50] hl[40:46] *} - -When creating `hero_rusty_man`, we are accessing `team_preventers.id`, so that will also trigger a refresh of its data, generating an output of: - -``` -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team -WHERE team.id = ? -INFO Engine [cached since 0.001795s ago] (1,) -``` - -There's something else to note. We marked `team_id` as `int | None`, meaning that this could be `NULL` on the database (and `None` in Python). - -That means that a hero doesn't have to have a team. And in this case, **Spider-Boy** doesn't have one. - -Next we just commit the changes to save them to the database, and that will generate the output: - -``` -INFO Engine INSERT INTO hero (name, secret_name, age, team_id) VALUES (?, ?, ?, ?) -INFO Engine [generated in 0.00022s] ('Deadpond', 'Dive Wilson', None, 2) -INFO Engine INSERT INTO hero (name, secret_name, age, team_id) VALUES (?, ?, ?, ?) -INFO Engine [cached since 0.0007987s ago] ('Rusty-Man', 'Tommy Sharp', 48, 1) -INFO Engine INSERT INTO hero (name, secret_name, age, team_id) VALUES (?, ?, ?, ?) -INFO Engine [cached since 0.001095s ago] ('Spider-Boy', 'Pedro Parqueador', None, None) -INFO Engine COMMIT -``` - -## Refresh and Print Heroes - -Now let's refresh and print those new heroes to see their new ID pointing to their teams: - -{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[29:58] hl[52:54,56:58] *} - -If we execute that in the command line, it will output: - -
- -```console -$ python app.py - -// Previous output omitted πŸ˜‰ - -// Automatically start a transaction -INFO Engine BEGIN (implicit) - -// Refresh the first hero -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age, hero.team_id -FROM hero -WHERE hero.id = ? -INFO Engine [generated in 0.00021s] (1,) -// Refresh the second hero -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age, hero.team_id -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.001575s ago] (2,) -// Refresh the third hero -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age, hero.team_id -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.002518s ago] (3,) - -// Print the heroes -Created hero: id=1 secret_name='Dive Wilson' team_id=2 name='Deadpond' age=None -Created hero: id=2 secret_name='Tommy Sharp' team_id=1 name='Rusty-Man' age=48 -Created hero: id=3 secret_name='Pedro Parqueador' team_id=None name='Spider-Boy' age=None -``` - -
- -They now have their `team_id`s, nice! - -## Relationships - -Relationships in SQL databases are just made by having **columns in one table** referencing the values in **columns on other tables**. - -And here we have treated them just like that, more **column fields**, which is what they actually are behind the scenes in the SQL database. - -But later in this tutorial, in the next group of chapters, you will learn about **Relationship Attributes** to make it all a lot easier to work with in code. ✨ diff --git a/docs/tutorial/connect/create-connected-tables.md b/docs/tutorial/connect/create-connected-tables.md deleted file mode 100644 index 2b1a4f8758..0000000000 --- a/docs/tutorial/connect/create-connected-tables.md +++ /dev/null @@ -1,215 +0,0 @@ -# Create Connected Tables - -Now we will deal with **connected** data put in different tables. - -So, the first step is to create more than one table and connect them, so that each row in one table can reference another row in the other table. - -We have been working with heroes in a single table `hero`. Let's now add a table `team`. - -The team table will look like this: - - - - - - - - - - - -
idnameheadquarters
1PreventersSharp Tower
2Z-ForceSister Margaret's Bar
- -To connect them, we will add another column to the hero table to point to each team by the ID with the `team_id`: - - - - - - - - - - - - - - -
idnamesecret_nameageteam_id ✨
1DeadpondDive Wilsonnull2 ✨
2Spider-BoyPedro Parqueadornull1 ✨
3Rusty-ManTommy Sharp481 ✨
- -This way each row in the table `hero` can point to a row in the table `team`: - -table relationships - -## One-to-Many and Many-to-One - -Here we are creating connected data in a relationship where **one** team could have **many** heroes. So it is commonly called a **one-to-many** or **many-to-one** relationship. - -The **many-to-one** part can be seen if we start from the heroes, **many** heroes could be part of **one** team. - -This is probably the most popular type of relationship, so we'll start with that. But there's also **many-to-many** and **one-to-one** relationships. - -## Create Tables in Code - -### Create the `team` Table - -Let's start by creating the tables in code. - -Import the things we need from `sqlmodel` and create a new `Team` model: - -{* ./docs_src/tutorial/connect/create_tables/tutorial001_py310.py ln[1:7] hl[4:7] *} - -This is very similar to what we have been doing with the `Hero` model. - -The `Team` model will be in a table automatically named `"team"`, and it will have the columns: - -* `id`, the primary key, automatically generated by the database -* `name`, the name of the team - * We also tell **SQLModel** to create an index for this column -* `headquarters`, the headquarters of the team - -And finally we mark it as a table in the config. - -### Create the New `hero` Table - -Now let's create the `hero` table. - -This is the same model we have been using up to now, we are just adding the new column `team_id`: - -{* ./docs_src/tutorial/connect/create_tables/tutorial001_py310.py ln[1:16] hl[16] *} - -Most of that should look familiar: - -The column will be named `team_id`. It will be an integer, and it could be `NULL` in the database (or `None` in Python), because there could be some heroes that don't belong to any team. - -We add a default of `None` to the `Field()` so we don't have to explicitly pass `team_id=None` when creating a hero. - -Now, here's the new part: - -In `Field()` we pass the argument `foreign_key="team.id"`. This tells the database that this column `team_id` is a foreign key to the table `team`. A "**foreign key**" just means that this column will have the **key** to identify a row in a **foreign** table. - -The value in this column `team_id` will be the same integer that is in some row in the `id` column on the `team` table. That is what connects the two tables. - -#### The Value of `foreign_key` - -Notice that the `foreign_key` is a string. - -Inside it has the name of the **table**, then a dot, and then the name of the **column**. - -This is the name of the **table** in the database, so it is `"team"`, not the name of the **model** class `Team` (with a capital `T`). - -If you had a custom table name, you would use that custom table name. - -/// info - -You can learn about setting a custom table name for a model in the Advanced User Guide. - -/// - -### Create the Tables - -Now we can add the same code as before to create the engine and the function to create the tables: - -{* ./docs_src/tutorial/connect/create_tables/tutorial001_py310.py ln[19:26] hl[19:20,22,25:26] *} - -And as before, we'll call this function from another function `main()`, and we'll add that function `main()` to the main block of the file: - -{* ./docs_src/tutorial/connect/create_tables/tutorial001_py310.py ln[29:34] hl[29:30,33:34] *} - -## Run the Code - -/// tip - -Before running the code, make sure you delete the file `database.db` to make sure you start from scratch. - -/// - -If we run the code we have up to now, it will go and create the database file `database.db` and the tables in it we just defined, `team` and `hero`: - -
- -```console -$ python app.py - -// Automatically start a new transaction -INFO Engine BEGIN (implicit) - -// Check if the tables exist already -INFO Engine PRAGMA main.table_info("team") -INFO Engine [raw sql] () -INFO Engine PRAGMA temp.table_info("team") -INFO Engine [raw sql] () -INFO Engine PRAGMA main.table_info("hero") -INFO Engine [raw sql] () -INFO Engine PRAGMA temp.table_info("hero") -INFO Engine [raw sql] () - -// Create the tables -INFO Engine -CREATE TABLE team ( - id INTEGER, - name VARCHAR NOT NULL, - headquarters VARCHAR NOT NULL, - PRIMARY KEY (id) -) - - -INFO Engine [no key 0.00010s] () -INFO Engine -CREATE TABLE hero ( - id INTEGER, - name VARCHAR NOT NULL, - secret_name VARCHAR NOT NULL, - age INTEGER, - team_id INTEGER, - PRIMARY KEY (id), - FOREIGN KEY(team_id) REFERENCES team (id) -) - - -INFO Engine [no key 0.00026s] () -INFO Engine COMMIT -``` - -
- -## Create Tables in SQL - -Let's see that same generated SQL code. - -As we saw before, those `VARCHAR` columns are converted to `TEXT` in SQLite, which is the database we are using for these experiments. - -So, the first SQL could also be written as: - -```SQL -CREATE TABLE team ( - id INTEGER, - name TEXT NOT NULL, - headquarters TEXT NOT NULL, - PRIMARY KEY (id) -) -``` - -And the second table could be written as: - -```SQL hl_lines="8" -CREATE TABLE hero ( - id INTEGER, - name TEXT NOT NULL, - secret_name TEXT NOT NULL, - age INTEGER, - team_id INTEGER, - PRIMARY KEY (id), - FOREIGN KEY(team_id) REFERENCES team (id) -) -``` - -The only new is the `FOREIGN KEY` line, and as you can see, it tells the database what column in this table is a foreign key (`team_id`), which other (foreign) table it references (`team`) and which column in that table is the key to define which row to connect (`id`). - -Feel free to experiment with it in **DB Browser for SQLite**. - -## Recap - -Using **SQLModel**, in most of the cases you only need a field (column) with a `foreign_key` in the `Field()` with a string pointing to another table and column to connect two tables. - -Now that we have the tables created and connected, let's create some rows in the next chapter. πŸš€ diff --git a/docs/tutorial/connect/index.md b/docs/tutorial/connect/index.md deleted file mode 100644 index aa57e432fa..0000000000 --- a/docs/tutorial/connect/index.md +++ /dev/null @@ -1,15 +0,0 @@ -# Connect Tables - JOIN - Intro - -By this point, you already know how to perform the main CRUD operations with **SQLModel** using a single table. πŸŽ‰ - -But the main advantage and feature of SQL databases is being able to handle related data, to **connect** or **"join"** different tables together. Connecting rows in one table to rows in another. - -Let's see how to use **SQLModel** to manage connected data in the next chapters. 🀝 - -/// tip - -We will extend this further in the next group of chapters making it even more convenient to work with in Python code, using **relationship attributes**. - -But you should start in this group of chapters first. πŸ€“ - -/// diff --git a/docs/tutorial/connect/read-connected-data.md b/docs/tutorial/connect/read-connected-data.md deleted file mode 100644 index 3fd4607c92..0000000000 --- a/docs/tutorial/connect/read-connected-data.md +++ /dev/null @@ -1,488 +0,0 @@ -# Read Connected Data - -Now that we have some data in both tables, let's select the data that is connected together. - -The `team` table has this data: - - - - - - - - - - - -
idnameheadquarters
1PreventersSharp Tower
2Z-ForceSister Margaret's Bar
- -And the `hero` table has this data: - - - - - - - - - - - - - - -
idnamesecret_nameageteam_id
1DeadpondDive Wilsonnull2
2Rusty-ManTommy Sharp481
3Spider-BoyPedro Parqueadornullnull
- -We will continue with the code in the previous example and we will add more things to it. - -{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[0] *} - -## `SELECT` Connected Data with SQL - -Let's start seeing how SQL works when selecting connected data. This is where SQL databases actually shine. - -If you don't have a `database.db` file, run that previous program we had written (or copy it from the preview above) to create it. - -Now open **DB Browser for SQLite** and open the `database.db` file. - -To `SELECT` connected data we use the same keywords we have used before, but now we combine the two tables. - -Let's get each hero with the `id`, `name`, and the team `name`: - -```SQL -SELECT hero.id, hero.name, team.name -FROM hero, team -WHERE hero.team_id = team.id -``` - -/// info - -Because we have two columns called `name`, one for `hero` and one for `team`, we can specify them with the prefix of the table name and the dot to make it explicit what we refer to. - -/// - -Notice that now in the `WHERE` part we are not comparing one column with a literal value (like `hero.name = "Deadpond"`), but we are comparing two columns. - -It means, more or less: - -> Hey SQL database πŸ‘‹, please go and `SELECT` some data for me. -> -> I'll first tell you the columns I want: -> -> * `id` of the `hero` table -> * `name` of the `hero` table -> * `name` of the `team` table -> -> I want you to get that data `FROM` the tables `hero` and `team`. -> -> And I don't want you to combine each hero with each possible team. Instead, for each hero, go and check each possible team, but give me only the ones `WHERE` the `hero.team_id` is the same as the `team.id`. - -If we execute that SQL, it will return the table: - - - - - - - - - - - -
idnamename
1DeadpondZ-Force
2Rusty-ManPreventers
- -You can go ahead and try it in **DB Browser for SQLite**: - - - -/// note - -Wait, what about Spider-Boy? 😱 - -He doesn't have a team, so his `team_id` is `NULL` in the database. And this SQL is comparing that `NULL` from the `team_id` with all the `id` fields in the rows in the `team` table. - -As there's no team with an ID of `NULL`, it doesn't find a match. - -But we'll see how to fix that later with a `LEFT JOIN`. - -/// - -## Select Related Data with **SQLModel** - -Now let's use SQLModel to do the same select. - -We'll create a function `select_heroes()` just as we did before, but now we'll work with two tables. - -Remember SQLModel's `select()` function? It can take more than one argument. - -So, we can pass the `Hero` and `Team` model classes. And we can also use both their columns in the `.where()` part: - -{* ./docs_src/tutorial/connect/select/tutorial001_py310.py ln[61:63] hl[63] *} - -Notice that in the comparison with `==` we are using the class attributes for both `Hero.team_id` and `Team.id`. - -That will generate the appropriate **expression** object that will be converted to the right SQL, equivalent to the SQL example we saw above. - -Now we can execute it and get the `results` object. - -And as we used `select` with two models, we will receive tuples of instances of those two models, so we can iterate over them naturally in a `for` loop: - -{* ./docs_src/tutorial/connect/select/tutorial001_py310.py ln[61:66] hl[65] *} - -For each iteration in the `for` loop we get a a tuple with an instance of the class `Hero` and an instance of the class `Team`. - -And in this `for` loop we assign them to the variable `hero` and the variable `team`. - -/// info - -There was a lot of research, design, and work behind **SQLModel** to make this provide the best possible developer experience. - -And you should get autocompletion and inline errors in your editor for both `hero` and `team`. πŸŽ‰ - -/// - -## Add It to Main - -As always, we must remember to add this new `select_heroes()` function to the `main()` function to make sure it is executed when we call this program from the command line. - -{* ./docs_src/tutorial/connect/select/tutorial001_py310.py ln[69:72] hl[72] *} - -## Run the Program - -Now we can run the program and see how it shows us each hero with their corresponding team: - -
- -```console -$ python app.py - -// Previous output omitted πŸ˜‰ - -// Get the heroes with their teams -2021-08-09 08:55:50,682 INFO sqlalchemy.engine.Engine SELECT hero.id, hero.name, hero.secret_name, hero.age, hero.team_id, team.id AS id_1, team.name AS name_1, team.headquarters -FROM hero, team -WHERE hero.team_id = team.id -2021-08-09 08:55:50,682 INFO sqlalchemy.engine.Engine [no key 0.00015s] () - -// Print the first hero and team -Hero: id=1 secret_name='Dive Wilson' team_id=2 name='Deadpond' age=None Team: headquarters='Sister Margaret's Bar' id=2 name='Z-Force' - -// Print the second hero and team -Hero: id=2 secret_name='Tommy Sharp' team_id=1 name='Rusty-Man' age=48 Team: headquarters='Sharp Tower' id=1 name='Preventers' -2021-08-09 08:55:50,682 INFO sqlalchemy.engine.Engine ROLLBACK -``` - -
- -## `JOIN` Tables with SQL - -There's an alternative syntax for that SQL query from above using the keyword `JOIN` instead of `WHERE`. - -This is the same version from above, using `WHERE`: - -```SQL -SELECT hero.id, hero.name, team.name -FROM hero, team -WHERE hero.team_id = team.id -``` - -And this is the alternative version using `JOIN`: - -```SQL -SELECT hero.id, hero.name, team.name -FROM hero -JOIN team -ON hero.team_id = team.id -``` - -Both are equivalent. The differences in the SQL code are that instead of passing the `team` to the `FROM` part (also called `FROM` clause) we add a `JOIN` and put the `team` table there. - -And then, instead of putting a `WHERE` with a condition, we put an `ON` keyword with the condition, because `ON` is the one that comes with `JOIN`. 🀷 - -So, this second version means, more or less: - -> Hey SQL database πŸ‘‹, please go and `SELECT` some data for me. -> -> I'll first tell you the columns I want: -> -> * `id` of the `hero` table -> * `name` of the `hero` table -> * `name` of the `team` table -> -> ...up to here it's the same as before, LOL. -> -> Now, I want you to get that data starting `FROM` the table `hero`. -> -> And to get the rest of the data, I want you to `JOIN` it with the table `team`. -> -> And I want you to join those two tables `ON` the combinations of rows that have the `hero.team_id` with the same value as the `team.id`. -> -> Did I say all this before already? I feel like I'm just repeating myself. πŸ€” - -That will return the same table as before: - - - - - - - - - - - -
idnamename
1DeadpondZ-Force
2Rusty-ManPreventers
- -Also in **DB Browser for SQLite**: - - - -/// tip - -Why bother with all this if the result is the same? - -This `JOIN` will be useful in a bit to be able to also get Spider-Boy, even if he doesn't have a team. - -/// - -## Join Tables in **SQLModel** - -The same way there's a `.where()` available when using `select()`, there's also a `.join()`. - -And in SQLModel (actually SQLAlchemy), when using the `.join()`, because we already declared what is the `foreign_key` when creating the models, we don't have to pass an `ON` part, it is inferred automatically: - -{* ./docs_src/tutorial/connect/select/tutorial002_py310.py ln[61:66] hl[63] *} - -Also notice that we are still including `Team` in the `select(Hero, Team)`, because we still want to access that data. - -This is equivalent to the previous example. - -And if we run it in the command line, it will output: - -
- -```console -$ python app.py - -// Previous output omitted πŸ˜‰ - -// Select using a JOIN with automatic ON -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age, hero.team_id, team.id AS id_1, team.name AS name_1, team.headquarters -FROM hero JOIN team ON team.id = hero.team_id -INFO Engine [no key 0.00032s] () - -// Print the first hero and team -Hero: id=1 secret_name='Dive Wilson' team_id=2 name='Deadpond' age=None Team: headquarters='Sister Margaret's Bar' id=2 name='Z-Force' - -// Print the second hero and team -Hero: id=2 secret_name='Tommy Sharp' team_id=1 name='Rusty-Man' age=48 Team: headquarters='Sharp Tower' id=1 name='Preventers' - -``` - -
- -## `JOIN` Tables with SQL and `LEFT OUTER` (Maybe `JOIN`) - -When working with a `JOIN`, you can imagine that you start with a table on the `FROM` part and put that table in an imaginary space on the **left** side. - -And then you want another table to `JOIN` the result. - -And you put that second table in the **right** side on that imaginary space. - -And then you tell the database `ON` which condition it should join those two tables and give you the results back. - -But by default, only the rows from both left and right that match the condition will be returned. - -table relationships - -In this example of tables above πŸ‘†, it would return all the heroes, because every hero has a `team_id`, so every hero can be joined with the `team` table: - - - - - - - - - - - - - - -
idnamename
1DeadpondZ-Force
2Rusty-ManPreventers
3Spider-BoyPreventers
- -### Foreign Keys with `NULL` - -But in the database that we are working with in the code above, **Spider-Boy** doesn't have any team, the value of `team_id` is `NULL` in the database. - -So there's no way to join the **Spider-Boy** row with some row in the `team` table: - -table relationships - -Running the same SQL we used above, the resulting table would not include **Spider-Boy** 😱: - - - - - - - - - - - -
idnamename
1DeadpondZ-Force
2Rusty-ManPreventers
- -### Include Everything on the `LEFT OUTER` - -In this case, that we want to include all heroes in the result even if they don't have a team, we can extend that same SQL using a `JOIN` from above and add a `LEFT OUTER` right before `JOIN`: - -```SQL hl_lines="3" -SELECT hero.id, hero.name, team.name -FROM hero -LEFT OUTER JOIN team -ON hero.team_id = team.id -``` - -This `LEFT OUTER` part tells the database that we want to keep everything on the first table, the one on the `LEFT` in the imaginary space, even if those rows would be left **out**, so we want it to include the `OUTER` rows too. In this case, every hero with or without a team. - -And that would return the following result, including **Spider-Boy** πŸŽ‰: - - - - - - - - - - - - - - -
idnamename
1DeadpondZ-Force
2Rusty-ManPreventers
3Spider-Boynull
- -/// tip - -The only difference between this query and the previous is that extra `LEFT OUTER`. - -/// - -And here's another of the SQL variations, you could write `LEFT OUTER JOIN` or just `LEFT JOIN`, it means the same. - -## Join Tables in **SQLModel** with `LEFT OUTER` - -Now let's replicate the same query in **SQLModel**. - -`.join()` has a parameter we can use `isouter=True` to make the `JOIN` be a `LEFT OUTER JOIN`: - -{* ./docs_src/tutorial/connect/select/tutorial003_py310.py ln[61:66] hl[63] *} - -And if we run it, it will output: - -
- -```console -$ python app.py - -// Previous output omitted πŸ˜‰ - -// SELECT using LEFT OUTER JOIN -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age, hero.team_id, team.id AS id_1, team.name AS name_1, team.headquarters -FROM hero LEFT OUTER JOIN team ON team.id = hero.team_id - -INFO Engine [no key 0.00051s] () - -// Print the first hero and team -Hero: id=1 secret_name='Dive Wilson' team_id=2 name='Deadpond' age=None Team: headquarters='Sister Margaret's Bar' id=2 name='Z-Force' -// Print the second hero and team -Hero: id=2 secret_name='Tommy Sharp' team_id=1 name='Rusty-Man' age=48 Team: headquarters='Sharp Tower' id=1 name='Preventers' -// Print the third hero and team, we included Spider-Boy πŸŽ‰ -Hero: id=3 secret_name='Pedro Parqueador' team_id=None name='Spider-Boy' age=None Team: None -``` - -
- -## What Goes in `select()` - -You might be wondering why we put the `Team` in the `select()` and not just in the `.join()`. - -And then why we didn't include `Hero` in the `.join()`. πŸ€” - -In SQLModel (actually in SQLAlchemy), all these functions and tools try to **replicate** how it would be to work with the **SQL** language. - -Remember that [`SELECT` defines the columns to get and `WHERE` how to filter them?](../where.md#select-and-where){.internal-link target=_blank}. - -This also applies here, but with `JOIN` and `ON`. - -### Select Only Heroes But Join with Teams - -If we only put the `Team` in the `.join()` and not in the `select()` function, we would not get the `team` data. - -But we would still be able to **filter** the rows with it. πŸ€“ - -We could even add some additional `.where()` after `.join()` to filter the data more, for example to return only the heroes from one team: - -{* ./docs_src/tutorial/connect/select/tutorial004_py310.py ln[61:66] hl[63] *} - -Here we are **filtering** with `.where()` to get only the heroes that belong to the **Preventers** team. - -But we are still only requesting the data from the heroes, not their teams. - -If we run that, it would output: - -
- -```console -$ python app.py - -// Select only the hero data -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age, hero.team_id -// But still join with the team table -FROM hero JOIN team ON team.id = hero.team_id -// And filter with WHERE to get only the Preventers -WHERE team.name = ? -INFO Engine [no key 0.00066s] ('Preventers',) - -// We filter with the team, but only get the hero -Preventer Hero: id=2 secret_name='Tommy Sharp' team_id=1 name='Rusty-Man' age=48 -``` - -
- -### Include the `Team` - -By putting the `Team` in `select()` we tell **SQLModel** and the database that we want the team data too. - -{* ./docs_src/tutorial/connect/select/tutorial005_py310.py ln[61:66] hl[63] *} - -And if we run that, it will output: - -
- -```console -$ python app.py - -// Select the hero and the team data -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age, hero.team_id, team.id AS id_1, team.name AS name_1, team.headquarters -// Join the hero with the team table -FROM hero JOIN team ON team.id = hero.team_id -// Filter with WHERE to get only Preventers -WHERE team.name = ? -INFO Engine [no key 0.00018s] ('Preventers',) - -// Print the hero and the team -Preventer Hero: id=2 secret_name='Tommy Sharp' team_id=1 name='Rusty-Man' age=48 Team: headquarters='Sharp Tower' id=1 name='Preventers' -``` - -
- -We still have to `.join()` because otherwise it would just compute all the possible combinations of heroes and teams, for example including **Rusty-Man** with **Preventers** and also **Rusty-Man** with **Z-Force**, which would be a mistake. - -## Relationship Attributes - -Here we have been using the pure class models directly, but in a future chapter we will also see how to use **Relationship Attributes** that let us interact with the database in a way much more close to the code with Python objects. - -And we will also see how to load their data in a different, simpler way, achieving the same we achieved here. ✨ diff --git a/docs/tutorial/connect/remove-data-connections.md b/docs/tutorial/connect/remove-data-connections.md deleted file mode 100644 index d0a0267b1f..0000000000 --- a/docs/tutorial/connect/remove-data-connections.md +++ /dev/null @@ -1,82 +0,0 @@ -# Remove Data Connections - -We currently have a `team` table: - - - - - - - - - - - -
idnameheadquarters
1PreventersSharp Tower
2Z-ForceSister Margaret's Bar
- -And a `hero` table: - - - - - - - - - - - - - - -
idnamesecret_nameageteam_id
1DeadpondDive Wilsonnull2
2Rusty-ManTommy Sharp481
3Spider-BoyPedro Parqueadornull1
- -Let's see how to **remove** connections between rows in tables. - -We will continue with the code from the previous chapter. - -{* ./docs_src/tutorial/connect/update/tutorial001_py310.py ln[0] *} - -## Break a Connection - -We don't really have to delete anything to break a connection. We can just assign `None` to the foreign key, in this case, to the `team_id`. - -Let's say **Spider-Boy** is tired of the lack of friendly neighbors and wants to get out of the **Preventers**. - -We can simply set the `team_id` to `None`, and now it doesn't have a connection with the team: - -{* ./docs_src/tutorial/connect/delete/tutorial001_py310.py ln[29:30,66:70] hl[66] *} - -Again, we just **assign** a value to that field attribute `team_id`, now the value is `None`, which means `NULL` in the database. Then we `add()` the hero to the session, and then `commit()`. - -Next we `refresh()` it to get the recent data, and we print it. - -Running that in the command line will output: - -
- -```console -$ python app.py - -// Previous output omitted πŸ˜‰ - -// Update the hero -INFO Engine UPDATE hero SET team_id=? WHERE hero.id = ? -INFO Engine [cached since 0.07753s ago] (None, 3) -// Commit the session -INFO Engine COMMIT -// Automatically start a new transaction -INFO Engine BEGIN (implicit) -// Refresh the hero -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age, hero.team_id -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.1661s ago] (3,) - -// Print the hero without a team -No longer Preventer: id=3 secret_name='Pedro Parqueador' team_id=None name='Spider-Boy' age=None -``` - -
- -That's it, we now removed a connection between rows in different tables by unsetting the foreign key column. πŸ’₯ diff --git a/docs/tutorial/connect/update-data-connections.md b/docs/tutorial/connect/update-data-connections.md deleted file mode 100644 index 147c4e599d..0000000000 --- a/docs/tutorial/connect/update-data-connections.md +++ /dev/null @@ -1,84 +0,0 @@ -# Update Data Connections - -At this point we have a `team` table: - - - - - - - - - - - -
idnameheadquarters
1PreventersSharp Tower
2Z-ForceSister Margaret's Bar
- -And a `hero` table: - - - - - - - - - - - - - - -
idnamesecret_nameageteam_id
1DeadpondDive Wilsonnull2
2Rusty-ManTommy Sharp481
3Spider-BoyPedro Parqueadornullnull
- -Some of these heroes are part of a team. - -Now we'll see how to **update** those connections between rows tables. - -We will continue with the code we used to create some heroes, and we'll update them. - -{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[0] *} - -## Assign a Team to a Hero - -Let's say that **Tommy Sharp** uses his "rich uncle" charms to recruit **Spider-Boy** to join the team of the **Preventers**, now we need to update our Spider-Boy hero object to connect it to the Preventers team. - -Doing it is just like updating any other field: - -{* ./docs_src/tutorial/connect/update/tutorial001_py310.py ln[29:30,60:64] hl[60] *} - -We can simply **assign** a value to that field attribute `team_id`, then `add()` the hero to the session, and then `commit()`. - -Next we `refresh()` it to get the recent data, and we print it. - -Running that in the command line will output: - -
- -```console -$ python app.py - -// Previous output omitted πŸ˜‰ - -// Update the hero -INFO Engine UPDATE hero SET team_id=? WHERE hero.id = ? -INFO Engine [generated in 0.00014s] (1, 3) -// Commit the session saving the changes -INFO Engine COMMIT -// Automatically start a new transaction -INFO Engine BEGIN (implicit) -// Refresh the hero data -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age, hero.team_id -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.08837s ago] (3,) - -// Print the updated hero -Updated hero: id=3 secret_name='Pedro Parqueador' team_id=1 name='Spider-Boy' age=None -``` - -
- -And now **Spider-Boy** has the `team_id=1`, which is the ID of the Preventers. πŸŽ‰ - -Let's now see how to remove connections in the next chapter. πŸ’₯ diff --git a/docs/tutorial/create-db-and-table-with-db-browser.md b/docs/tutorial/create-db-and-table-with-db-browser.md deleted file mode 100644 index 4c535df11c..0000000000 --- a/docs/tutorial/create-db-and-table-with-db-browser.md +++ /dev/null @@ -1,174 +0,0 @@ -# Create a Table with SQL - -Let's get started! - -We will: - -* Create a SQLite database with **DB Browser for SQLite** -* Create a table in the database with **DB Browser for SQLite** - -We'll add data later. For now, we'll create the database and the first table structure. - -We will create a table to hold this data: - - - - - - - - - - - - - - -
idnamesecret_nameage
1DeadpondDive Wilsonnull
2Spider-BoyPedro Parqueadornull
3Rusty-ManTommy Sharp48
- -## Create a Database - -**SQLModel** and SQLAlchemy are based on SQL. - -They are designed to help you with using SQL through Python classes and objects. But it's still always very useful to understand SQL. - -So let's start with a simple, pure SQL example. - -Open **DB Browser for SQLite**. - -Click the button New Database. - - - -A dialog should show up. Go to the [project directory you created](../virtual-environments.md#create-a-project){.internal-link target=_blank} and save the file with a name of `database.db`. - -/// tip - -It's common to save SQLite database files with an extension of `.db`. Sometimes also `.sqlite`. - -/// - -## Create a Table - -After doing that, it might prompt you to create a new table right away. - -If it doesn't, click the button Create Table. - - - -Then you will see the dialog to create a new table. - -So, let's create a new table called `hero` with the following columns: - -* `id`: an `INTEGER` that will be the **primary key** (check `PK` βœ…). -* `name`: a `TEXT`, it should be `NOT NULL` (check `NN` βœ…), so, it should always have a value. -* `secret_name`: a `TEXT`, it should be `NOT NULL` too (check `NN` βœ…). -* `age`: an `INTEGER`, this one can be `NULL`, so you don't have to check anything else. - - - -Click OK to create the table. - -While you click on the Add button and add the information, it will create and update the SQL statement that is executed to create the table: - -```{ .sql .annotate } -CREATE TABLE "hero" ( --(1) - "id" INTEGER, --(2) - "name" TEXT NOT NULL, --(3) - "secret_name" TEXT NOT NULL, --(4) - "age" INTEGER, --(5) - PRIMARY KEY("id") --(6) -); --(7) -``` - -1. Create a table with the name `hero`. Also notice that the columns for this table are declared inside the parenthesis " `(`" that starts here. -2. The `id` column, an `INTEGER`. This is declared as the primary key at the end. -3. The `name` column, a `TEXT`, and it should always have a value `NOT NULL`. -4. The `secret_name` column, another `TEXT`, also `NOT NULL`. -5. The `age` column, an `INTEGER`. This one doesn't have `NOT NULL`, so it *can* be `NULL`. -6. The `PRIMARY KEY` of all this is the `id` column. -7. This is the end of the SQL table, with the final parenthesis "`)`". It also has the semicolon "`;`" that marks the end of the SQL statement. There could be more SQL statements in the same SQL string. - -Now you will see that it shows up in the list of Tables with the columns we specified. πŸŽ‰ - - - -The only step left is to click Write Changes to save the changes to the file. - - - -After that, the new table is saved in this database on the file `./database.db`. - -## Confirm the Table - -Let's confirm that it's all saved. - -First click the button Close Database to close the database. - - - -Now click on Open Database to open the database again, and select the same file `./database.db`. - - - -You will see again the same table we created. - - - -## Create the Table again, with SQL - -Now, to see how is it that SQL works, let's create the table again, but with SQL. - -Click the Close Database button again. - -And delete that `./database.db` file in your project directory. - -And click again on New Database. - -Save the file with the name `database.db` again. - -This time, if you see the dialog to create a new table, just close it by clicking the Cancel button. - -And now, go to the tab Execute SQL. - -Write the same SQL that was generated in the previous step: - -```SQL -CREATE TABLE "hero" ( - "id" INTEGER, - "name" TEXT NOT NULL, - "secret_name" TEXT NOT NULL, - "age" INTEGER, - PRIMARY KEY("id") -); -``` - -Then click the "Execute all" β–Ά button. - - - -You will see the "execution finished successfully" message. - - - -And if you go back to the Database Structure tab, you will see that you effectively created again the same table. - - - -## Learn More SQL - -I will keep showing you small bits of SQL through this tutorial. And you don't have to be a SQL expert to use **SQLModel**. - -But if you are curious and want to get a quick overview of SQL, I recommend the visual documentation from SQLite, on SQL As Understood By SQLite. - -You can start with `CREATE TABLE`. - -Of course, you can also go and take a full SQL course or read a book about SQL, but you don't need more than what I'll explain here on the tutorial to start being productive with **SQLModel**. πŸ€“ - -## Recap - -We saw how to interact with SQLite databases in files using **DB Browser for SQLite** in a visual user interface. - -We also saw how to use it to write some SQL directly to the SQLite database. This will be useful to verify the data in the database is looking correctly, to debug, etc. - -In the next chapters we will start using **SQLModel** to interact with the database, and we will continue to use **DB Browser for SQLite** at the same time to look at the database underneath. πŸ” diff --git a/docs/tutorial/create-db-and-table.md b/docs/tutorial/create-db-and-table.md deleted file mode 100644 index 688567ed4d..0000000000 --- a/docs/tutorial/create-db-and-table.md +++ /dev/null @@ -1,587 +0,0 @@ -# Create a Table with SQLModel - Use the Engine - -Now let's get to the code. πŸ‘©β€πŸ’» - -Make sure you are inside of your project directory and with your virtual environment activated as explained in [Virtual Environments](../virtual-environments.md#create-a-project){.internal-link target=_blank}. - -We will: - -* Define a table with **SQLModel** -* Create the same SQLite database and table with **SQLModel** -* Use **DB Browser for SQLite** to confirm the operations - -Here's a reminder of the table structure we want: - - - - - - - - - - - - - - -
idnamesecret_nameage
1DeadpondDive Wilsonnull
2Spider-BoyPedro Parqueadornull
3Rusty-ManTommy Sharp48
- -## Create the Table Model Class - -The first thing we need to do is create a class to represent the data in the table. - -A class like this that represents some data is commonly called a **model**. - -/// tip - -That's why this package is called `SQLModel`. Because it's mainly used to create **SQL Models**. - -/// - -For that, we will import `SQLModel` (plus other things we will also use) and create a class `Hero` that inherits from `SQLModel` and represents the **table model** for our heroes: - -{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:8] hl[1,4] *} - -This class `Hero` **represents the table** for our heroes. And each instance we create later will **represent a row** in the table. - -We use the config `table=True` to tell **SQLModel** that this is a **table model**, it represents a table. - -/// info - -It's also possible to have models without `table=True`, those would be only **data models**, without a table in the database, they would not be **table models**. - -Those **data models** will be **very useful later**, but for now, we'll just keep adding the `table=True` configuration. - -/// - -## Define the Fields, Columns - -The next step is to define the fields or columns of the class by using standard Python type annotations. - -The name of each of these variables will be the name of the column in the table. - -And the type of each of them will also be the type of table column: - -{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:8] hl[1,5:8] *} - -Let's now see with more detail these field/column declarations. - -### `None` Fields, Nullable Columns - -Let's start with `age`, notice that it has a type of `int | None`. - -That is the standard way to declare that something "could be an `int` or `None`" in Python. - -And we also set the default value of `age` to `None`. - -{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:8] hl[8] *} - -/// tip - -We also define `id` with `int | None`. But we will talk about `id` below. - -/// - -Because the type is `int | None`: - -* When validating data, `None` will be an allowed value for `age`. -* In the database, the column for `age` will be allowed to have `NULL` (the SQL equivalent to Python's `None`). - -And because there's a default value `= None`: - -* When validating data, this `age` field won't be required, it will be `None` by default. -* When saving to the database, the `age` column will have a `NULL` value by default. - -/// tip - -The default value could have been something else, like `= 42`. - -/// - -### Primary Key `id` - -Now let's review the `id` field. This is the **primary key** of the table. - -So, we need to mark `id` as the **primary key**. - -To do that, we use the special `Field` function from `sqlmodel` and set the argument `primary_key=True`: - -{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:8] hl[1,5] *} - -That way, we tell **SQLModel** that this `id` field/column is the primary key of the table. - -But inside the SQL database, it is **always required** and can't be `NULL`. Why should we declare it with `int | None`? - -The `id` will be required in the database, but it will be *generated by the database*, not by our code. - -So, whenever we create an instance of this class (in the next chapters), we *will not set the `id`*. And the value of `id` will be `None` **until we save it in the database**, and then it will finally have a value. - -```Python -my_hero = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") - -do_something(my_hero.id) # Oh no! my_hero.id is None! 😱🚨 - -# Imagine this saves it to the database -somehow_save_in_db(my_hero) - -do_something(my_hero.id) # Now my_hero.id has a value generated in DB πŸŽ‰ -``` - -So, because in *our code* (not in the database) the value of `id` *could be* `None`, we use `int | None`. This way **the editor will be able to help us**, for example, if we try to access the `id` of an object that we haven't saved in the database yet and would still be `None`. - - - -Now, because we are taking the place of the default value with our `Field()` function, we set **the actual default value** of `id` to `None` with the argument `default=None` in `Field()`: - -```Python -Field(default=None) -``` - -If we didn't set the `default` value, whenever we use this model later to do data validation (powered by Pydantic) it would *accept* a value of `None` apart from an `int`, but it would still **require** passing that `None` value. And it would be confusing for whoever is using this model later (probably us), so **better set the default value here**. - -## Create the Engine - -Now we need to create the SQLAlchemy **Engine**. - -It is an object that handles the communication with the database. - -If you have a server database (for example PostgreSQL or MySQL), the **engine** will hold the **network connections** to that database. - -Creating the **engine** is very simple, just call `create_engine()` with a URL for the database to use: - -{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:16] hl[1,14] *} - -You should normally have a single **engine** object for your whole application and re-use it everywhere. - -/// tip - -There's another related thing called a **Session** that normally should *not* be a single object per application. - -But we will talk about it later. - -/// - -### Engine Database URL - -Each supported database has its own URL type. For example, for **SQLite** it is `sqlite:///` followed by the file path. For example: - -* `sqlite:///database.db` -* `sqlite:///databases/local/application.db` -* `sqlite:///db.sqlite` - -SQLite supports a special database that lives all *in memory*. Hence, it's very fast, but be careful, the database gets deleted after the program terminates. You can specify this in-memory database by using just two slash characters (`//`) and no file name: - -* `sqlite://` - -{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:16] hl[11:12,14] *} - -You can read a lot more about all the databases supported by **SQLAlchemy** (and that way supported by **SQLModel**) in the SQLAlchemy documentation. - -### Engine Echo - -In this example, we are also using the argument `echo=True`. - -It will make the engine print all the SQL statements it executes, which can help you understand what's happening. - -It is particularly useful for **learning** and **debugging**: - -{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py ln[1:16] hl[14] *} - -But in production, you would probably want to remove `echo=True`: - -```Python -engine = create_engine(sqlite_url) -``` - -### Engine Technical Details - -/// tip - -If you didn't know about SQLAlchemy before and are just learning **SQLModel**, you can probably skip this section, scroll below. - -/// - -You can read a lot more about the engine in the SQLAlchemy documentation. - -**SQLModel** defines its own `create_engine()` function. It is the same as SQLAlchemy's `create_engine()`, but with the difference that it defaults to use `future=True` (which means that it uses the style of the latest SQLAlchemy, 1.4, and the future 2.0). - -And SQLModel's version of `create_engine()` is type annotated internally, so your editor will be able to help you with autocompletion and inline errors. - -## Create the Database and Table - -Now everything is in place to finally create the database and table: - -{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py hl[16] *} - -/// tip - -Creating the engine doesn't create the `database.db` file. - -But once we run `SQLModel.metadata.create_all(engine)`, it creates the `database.db` file **and** creates the `hero` table in that database. - -Both things are done in this single step. - -/// - -Let's unwrap that: - -```Python -SQLModel.metadata.create_all(engine) -``` - -### SQLModel MetaData - -The `SQLModel` class has a `metadata` attribute. It is an instance of a class `MetaData`. - -Whenever you create a class that inherits from `SQLModel` **and is configured with `table = True`**, it is registered in this `metadata` attribute. - -So, by the last line, `SQLModel.metadata` already has the `Hero` registered. - -### Calling `create_all()` - -This `MetaData` object at `SQLModel.metadata` has a `create_all()` method. - -It takes an **engine** and uses it to create the database and all the tables registered in this `MetaData` object. - -### SQLModel MetaData Order Matters - -This also means that you have to call `SQLModel.metadata.create_all()` *after* the code that creates new model classes inheriting from `SQLModel`. - -For example, let's imagine you do this: - -* Create the models in one Python file `models.py`. -* Create the engine object in a file `db.py`. -* Create your main app and call `SQLModel.metadata.create_all()` in `app.py`. - -If you only imported `SQLModel` and tried to call `SQLModel.metadata.create_all()` in `app.py`, it would not create your tables: - -```Python -# This wouldn't work! 🚨 -from sqlmodel import SQLModel - -from .db import engine - -SQLModel.metadata.create_all(engine) -``` - -It wouldn't work because when you import `SQLModel` alone, Python doesn't execute all the code creating the classes inheriting from it (in our example, the class `Hero`), so `SQLModel.metadata` is still empty. - -But if you import the models *before* calling `SQLModel.metadata.create_all()`, it will work: - -```Python -from sqlmodel import SQLModel - -from . import models -from .db import engine - -SQLModel.metadata.create_all(engine) -``` - -This would work because by importing the models, Python executes all the code creating the classes inheriting from `SQLModel` and registering them in the `SQLModel.metadata`. - -As an alternative, you could import `SQLModel` and your models inside of `db.py`: - -```Python -# db.py -from sqlmodel import SQLModel, create_engine -from . import models - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url) -``` - -And then import `SQLModel` *from* `db.py` in `app.py`, and there call `SQLModel.metadata.create_all()`: - -```Python -# app.py -from .db import engine, SQLModel - -SQLModel.metadata.create_all(engine) -``` - -The import of `SQLModel` from `db.py` would work because `SQLModel` is also imported in `db.py`. - -And this trick would work correctly and create the tables in the database because by importing `SQLModel` from `db.py`, Python executes all the code creating the classes that inherit from `SQLModel` in that `db.py` file, for example, the class `Hero`. - -## Migrations - -For this simple example, and for most of the **Tutorial - User Guide**, using `SQLModel.metadata.create_all()` is enough. - -But for a production system you would probably want to use a system to migrate the database. - -This would be useful and important, for example, whenever you add or remove a column, add a new table, change a type, etc. - -But you will learn about migrations later in the Advanced User Guide. - -## Run The Program - -Let's run the program to see it all working. - -Put the code it in a file `app.py` if you haven't already. - -{* ./docs_src/tutorial/create_db_and_table/tutorial001_py310.py *} - -/// tip - -Remember to [activate the virtual environment](../virtual-environments.md#create-a-virtual-environment){.internal-link target=_blank} before running it. - -/// - -Now run the program with Python: - -
- -```console -// We set echo=True, so this will show the SQL code -$ python app.py - -// First, some boilerplate SQL that we are not that interested in - -INFO Engine BEGIN (implicit) -INFO Engine PRAGMA main.table_info("hero") -INFO Engine [raw sql] () -INFO Engine PRAGMA temp.table_info("hero") -INFO Engine [raw sql] () -INFO Engine - -// Finally, the glorious SQL to create the table ✨ - -CREATE TABLE hero ( - id INTEGER, - name VARCHAR NOT NULL, - secret_name VARCHAR NOT NULL, - age INTEGER, - PRIMARY KEY (id) -) - -// More SQL boilerplate - -INFO Engine [no key 0.00020s] () -INFO Engine COMMIT -``` - -
- -/// info - -I simplified the output above a bit to make it easier to read. - -But in reality, instead of showing: - -``` -INFO Engine BEGIN (implicit) -``` - -it would show something like: - -``` -2021-07-25 21:37:39,175 INFO sqlalchemy.engine.Engine BEGIN (implicit) -``` - -/// - -### `TEXT` or `VARCHAR` - -In the example in the previous chapter we created the table using `TEXT` for some columns. - -But in this output SQLAlchemy is using `VARCHAR` instead. Let's see what's going on. - -Remember that [each SQL Database has some different variations in what they support?](../databases.md#sql-the-language){.internal-link target=_blank} - -This is one of the differences. Each database supports some particular **data types**, like `INTEGER` and `TEXT`. - -Some databases have some particular types that are special for certain things. For example, PostgreSQL and MySQL support `BOOLEAN` for values of `True` and `False`. SQLite accepts SQL with booleans, even when defining table columns, but what it actually uses internally are `INTEGER`s, with `1` to represent `True` and `0` to represent `False`. - -The same way, there are several possible types for storing strings. SQLite uses the `TEXT` type. But other databases like PostgreSQL and MySQL use the `VARCHAR` type by default, and `VARCHAR` is one of the most common data types. - -**`VARCHAR`** comes from **variable** length **character**. - -SQLAlchemy generates the SQL statements to create tables using `VARCHAR`, and then SQLite receives them, and internally converts them to `TEXT`s. - -Additional to the difference between those two data types, some databases like MySQL require setting a maximum length for the `VARCHAR` types, for example `VARCHAR(255)` sets the maximum number of characters to 255. - -To make it easier to start using **SQLModel** right away independent of the database you use (even with MySQL), and without any extra configurations, by default, `str` fields are interpreted as `VARCHAR` in most databases and `VARCHAR(255)` in MySQL, this way you know the same class will be compatible with the most popular databases without extra effort. - -/// tip - -You will learn how to change the maximum length of string columns later in the Advanced Tutorial - User Guide. - -/// - -### Verify the Database - -Now, open the database with **DB Browser for SQLite**, you will see that the program created the table `hero` just as before. πŸŽ‰ - - - -## Refactor Data Creation - -Now let's restructure the code a bit to make it easier to **reuse**, **share**, and **test** later. - -Let's move the code that has the main **side effects**, that changes data (creates a file with a database and a table) to a function. - -In this example it's just the `SQLModel.metadata.create_all(engine)`. - -Let's put it in a function `create_db_and_tables()`: - -{* ./docs_src/tutorial/create_db_and_table/tutorial002_py310.py ln[1:18] hl[17:18] *} - -If `SQLModel.metadata.create_all(engine)` was not in a function and we tried to import something from this module (from this file) in another, it would try to create the database and table **every time** we executed that other file that imported this module. - -We don't want that to happen like that, only when we **intend** it to happen, that's why we put it in a function, because we can make sure that the tables are created only when we call that function, and not when this module is imported somewhere else. - -Now we would be able to, for example, import the `Hero` class in some other file without having those **side effects**. - -/// tip - -πŸ˜… **Spoiler alert**: The function is called `create_db_and_tables()` because we will have more **tables** in the future with other classes apart from `Hero`. πŸš€ - -/// - -### Create Data as a Script - -We prevented the side effects when importing something from your `app.py` file. - -But we still want it to **create the database and table** when we call it with Python directly as an independent script from the terminal, just as above. - -/// tip - -Think of the word **script** and **program** as interchangeable. - -The word **script** often implies that the code could be run independently and easily. Or in some cases it refers to a relatively simple program. - -/// - -For that we can use the special variable `__name__` in an `if` block: - -{* ./docs_src/tutorial/create_db_and_table/tutorial002_py310.py hl[21:22] *} - -### About `__name__ == "__main__"` - -The main purpose of the `__name__ == "__main__"` is to have some code that is executed when your file is called with: - -
- -```console -$ python app.py - -// Something happens here ✨ -``` - -
- -...but is not called when another file imports it, like in: - -```Python -from app import Hero -``` - -/// tip - -That `if` block using `if __name__ == "__main__":` is sometimes called the "**main block**". - -The official name (in the Python docs) is "**Top-level script environment**". - -/// - -#### More details - -Let's say your file is named `myapp.py`. - -If you run it with: - -
- -```console -$ python myapp.py - -// This will call create_db_and_tables() -``` - -
- -...then the internal variable `__name__` in your file, created automatically by Python, will have as value the string `"__main__"`. - -So, the function in: - -```Python hl_lines="2" -if __name__ == "__main__": - create_db_and_tables() -``` - -...will run. - ---- - -This won't happen if you import that module (file). - -So, if you have another file `importer.py` with: - -```Python -from myapp import Hero - -# Some more code -``` - -...in that case, the automatic variable inside of `myapp.py` will not have the variable `__name__` with a value of `"__main__"`. - -So, the line: - -```Python hl_lines="2" -if __name__ == "__main__": - create_db_and_tables() -``` - -...will **not** be executed. - -/// info - -For more information, check the official Python docs. - -/// - -## Last Review - -After those changes, you could run it again, and it would generate the same output as before. - -But now we can import things from this module in other files. - -Now, let's give the code a final look: - -//// tab | Python 3.10+ - -```{.python .annotate} -{!./docs_src/tutorial/create_db_and_table/tutorial003_py310.py!} -``` - -{!./docs_src/tutorial/create_db_and_table/annotations/en/tutorial003.md!} - -//// - -//// tab | Python 3.8+ - -```{.python .annotate} -{!./docs_src/tutorial/create_db_and_table/tutorial003.py!} -``` - -{!./docs_src/tutorial/create_db_and_table/annotations/en/tutorial003.md!} - -//// - -/// tip - -Review what each line does by clicking each number bubble in the code. πŸ‘† - -/// - -## Recap - -We learnt how to use **SQLModel** to define how a table in the database should look like, and we created a database and a table using **SQLModel**. - -We also refactored the code to make it easier to reuse, share, and test later. - -In the next chapters we will see how **SQLModel** will help us interact with SQL databases from code. πŸ€“ diff --git a/docs/tutorial/delete.md b/docs/tutorial/delete.md deleted file mode 100644 index b0eaf6788b..0000000000 --- a/docs/tutorial/delete.md +++ /dev/null @@ -1,248 +0,0 @@ -# Delete Data - DELETE - -Now let's delete some data using **SQLModel**. - -## Continue From Previous Code - -As before, we'll continue from where we left off with the previous code. - -{* ./docs_src/tutorial/update/tutorial003_py310.py ln[0] *} - -Remember to remove the `database.db` file before running the examples to get the same results. - -## Delete with SQL - -This `Spider-Youngster` is getting too weird, so let's just delete it. - -But don't worry, we'll reboot it later with a new story. πŸ˜… - -Let's see how to delete it with **SQL**: - -```SQL hl_lines="1" -DELETE -FROM hero -WHERE name = "Spider-Youngster" -``` - -This means, more or less: - -> Hey SQL database πŸ‘‹, I want to `DELETE` rows `FROM` the table called `hero`. -> -> Please delete all the rows `WHERE` the value of the column `name` is equal to `"Spider-Youngster"`. - -Remember that when using a `SELECT` statement it has the form: - -```SQL -SELECT [some stuff here] -FROM [name of a table here] -WHERE [some condition here] -``` - -`DELETE` is very similar, and again we use `FROM` to tell the table to work on, and we use `WHERE` to tell the condition to use to match the rows that we want to delete. - -You can try that in **DB Browser for SQLite**: - - - -Have in mind that `DELETE` is to delete entire **rows**, not single values in a row. - -If you want to "delete" a single value in a column while **keeping the row**, you would instead **update** the row as explained in the previous chapter, setting the specific value of the column in that row to `NULL` (to `None` in Python). - -Now let's delete with **SQLModel**. - -To get the same results, delete the `database.db` file before running the examples. - -## Read From the Database - -We'll start by selecting the hero `"Spider-Youngster"` that we updated in the previous chapter, this is the one we will delete: - -{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[70:75] hl[72] *} - -As this is a new function `delete_heroes()`, we'll also add it to the `main()` function so that we call it when executing the program from the command line: - -{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[90:98] hl[94] *} - -That will print the same existing hero **Spider-Youngster**: - -
- -```console -$ python app.py - -// Some boilerplate and previous output omitted πŸ˜‰ - -// The SELECT with WHERE -INFO Engine BEGIN (implicit) -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.name = ? -INFO Engine [no key 0.00011s] ('Spider-Youngster',) - -// Print the hero as obtained from the database -Hero: name='Spider-Youngster' secret_name='Pedro Parqueador' age=16 id=2 -``` - -
- -## Delete the Hero from the Session - -Now, very similar to how we used `session.add()` to add or update new heroes, we can use `session.delete()` to delete the hero from the session: - -{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[70:77] hl[77] *} - -## Commit the Session - -To save the current changes in the session, **commit** it. - -This will save all the changes stored in the **session**, like the deleted hero: - -{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[70:78] hl[78] *} - -The same as we have seen before, `.commit()` will also save anything else that was added to the session. Including updates, or created heroes. - -This commit after deleting the hero will generate this output: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// Previous output omitted πŸ™ˆ - -// The SQL to update the hero in the database -INFO Engine DELETE FROM hero WHERE hero.id = ? -INFO Engine [generated in 0.00020s] (2,) -INFO Engine COMMIT -``` - -
- -## Print the Deleted Object - -Now the hero is deleted from the database. - -If we tried to use `session.refresh()` with it, it would raise an exception, because there's no data in the database for this hero. - -Nevertheless, the object is still available with its data, but now it's not connected to the session and it no longer exists in the database. - -As the object is not connected to the session, it is not marked as "expired", the session doesn't even care much about this object anymore. - -Because of that, the object still contains its attributes with the data in it, so we can print it: - -{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[70:80] hl[80] *} - -This will output: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// Previous output omitted πŸ™ˆ - -// Print the deleted hero -Deleted hero: name='Spider-Youngster' secret_name='Pedro Parqueador' age=16 id=2 -``` - -
- -## Query the Database for the Same Row - -To confirm if it was deleted, now let's query the database again, with the same `"Spider-Youngster"` name: - -{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[70:84] hl[82:84] *} - -Here we are using `results.first()` to get the first object found (in case it found multiple) or `None`, if it didn't find anything. - -If we used `results.one()` instead, it would raise an exception, because it expects exactly one result. - -And because we just deleted that hero, this should not find anything and we should get `None`. - -This will execute some SQL in the database and output: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// Previous output omitted πŸ™ˆ - -// Automatically start a new transaction -INFO Engine BEGIN (implicit) - -// SQL to search for the hero -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.name = ? -INFO Engine [no key 0.00013s] ('Spider-Youngster',) -``` - -
- -## Confirm the Deletion - -Now let's just confirm that, indeed, no hero was found in the database with that name. - -We'll do it by checking that the "first" item in the `results` is `None`: - -{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[70:87] hl[86:87] *} - -This will output: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// Previous output omitted πŸ™ˆ - -// Indeed, the hero was deleted πŸ”₯ -There's no hero named Spider-Youngster - -// Cleanup after the with block -INFO Engine ROLLBACK -``` - -
- -## Review the Code - -Now let's review all that code: - -//// tab | Python 3.10+ - -```{ .python .annotate hl_lines="70-88" } -{!./docs_src/tutorial/delete/tutorial002_py310.py!} -``` - -{!./docs_src/tutorial/delete/annotations/en/tutorial002.md!} - -//// - -//// tab | Python 3.8+ - -```{ .python .annotate hl_lines="72-90" } -{!./docs_src/tutorial/delete/tutorial002.py!} -``` - -{!./docs_src/tutorial/delete/annotations/en/tutorial002.md!} - -//// - -/// tip - -Check out the number bubbles to see what is done by each line of code. - -/// - -## Recap - -To delete rows with **SQLModel** you just have to `.delete()` them with the **session**, and then, as always, `.commit()` the session to save the changes to the database. πŸ”₯ diff --git a/docs/tutorial/fastapi/delete.md b/docs/tutorial/fastapi/delete.md deleted file mode 100644 index c7339ef384..0000000000 --- a/docs/tutorial/fastapi/delete.md +++ /dev/null @@ -1,29 +0,0 @@ -# Delete Data with FastAPI - -Let's now add a *path operation* to delete a hero. - -This is quite straightforward. 😁 - -## Delete Path Operation - -Because we want to **delete** data, we use an HTTP `DELETE` operation. - -We get a `hero_id` from the path parameter and verify if it exists, just as we did when reading a single hero or when updating it, **possibly raising an error** with a `404` response. - -And if we actually find a hero, we just delete it with the **session**. - -{* ./docs_src/tutorial/fastapi/delete/tutorial001_py310.py ln[89:97] hl[89:97] *} - -After deleting it successfully, we just return a response of: - -```JSON -{ - "ok": true -} -``` - -## Recap - -That's it, feel free to try it out in the interactive docs UI to delete some heroes. πŸ’₯ - -Using **FastAPI** to read data and combining it with **SQLModel** makes it quite straightforward to delete data from the database. diff --git a/docs/tutorial/fastapi/index.md b/docs/tutorial/fastapi/index.md deleted file mode 100644 index 0d57ab9bd8..0000000000 --- a/docs/tutorial/fastapi/index.md +++ /dev/null @@ -1,17 +0,0 @@ -# FastAPI and Pydantic - Intro - -One of the use cases where **SQLModel** shines the most, and the main one why it was built, was to be combined with **FastAPI**. ✨ - -FastAPI is a Python web framework for building web APIs created by the same author of SQLModel. FastAPI is also built on top of **Pydantic**. - -In this group of chapters we will see how to combine SQLModel **table models** representing tables in the SQL database as all the ones we have seen up to now, with **data models** that only represent data (which are actually just Pydantic models behind the scenes). - -Being able to combine SQLModel **table** models with pure **data** models would be useful on its own, but to make all the examples more concrete, we will use them with **FastAPI**. - -By the end we will have a **simple** but **complete** web **API** to interact with the data in the database. πŸŽ‰ - -## Learning FastAPI - -If you have never used FastAPI, maybe a good idea would be to go and study it a bit before continuing. - -Just reading and trying the examples on the FastAPI main page should be enough, and it shouldn't take you more than **10 minutes**. diff --git a/docs/tutorial/fastapi/limit-and-offset.md b/docs/tutorial/fastapi/limit-and-offset.md deleted file mode 100644 index 1a464a664f..0000000000 --- a/docs/tutorial/fastapi/limit-and-offset.md +++ /dev/null @@ -1,53 +0,0 @@ -# Read Heroes with Limit and Offset with FastAPI - -When a client sends a request to get all the heroes, we have been returning them all. - -But if we had **thousands** of heroes that could consume a lot of **computational resources**, network bandwidth, etc. - -So, we probably want to limit it. - -Let's use the same **offset** and **limit** we learned about in the previous tutorial chapters for the API. - -/// info - -In many cases, this is also called **pagination**. - -/// - -## Add a Limit and Offset to the Query Parameters - -Let's add `limit` and `offset` to the query parameters. - -By default, we will return the first results from the database, so `offset` will have a default value of `0`. - -And by default, we will return a maximum of `100` heroes, so `limit` will have a default value of `100`. - -{* ./docs_src/tutorial/fastapi/limit_and_offset/tutorial001_py310.py ln[1:2,52:56] hl[1,53,55] *} - -We want to allow clients to set different `offset` and `limit` values. - -But we don't want them to be able to set a `limit` of something like `9999`, that's over `9000`! 😱 - -So, to prevent it, we add additional validation to the `limit` query parameter, declaring that it has to be **l**ess than or **e**qual to `100` with `le=100`. - -This way, a client can decide to take fewer heroes if they want, but not more. - -/// info - -If you need to refresh how query parameters and their validation work, check out the docs in FastAPI: - -* Query Parameters -* Query Parameters and String Validations -* Path Parameters and Numeric Validations - -/// - -## Check the Docs UI - -Now we can see that the docs UI shows the new parameters to control **limit** and **offset** of our data. - -Interactive API docs UI - -## Recap - -You can use **FastAPI**'s automatic data validation to get the parameters for `limit` and `offset`, and then use them with the **session** to control ranges of data to be sent in responses. diff --git a/docs/tutorial/fastapi/multiple-models.md b/docs/tutorial/fastapi/multiple-models.md deleted file mode 100644 index 1bc045612b..0000000000 --- a/docs/tutorial/fastapi/multiple-models.md +++ /dev/null @@ -1,351 +0,0 @@ -# Multiple Models with FastAPI - -We have been using the same `Hero` model to declare the schema of the data we receive in the API, the table model in the database, and the schema of the data we send back in responses. - -But in most of the cases, there are slight differences. Let's use multiple models to solve it. - -Here you will see the main and biggest feature of **SQLModel**. 😎 - -## Review Creation Schema - -Let's start by reviewing the automatically generated schemas from the docs UI. - -For input, we have: - -Interactive API docs UI - -If we pay attention, it shows that the client *could* send an `id` in the JSON body of the request. - -This means that the client could try to use the same ID that already exists in the database to create another hero. - -That's not what we want. - -We want the client only to send the data that is needed to create a new hero: - -* `name` -* `secret_name` -* Optional `age` - -And we want the `id` to be generated automatically by the database, so we don't want the client to send it. - -We'll see how to fix it in a bit. - -## Review Response Schema - -Now let's review the schema of the response we send back to the client in the docs UI. - -If you click the small tab Schema instead of the Example Value, you will see something like this: - -Interactive API docs UI - -Let's see the details. - -The fields with a red asterisk (*) are "required". - -This means that our API application is required to return those fields in the response: - -* `name` -* `secret_name` - -The `age` is optional, we don't have to return it, or it could be `None` (or `null` in JSON), but the `name` and the `secret_name` are required. - -Here's the weird thing, the `id` currently seems also "optional". πŸ€” - -This is because in our **SQLModel** class we declare the `id` with a default value of `= None`, because it could be `None` in memory until we save it in the database and we finally get the actual ID. - -But in the responses, we always send a model from the database, so it **always has an ID**. So the `id` in the responses can be declared as required. - -This means that our application is making the promise to the clients that if it sends a hero, it will for sure have an `id` with a value, it will not be `None`. - -### Why Is it Important to Have a Contract for Responses - -The ultimate goal of an API is for some **clients to use it**. - -The clients could be a frontend application, a command line program, a graphical user interface, a mobile application, another backend application, etc. - -And the code those clients write depends on what our API tells them they **need to send**, and what they can **expect to receive**. - -Making both sides very clear will make it much easier to interact with the API. - -And in most of the cases, the developer of the client for that API **will also be yourself**, so you are **doing your future self a favor** by declaring those schemas for requests and responses. πŸ˜‰ - -### So Why is it Important to Have Required IDs - -Now, what's the matter with having one **`id` field marked as "optional"** in a response when in reality it is always available (required)? - -For example, **automatically generated clients** in other languages (or also in Python) would have some declaration that this field `id` is optional. - -And then the developers using those clients in their languages would have to be checking all the time in all their code if the `id` is not `None` before using it anywhere. - -That's a lot of unnecessary checks and **unnecessary code** that could have been saved by declaring the schema properly. πŸ˜” - -It would be a lot simpler for that code to know that the `id` from a response is required and **will always have a value**. - -Let's fix that too. πŸ€“ - -## Multiple Hero Schemas - -So, we want to have our `Hero` model that declares the **data in the database**: - -* `id`, optional on creation, required on database -* `name`, required -* `secret_name`, required -* `age`, optional - -But we also want to have a `HeroCreate` for the data we want to receive when **creating** a new hero, which is almost all the same data as `Hero`, except for the `id`, because that is created automatically by the database: - -* `name`, required -* `secret_name`, required -* `age`, optional - -And we want to have a `HeroPublic` with the `id` field, but this time with a type of `id: int`, instead of `id: int | None`, to make it clear that it will always have an `int` in responses **read** from the clients: - -* `id`, required -* `name`, required -* `secret_name`, required -* `age`, optional - -## Multiple Models with Duplicated Fields - -The simplest way to solve it could be to create **multiple models**, each one with all the corresponding fields: - -{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[5:22] hl[5:9,12:15,18:22] *} - -Here's the important detail, and probably the most important feature of **SQLModel**: only `Hero` is declared with `table = True`. - -This means that the class `Hero` represents a **table** in the database. It is both a **Pydantic** model and a **SQLAlchemy** model. - -But `HeroCreate` and `HeroPublic` don't have `table = True`. They are only **data models**, they are only **Pydantic** models. They won't be used with the database, but only to declare data schemas for the API (or for other uses). - -This also means that `SQLModel.metadata.create_all()` won't create tables in the database for `HeroCreate` and `HeroPublic`, because they don't have `table = True`, which is exactly what we want. πŸš€ - -/// tip - -We will improve this code to avoid duplicating the fields, but for now we can continue learning with these models. - -/// - -## Use Multiple Models to Create a Hero - -Let's now see how to use these new models in the FastAPI application. - -Let's first check how is the process to create a hero now: - -{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[44:51] hl[44:45,47] *} - -Let's check that in detail. - -Now we use the type annotation `HeroCreate` for the request JSON data in the `hero` parameter of the **path operation function**. - -{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[45] hl[45] *} - -Then we create a new `Hero` (this is the actual **table** model that saves things to the database) using `Hero.model_validate()`. - -The method `.model_validate()` reads data from another object with attributes (or a dict) and creates a new instance of this class, in this case `Hero`. - -In this case, we have a `HeroCreate` instance in the `hero` variable. This is an object with attributes, so we use `.model_validate()` to read those attributes. - -/// tip -In versions of **SQLModel** before `0.0.14` you would use the method `.from_orm()`, but it is now deprecated and you should use `.model_validate()` instead. -/// - -We can now create a new `Hero` instance (the one for the database) and put it in the variable `db_hero` from the data in the `hero` variable that is the `HeroCreate` instance we received from the request. - -{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[47] hl[47] *} - -Then we just `add` it to the **session**, `commit`, and `refresh` it, and finally, we return the same `db_hero` variable that has the just refreshed `Hero` instance. - -Because it is just refreshed, it has the `id` field set with a new ID taken from the database. - -And now that we return it, FastAPI will validate the data with the `response_model`, which is a `HeroPublic`: - -{* ./docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py ln[44] hl[44] *} - -This will validate that all the data that we promised is there and will remove any data we didn't declare. - -/// tip - -This filtering could be very important and could be a very good security feature, for example, to make sure you filter private data, hashed passwords, etc. - -You can read more about it in the FastAPI docs about Response Model. - -/// - -In particular, it will make sure that the `id` is there and that it is indeed an integer (and not `None`). - -## Shared Fields - -But looking closely, we could see that these models have a lot of **duplicated information**. - -All **the 3 models** declare that they share some **common fields** that look exactly the same: - -* `name`, required -* `secret_name`, required -* `age`, optional - -And then they declare other fields with some differences (in this case, only about the `id`). - -We want to **avoid duplicated information** if possible. - -This is important if, for example, in the future, we decide to **refactor the code** and rename one field (column). For example, from `secret_name` to `secret_identity`. - -If we have that duplicated in multiple models, we could easily forget to update one of them. But if we **avoid duplication**, there's only one place that would need updating. ✨ - -Let's now improve that. πŸ€“ - -## Multiple Models with Inheritance - -And here it is, you found the biggest feature of **SQLModel**. πŸ’Ž - -Each of these models is only a **data model** or both a data model and a **table model**. - -So, it's possible to create models with **SQLModel** that don't represent tables in the database. - -On top of that, we can use inheritance to avoid duplicated information in these models. - -We can see from above that they all share some **base** fields: - -* `name`, required -* `secret_name`, required -* `age`, optional - -So let's create a **base** model `HeroBase` that the others can inherit from: - -{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:8] hl[5:8] *} - -As you can see, this is *not* a **table model**, it doesn't have the `table = True` config. - -But now we can create the **other models inheriting from it**, they will all share these fields, just as if they had them declared. - -### The `Hero` **Table Model** - -Let's start with the only **table model**, the `Hero`: - -{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:12] hl[11:12] *} - -Notice that `Hero` now doesn't inherit from `SQLModel`, but from `HeroBase`. - -And now we only declare one single field directly, the `id`, that here is `int | None`, and is a `primary_key`. - -And even though we don't declare the other fields **explicitly**, because they are inherited, they are also part of this `Hero` model. - -And of course, all these fields will be in the columns for the resulting `hero` table in the database. - -And those inherited fields will also be in the **autocompletion** and **inline errors** in editors, etc. - -### Columns and Inheritance with Multiple Models - -Notice that the parent model `HeroBase` is not a **table model**, but still, we can declare `name` and `age` using `Field(index=True)`. - -{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:12] hl[6,8,11] *} - -This won't affect this parent **data model** `HeroBase`. - -But once the child model `Hero` (the actual **table model**) inherits those fields, it will use those field configurations to create the indexes when creating the tables in the database. - -### The `HeroCreate` **Data Model** - -Now let's see the `HeroCreate` model that will be used to define the data that we want to receive in the API when creating a new hero. - -This is a fun one: - -{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:16] hl[15:16] *} - -What's happening here? - -The fields we need to create are **exactly the same** as the ones in the `HeroBase` model. So we don't have to add anything. - -And because we can't leave the empty space when creating a new class, but we don't want to add any field, we just use `pass`. - -This means that there's nothing else special in this class apart from the fact that it is named `HeroCreate` and that it inherits from `HeroBase`. - -As an alternative, we could use `HeroBase` directly in the API code instead of `HeroCreate`, but it would show up in the automatic docs UI with that name "`HeroBase`" which could be **confusing** for clients. Instead, "`HeroCreate`" is a bit more explicit about what it is for. - -On top of that, we could easily decide in the future that we want to receive **more data** when creating a new hero apart from the data in `HeroBase` (for example, a password), and now we already have the class to put those extra fields. - -### The `HeroPublic` **Data Model** - -Now let's check the `HeroPublic` model. - -This one just declares that the `id` field is required when reading a hero from the API, because a hero read from the API will come from the database, and in the database it will always have an ID. - -{* ./docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py ln[5:20] hl[19:20] *} - -## Review the Updated Docs UI - -The FastAPI code is still the same as above, we still use `Hero`, `HeroCreate`, and `HeroPublic`. But now, we define them in a smarter way with inheritance. - -So, we can jump to the docs UI right away and see how they look with the updated data. - -### Docs UI to Create a Hero - -Let's see the new UI for creating a hero: - -Interactive API docs UI - -Nice! It now shows that to create a hero, we just pass the `name`, `secret_name`, and optionally `age`. - -We no longer pass an `id`. - -### Docs UI with Hero Responses - -Now we can scroll down a bit to see the response schema: - -Interactive API docs UI - -We can now see that `id` is a required field, it has a red asterisk (*). - -And if we check the schema for the **Read Heroes** *path operation* it will also show the updated schema. - -## Inheritance and Table Models - -We just saw how powerful the inheritance of these models could be. - -This is a very simple example, and it might look a bit... meh. πŸ˜… - -But now imagine that your table has **10 or 20 columns**. And that you have to duplicate all that information for all your **data models**... then it becomes more obvious why it's quite useful to be able to avoid all that information duplication with inheritance. - -Now, this probably looks so flexible that it's not obvious **when to use inheritance** and for what. - -Here are a couple of rules of thumb that can help you. - -### Only Inherit from Data Models - -Only inherit from **data models**, don't inherit from **table models**. - -It will help you avoid confusion, and there won't be any reason for you to need to inherit from a **table model**. - -If you feel like you need to inherit from a **table model**, then instead create a **base** class that is only a **data model** and has all those fields, like `HeroBase`. - -And then inherit from that **base** class that is only a **data model** for any other **data model** and for the **table model**. - -### Avoid Duplication - Keep it Simple - -It could feel like you need to have a profound reason why to inherit from one model or another, because "in some mystical way" they separate different concepts... or something like that. - -In some cases, there are **simple separations** that you can use, like the models to create data, read, update, etc. If that's quick and obvious, nice, use it. πŸ’― - -Otherwise, don't worry too much about profound conceptual reasons to separate models, just try to **avoid duplication** and **keep the code simple** enough to reason about it. - -If you see you have a lot of **overlap** between two models, then you can probably **avoid some of that duplication** with a base model. - -But if to avoid some duplication you end up with a crazy tree of models with inheritance, then it might be **simpler** to just duplicate some of those fields, and that might be easier to reason about and to maintain. - -Do whatever is easier to **reason** about, to **program** with, to **maintain**, and to **refactor** in the future. πŸ€“ - -Remember that inheritance, the same as **SQLModel**, and anything else, are just tools to **help you be more productive**, that's one of their main objectives. If something is not helping with that (e.g. too much duplication, too much complexity), then change it. πŸš€ - -## Recap - -You can use **SQLModel** to declare multiple models: - -* Some models can be only **data models**. They will also be **Pydantic** models. -* And some can *also* be **table models** (apart from already being **data models**) by having the config `table = True`. They will also be **Pydantic** models and **SQLAlchemy** models. - -Only the **table models** will create tables in the database. - -So, you can use all the other **data models** to validate, convert, filter, and document the schema of the data for your application. ✨ - -You can use inheritance to **avoid information and code duplication**. 😎 - -And you can use all these models directly with **FastAPI**. πŸš€ diff --git a/docs/tutorial/fastapi/read-one.md b/docs/tutorial/fastapi/read-one.md deleted file mode 100644 index 03a65a0a2d..0000000000 --- a/docs/tutorial/fastapi/read-one.md +++ /dev/null @@ -1,55 +0,0 @@ -# Read One Model with FastAPI - -Let's now add a *path operation* to read a single model to our **FastAPI** application. - -## Path Operation for One Hero - -Let's add a new *path operation* to read one single hero. - -We want to get the hero based on the `id`, so we will use a **path parameter** `hero_id`. - -/// info - -If you need to refresh how *path parameters* work, including their data validation, check the FastAPI docs about Path Parameters. - -/// - -{* ./docs_src/tutorial/fastapi/read_one/tutorial001_py310.py ln[1:2,59:65] hl[59] *} - -For example, to get the hero with ID `2` we would send a `GET` request to: - -``` -/heroes/2 -``` - -## Handling Errors - -Then, because FastAPI already takes care of making sure that the `hero_id` is an actual integer, we can use it directly with `Hero.get()` to try and get one hero by that ID. - -But if the integer is not the ID of any hero in the database, it will not find anything, and the variable `hero` will be `None`. - -So, we check it in an `if` block, if it's `None`, we raise an `HTTPException` with a `404` status code. - -And to use it, we first import `HTTPException` from `fastapi`. - -This will let the client know that they probably made a mistake on their side and requested a hero that doesn't exist in the database. - -{* ./docs_src/tutorial/fastapi/read_one/tutorial001_py310.py ln[1:2,59:65] hl[1,62:64] *} - -## Return the Hero - -Then, if the hero exists, we return it. - -And because we are using the `response_model` with `HeroPublic`, it will be validated, documented, etc. - -{* ./docs_src/tutorial/fastapi/read_one/tutorial001_py310.py ln[1:2,59:65] hl[59,65] *} - -## Check the Docs UI - -We can then go to the docs UI and see the new *path operation*. - -Interactive API docs UI - -## Recap - -You can combine **FastAPI** features like automatic path parameter validation to get models by ID. diff --git a/docs/tutorial/fastapi/relationships.md b/docs/tutorial/fastapi/relationships.md deleted file mode 100644 index f789fd930a..0000000000 --- a/docs/tutorial/fastapi/relationships.md +++ /dev/null @@ -1,257 +0,0 @@ -# Models with Relationships in FastAPI - -If we go right now and read a single **hero** by ID, we get the hero data with the team ID. - -But we don't get any data about the particular team: - -Interactive API docs UI getting a single hero - -We get a response of: - -```JSON hl_lines="5" -{ - "name": "Deadpond", - "secret_name": "Dive Wilson", - "age": null, - "team_id": 1, - "id": 1, -} -``` - -And the same way, if we get a **team** by ID, we get the team data, but we don't get any information about this team's heroes: - -Interactive API docs UI getting a single team - -Here we get a response of: - -```JSON -{ - "name": "Preventers", - "headquarters": "Sharp Tower", - "id": 2 -} -``` - -...but no information about the heroes. - -Let's update that. πŸ€“ - -## Why Aren't We Getting More Data - -First, why is it that we are not getting the related data for each hero and for each team? - -It's because we declared the `HeroPublic` with only the same base fields of the `HeroBase` plus the `id`. But it doesn't include a field `team` for the **relationship attribute**. - -And the same way, we declared the `TeamPublic` with only the same base fields of the `TeamBase` plus the `id`. But it doesn't include a field `heroes` for the **relationship attribute**. - -{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[5:7,20:21,29:34,43:44] hl[5:7,20:21,29:34,43:44] *} - -Now, remember that FastAPI uses the `response_model` to validate and **filter** the response data? - -In this case, we used `response_model=TeamPublic` and `response_model=HeroPublic`, so FastAPI will use them to filter the response data, even if we return a **table model** that includes **relationship attributes**: - -{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[102:107,155:160] hl[102,107,155,160] *} - -## Don't Include All the Data - -Now let's stop for a second and think about it. - -We cannot simply include *all* the data, including all the internal relationships, because each **hero** has an attribute `team` with their team, and then that **team** also has an attribute `heroes` with all the **heroes** in the team, including this one. - -If we tried to include everything, we could make the server application **crash** trying to extract **infinite data**, going through the same hero and team over and over again internally, something like this: - -```JSON hl_lines="2 13 24 34" -{ - "name": "Rusty-Man", - "secret_name": "Tommy Sharp", - "age": 48, - "team_id": 1, - "id": 1, - "team": { - "name": "Preventers", - "headquarters": "Sharp Tower", - "id": 2, - "heroes": [ - { - "name": "Rusty-Man", - "secret_name": "Tommy Sharp", - "age": 48, - "team_id": 1, - "id": 1, - "team": { - "name": "Preventers", - "headquarters": "Sharp Tower", - "id": 2, - "heroes": [ - { - "name": "Rusty-Man", - "secret_name": "Tommy Sharp", - "age": 48, - "team_id": 1, - "id": 1, - "team": { - "name": "Preventers", - "headquarters": "Sharp Tower", - "id": 2, - "heroes": [ - ...with infinite data here... 😱 - ] - } - } - ] - } - } - ] - } -} -``` - -As you can see, in this example, we would get the hero **Rusty-Man**, and from this hero we would get the team **Preventers**, and then from this team we would get its heroes, of course, including **Rusty-Man**... 😱 - -So we start again, and in the end, the server would just crash trying to get all the data with a `"Maximum recursion error"`, we would not even get a response like the one above. - -So, we need to carefully choose in which cases we want to include data and in which not. - -## What Data to Include - -This is a decision that will depend on **each application**. - -In our case, let's say that if we get a **list of heroes**, we don't want to also include each of their teams in each one. - -And if we get a **list of teams**, we don't want to get a list of the heroes for each one. - -But if we get a **single hero**, we want to include the team data (without the team's heroes). - -And if we get a **single team**, we want to include the list of heroes (without each hero's team). - -Let's add a couple more **data models** that declare that data so we can use them in those two specific *path operations*. - -## Models with Relationships - -Let's add the models `HeroPublicWithTeam` and `TeamPublicWithHeroes`. - -We'll add them **after** the other models so that we can easily reference the previous models. - -{* ./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py ln[59:64] hl[59:60,63:64] *} - -These two models are very **simple in code**, but there's a lot happening here. Let's check it out. - -### Inheritance and Type Annotations - -The `HeroPublicWithTeam` **inherits** from `HeroPublic`, which means that it will have the **normal fields for reading**, including the required `id` that was declared in `HeroPublic`. - -And then it adds the **new field** `team`, which could be `None`, and is declared with the type `TeamPublic` with the base fields for reading a team. - -Then we do the same for the `TeamPublicWithHeroes`, it **inherits** from `TeamPublic`, and declares the **new field** `heroes`, which is a list of `HeroPublic`. - -### Data Models Without Relationship Attributes - -Now, notice that these new fields `team` and `heroes` are not declared with `Relationship()`, because these are not **table models**, they cannot have **relationship attributes** with the magic access to get that data from the database. - -Instead, here these are only **data models** that will tell FastAPI **which attributes** to get data from and **which data** to get from them. - -### Reference to Other Models - -Also, notice that the field `team` is not declared with this new `TeamPublicWithHeroes`, because that would again create that infinite recursion of data. Instead, we declare it with the normal `TeamPublic` model. - -And the same for `TeamPublicWithHeroes`, the model used for the new field `heroes` uses `HeroPublic` to get only each hero's data. - -This also means that, even though we have these two new models, **we still need the previous ones**, `HeroPublic` and `TeamPublic`, because we need to reference them here (and we are also using them in the rest of the *path operations*). - -## Update the Path Operations - -Now we can update the *path operations* to use the new models. - -This will tell **FastAPI** to take the object that we return from the *path operation function* (a **table model**) and **access the additional attributes** from them to extract their data. - -In the case of the hero, this tells FastAPI to extract the `team` too. And in the case of the team, to extract the list of `heroes` too. - -{* ./docs_src/tutorial/fastapi/relationships/tutorial001_py310.py ln[111:116,164:169] hl[111,116,164,169] *} - -## Check It Out in the Docs UI - -Now let's try it out again in the **docs UI**. - -Let's try again with the same **hero** with ID `1`: - -Interactive API docs UI getting a single hero with team - -Now we get the **team** data included: - -```JSON hl_lines="7-11" -{ - "name": "Deadpond", - "secret_name": "Dive Wilson", - "age": null, - "team_id": 1, - "id": 1, - "team": { - "name": "Z-Force", - "headquarters": "Sister Margaret's Bar", - "id": 1 - } -} -``` - -And if we get now the **team** with ID `2`: - -Interactive API docs UI getting a single team with the list of heroes - -Now we get the list of **heroes** included: - -```JSON hl_lines="5-41" -{ - "name": "Preventers", - "headquarters": "Sharp Tower", - "id": 2, - "heroes": [ - { - "name": "Rusty-Man", - "secret_name": "Tommy Sharp", - "age": 48, - "team_id": 2, - "id": 2 - }, - { - "name": "Spider-Boy", - "secret_name": "Pedro Parqueador", - "age": null, - "team_id": 2, - "id": 3 - }, - { - "name": "Tarantula", - "secret_name": "Natalia Roman-on", - "age": 32, - "team_id": 2, - "id": 6 - }, - { - "name": "Dr. Weird", - "secret_name": "Steve Weird", - "age": 36, - "team_id": 2, - "id": 7 - }, - { - "name": "Captain North America", - "secret_name": "Esteban Rogelios", - "age": 93, - "team_id": 2, - "id": 8 - } - ] -} -``` - -## Recap - -Using the same techniques to declare additional **data models**, we can tell FastAPI what data to return in the responses, even when we return **table models**. - -Here we almost **didn't have to change the FastAPI app** code, but of course, there will be cases where you need to get the data and process it in different ways in the *path operation function* before returning it. - -But even in those cases, you will be able to define the **data models** to use in `response_model` to tell FastAPI how to validate and filter the data. - -By this point, you already have a very robust API to handle data in a SQL database combining **SQLModel** with **FastAPI**, and implementing **best practices**, like data validation, conversion, filtering, and documentation. ✨ - -In the next chapter, I'll tell you how to implement automated **testing** for your application using FastAPI and SQLModel. βœ… diff --git a/docs/tutorial/fastapi/response-model.md b/docs/tutorial/fastapi/response-model.md deleted file mode 100644 index f9214332c6..0000000000 --- a/docs/tutorial/fastapi/response-model.md +++ /dev/null @@ -1,81 +0,0 @@ -# FastAPI Response Model with SQLModel - -Now I'll show you how to use FastAPI's `response_model` with **SQLModel**. - -## Interactive API Docs - -Up to now, with the code we have used, the API docs know the data the clients have to send: - -Interactive API docs UI - -This interactive docs UI is powered by Swagger UI, and what Swagger UI does is to read a big JSON content that defines the API with all the data schemas (data shapes) using the standard OpenAPI, and showing it in that nice UI. - -FastAPI automatically **generates that OpenAPI** for Swagger UI to read it. - -And it generates it **based on the code you write**, using the Pydantic models (in this case **SQLModel** models) and type annotations to know the schemas of the data that the API handles. - -## Response Data - -But up to now, the API docs UI doesn't know the schema of the *responses* our app sends back. - -You can see that there's a possible "Successful Response" with a code `200`, but we have no idea how the response data would look like. - -API docs UI without response data schemas - -Right now, we only tell FastAPI the data we want to receive, but we don't tell it yet the data we want to send back. - -Let's do that now. πŸ€“ - -## Use `response_model` - -We can use `response_model` to tell FastAPI the schema of the data we want to send back. - -For example, we can pass the same `Hero` **SQLModel** class (because it is also a Pydantic model): - -{* ./docs_src/tutorial/fastapi/response_model/tutorial001_py310.py ln[31:37] hl[31] *} - -## List of Heroes in `response_model` - -We can also use other type annotations, the same way we can use with Pydantic fields. For example, we can pass a list of `Hero`s. - -To do so, we declare the `response_model` with `list[Hero]`: - -{* ./docs_src/tutorial/fastapi/response_model/tutorial001_py310.py ln[40:44] hl[40] *} - -## FastAPI and Response Model - -FastAPI will do data validation and filtering of the response with this `response_model`. - -So this works like a contract between our application and the client. - -You can read more about it in the FastAPI docs about `response_model`. - -## New API Docs UI - -Now we can go back to the docs UI and see that they now show the schema of the response we will receive. - -API docs UI without response data schemas - -The clients will know what data they should expect. - -## Automatic Clients - -The most visible advantage of using the `response_model` is that it shows up in the API docs UI. - -But there are other advantages, like that FastAPI will do automatic data validation and filtering of the response data using this model. - -Additionally, because the schemas are defined in using a standard, there are many tools that can take advantage of this. - -For example, client generators, that can automatically create the code necessary to talk to your API in many languages. - -/// info - -If you are curious about the standards, FastAPI generates OpenAPI, that internally uses JSON Schema. - -You can read about all that in the FastAPI docs - First Steps. - -/// - -## Recap - -Use the `response_model` to tell FastAPI the schema of the data you want to send back and have awesome data APIs. 😎 diff --git a/docs/tutorial/fastapi/session-with-dependency.md b/docs/tutorial/fastapi/session-with-dependency.md deleted file mode 100644 index e81e9e6745..0000000000 --- a/docs/tutorial/fastapi/session-with-dependency.md +++ /dev/null @@ -1,89 +0,0 @@ -# Session with FastAPI Dependency - -Before we keep adding things, let's change a bit how we get the session for each request to simplify our life later. - -## Current Sessions - -Up to now, we have been creating a session in each *path operation*, in a `with` block. - -{* ./docs_src/tutorial/fastapi/delete/tutorial001_py310.py ln[48:55] hl[50] *} - -That's perfectly fine, but in many use cases we would want to use FastAPI Dependencies, for example to **verify** that the client is **logged in** and get the **current user** before executing any other code in the *path operation*. - -These dependencies are also very useful during **testing**, because we can **easily replace them**, and then, for example, use a new database for our tests, or put some data before the tests, etc. - -So, let's refactor these sessions to use **FastAPI Dependencies**. - -## Create a **FastAPI** Dependency - -A **FastAPI** dependency is very simple, it's just a function that returns a value. - -It could use `yield` instead of `return`, and in that case **FastAPI** will make sure it executes all the code **after** the `yield`, once it is done with the request. - -{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[40:42] hl[40:42] *} - -## Use the Dependency - -Now let's make FastAPI execute a dependency and get its value in the *path operation*. - -We import `Depends()` from `fastapi`. Then we use it in the *path operation function* in a **parameter**, the same way we declared parameters to get JSON bodies, path parameters, etc. - -{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[1:2,40:42,53:59] hl[1,54] *} - -/// tip - -Here's a tip about that `*,` thing in the parameters. - -Here we are passing the parameter `session` that has a "default value" of `Depends(get_session)` before the parameter `hero`, that doesn't have any default value. - -Python would normally complain about that, but we can use the initial "parameter" `*,` to mark all the rest of the parameters as "keyword only", which solves the problem. - -You can read more about it in the FastAPI documentation Path Parameters and Numeric Validations - Order the parameters as you need, tricks - -/// - -The value of a dependency will **only be used for one request**, FastAPI will call it right before calling your code and will give you the value from that dependency. - -If it had `yield`, then it will continue the rest of the execution once you are done sending the response. In the case of the **session**, it will finish the cleanup code from the `with` block, closing the session, etc. - -Then FastAPI will call it again for the **next request**. - -Because it is called **once per request**, we will still get a **single session per request** as we should, so we are still fine with that. βœ… - -And because dependencies can use `yield`, FastAPI will make sure to run the code **after** the `yield` once it is done, including all the **cleanup code** at the end of the `with` block. So we are also fine with that. βœ… - -## The `with` Block - -This means that in the main code of the *path operation function*, it will work equivalently to the previous version with the explicit `with` block. - -{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[1:2,40:42,53:59] hl[55:59] *} - -In fact, you could think that all that block of code inside of the `create_hero()` function is still inside a `with` block for the **session**, because this is more or less what's happening behind the scenes. - -But now, the `with` block is not explicitly in the function, but in the dependency above: - -{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[1:2,40:42,53:59] hl[41:42] *} - -We will see how this is very useful when testing the code later. βœ… - -## Update the Path Operations to Use the Dependency - -Now we can update the rest of the *path operations* to use the new dependency. - -We just declare the dependency in the parameters of the function, with: - -```Python -session: Session = Depends(get_session) -``` - -And then we remove the previous `with` block with the old **session**. - -{* ./docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py ln[1:2,40:42,53:104] hl[54,65,74,83,98] *} - -## Recap - -You just learned how to use **FastAPI dependencies** to handle the database session. This will come in handy later when testing the code. - -And you will see how much these dependencies can help the more you work with FastAPI, to handle **permissions**, **authentication**, resources like database **sessions**, etc. πŸš€ - -If you want to learn more about dependencies, checkout the FastAPI docs about Dependencies. diff --git a/docs/tutorial/fastapi/simple-hero-api.md b/docs/tutorial/fastapi/simple-hero-api.md deleted file mode 100644 index 79cf075e1b..0000000000 --- a/docs/tutorial/fastapi/simple-hero-api.md +++ /dev/null @@ -1,211 +0,0 @@ -# Simple Hero API with FastAPI - -Let's start by building a simple hero web API with **FastAPI**. ✨ - -## Install **FastAPI** - -The first step is to install FastAPI. - -FastAPI is the framework to create the **web API**. - -Make sure you create a [virtual environment](../../virtual-environments.md){.internal-link target=_blank}, activate it, and then install them, for example with: - -
- -```console -$ pip install fastapi "uvicorn[standard]" - ----> 100% -``` - -
- -## **SQLModel** Code - Models, Engine - -Now let's start with the SQLModel code. - -We will start with the **simplest version**, with just heroes (no teams yet). - -This is almost the same code we have seen up to now in previous examples: - -{* ./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py ln[2,5:20] hl[19:20] *} - -There's only one change here from the code we have used before, the `check_same_thread` in the `connect_args`. - -That is a configuration that SQLAlchemy passes to the low-level library in charge of communicating with the database. - -`check_same_thread` is by default set to `True`, to prevent misuses in some simple cases. - -But here we will make sure we don't share the same **session** in more than one request, and that's the actual **safest way** to prevent any of the problems that configuration is there for. - -And we also need to disable it because in **FastAPI** each request could be handled by multiple interacting threads. - -/// info - -That's enough information for now, you can read more about it in the FastAPI docs for `async` and `await`. - -The main point is, by ensuring you **don't share** the same **session** with more than one request, the code is already safe. - -/// - -## **FastAPI** App - -The next step is to create the **FastAPI** app. - -We will import the `FastAPI` class from `fastapi`. - -And then create an `app` object that is an instance of that `FastAPI` class: - -{* ./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py ln[1:2,23] hl[1,23] *} - -## Create Database and Tables on `startup` - -We want to make sure that once the app starts running, the function `create_db_and_tables` is called. To create the database and tables. - -This should be called only once at startup, not before every request, so we put it in the function to handle the `"startup"` event: - -{* ./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py ln[23:28] hl[26:28] *} - -## Create Heroes *Path Operation* - -/// info - -If you need a refresher on what a **Path Operation** is (an endpoint with a specific HTTP Operation) and how to work with it in FastAPI, check out the FastAPI First Steps docs. - -/// - -Let's create the **path operation** code to create a new hero. - -It will be called when a user sends a request with a `POST` **operation** to the `/heroes/` **path**: - -{* ./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py ln[23:37] hl[31:32] *} - -/// info - -If you need a refresher on some of those concepts, checkout the FastAPI documentation: - -* First Steps -* Path Parameters - Data Validation and Data Conversion -* Request Body - -/// - -## The **SQLModel** Advantage - -Here's where having our **SQLModel** class models be both **SQLAlchemy** models and **Pydantic** models at the same time shine. ✨ - -Here we use the **same** class model to define the **request body** that will be received by our API. - -Because **FastAPI** is based on Pydantic, it will use the same model (the Pydantic part) to do automatic data validation and conversion from the JSON request to an object that is an actual instance of the `Hero` class. - -And then, because this same **SQLModel** object is not only a **Pydantic** model instance but also a **SQLAlchemy** model instance, we can use it directly in a **session** to create the row in the database. - -So we can use intuitive standard Python **type annotations**, and we don't have to duplicate a lot of the code for the database models and the API data models. πŸŽ‰ - -/// tip - -We will improve this further later, but for now, it already shows the power of having **SQLModel** classes be both **SQLAlchemy** models and **Pydantic** models at the same time. - -/// - -## Read Heroes *Path Operation* - -Now let's add another **path operation** to read all the heroes: - -{* ./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py ln[23:44] hl[40:44] *} - -This is pretty straightforward. - -When a client sends a request to the **path** `/heroes/` with a `GET` HTTP **operation**, we run this function that gets the heroes from the database and returns them. - -## One Session per Request - -Remember that we should use a SQLModel **session** per each group of operations and if we need other unrelated operations we should use a different session? - -Here it is much more obvious. - -We should normally have **one session per request** in most of the cases. - -In some isolated cases, we would want to have new sessions inside, so, **more than one session** per request. - -But we would **never want to *share* the same session** among different requests. - -In this simple example, we just create the new sessions manually in the **path operation functions**. - -In future examples later we will use a FastAPI Dependency to get the **session**, being able to share it with other dependencies and being able to replace it during testing. πŸ€“ - -## Run the **FastAPI** Server in Development Mode - -Now we are ready to run the FastAPI application. - -Put all that code in a file called `main.py`. - -Then run it with the `fastapi` CLI, in development mode: - -
- -```console -$ fastapi dev main.py - -INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) -``` - -
- -/// info - -The `fastapi` command uses Uvicorn underneath. - -/// - -When you use `fastapi dev` it starts Uvicorn with the option to reload automatically every time you make a change to the code, this way you will be able to develop faster. πŸ€“ - -## Run the **FastAPI** Server in Production Mode - -The development mode should not be used in production, as it includes automatic reload by default it consumes much more resources than necessary, and it would be more error prone, etc. - -For production, use `fastapi run` instead of `fastapi dev`: - -
- -```console -$ fastapi run main.py - -INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) -``` - -
- -## Check the API docs UI - -Now you can go to that URL in your browser `http://127.0.0.1:8000`. We didn't create a *path operation* for the root path `/`, so that URL alone will only show a "Not Found" error... that "Not Found" error is produced by your FastAPI application. - -But you can go to the **automatically generated interactive API documentation** at the path `/docs`: http://127.0.0.1:8000/docs. ✨ - -You will see that this **automatic API docs UI** has the *paths* that we defined above with their *operations*, and that it already knows the shape of the data that the **path operations** will receive: - -Interactive API docs UI - -## Play with the API - -You can actually click the button Try it out and send some requests to create some heroes with the **Create Hero** *path operation*. - -And then you can get them back with the **Read Heroes** *path operation*: - -Interactive API docs UI reading heroes - -## Check the Database - -Now you can terminate that server program by going back to the terminal and pressing Ctrl+C. - -And then, you can open **DB Browser for SQLite** and check the database, to explore the data and confirm that it indeed saved the heroes. πŸŽ‰ - -DB Browser for SQLite showing the heroes - -## Recap - -Good job! This is already a FastAPI **web API** application to interact with the heroes database. πŸŽ‰ - -There are several things we can improve and extend. For example, we want the database to decide the ID of each new hero, we don't want to allow a user to send it. - -We will make all those improvements in the next chapters. πŸš€ diff --git a/docs/tutorial/fastapi/teams.md b/docs/tutorial/fastapi/teams.md deleted file mode 100644 index 4f07fb1981..0000000000 --- a/docs/tutorial/fastapi/teams.md +++ /dev/null @@ -1,65 +0,0 @@ -# FastAPI Path Operations for Teams - Other Models - -Let's now update the **FastAPI** application to handle data for teams. - -This is very similar to the things we have done for heroes, so we will go over it quickly here. - -We will use the same models we used in previous examples, with the **relationship attributes**, etc. - -## Add Teams Models - -Let's add the models for the teams. - -It's the same process we did for heroes, with a base model, a **table model**, and some other **data models**. - -We have a `TeamBase` **data model**, and from it, we inherit with a `Team` **table model**. - -Then we also inherit from the `TeamBase` for the `TeamCreate` and `TeamPublic` **data models**. - -And we also create a `TeamUpdate` **data model**. - -{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[1:26] hl[5:7,10:13,16:17,20:21,24:26] *} - -We now also have **relationship attributes**. πŸŽ‰ - -Let's now update the `Hero` models too. - -## Update Hero Models - -{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[29:55] hl[29:34,37:40,43:44,47:48,51:55] *} - -We now have a `team_id` in the hero models. - -Notice that we can declare the `team_id` in the `HeroBase` because it can be reused by all the models, in all the cases it's an optional integer. - -And even though the `HeroBase` is *not* a **table model**, we can declare `team_id` in it with the `foreign key` parameter. It won't do anything in most of the models that inherit from `HeroBase`, but in the **table model** `Hero` it will be used to tell **SQLModel** that this is a **foreign key** to that table. - -## Relationship Attributes - -Notice that the **relationship attributes**, the ones with `Relationship()`, are **only** in the **table models**, as those are the ones that are handled by **SQLModel** with SQLAlchemy and that can have the automatic fetching of data from the database when we access them. - -{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[5:55] hl[13,40] *} - -## Path Operations for Teams - -Let's now add the **path operations** for teams. - -These are equivalent and very similar to the **path operations** for the **heroes** we had before, so we don't have to go over the details for each one, let's check the code. - -{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[135:188] hl[135:141,144:152,155:160,163:178,181:188] *} - -## Using Relationships Attributes - -Up to this point, we are actually not using the **relationship attributes**, but we could access them in our code. - -In the next chapter, we will play more with them. - -## Check the Docs UI - -Now we can check the automatic docs UI to see all the **path operations** for heroes and teams. - -Interactive API docs UI - -## Recap - -We can use the same patterns to add more models and API **path operations** to our **FastAPI** application. πŸŽ‰ diff --git a/docs/tutorial/fastapi/tests.md b/docs/tutorial/fastapi/tests.md deleted file mode 100644 index ed4f91bcd6..0000000000 --- a/docs/tutorial/fastapi/tests.md +++ /dev/null @@ -1,384 +0,0 @@ -# Test Applications with FastAPI and SQLModel - -To finish this group of chapters about **FastAPI** with **SQLModel**, let's now learn how to implement automated tests for an application using FastAPI with SQLModel. βœ… - -Including the tips and tricks. 🎁 - -## FastAPI Application - -Let's work with one of the **simpler** FastAPI applications we built in the previous chapters. - -All the same **concepts**, **tips** and **tricks** will apply to more complex applications as well. - -We will use the application with the hero models, but without team models, and we will use the dependency to get a **session**. - -Now we will see how useful it is to have this session dependency. ✨ - -{* ./docs_src/tutorial/fastapi/app_testing/tutorial001/main.py ln[0] *} - -## File Structure - -Now we will have a Python project with multiple files, one file `main.py` with all the application, and one file `test_main.py` with the tests, with the same ideas from [Code Structure and Multiple Files](../code-structure.md){.internal-link target=_blank}. - -The file structure is: - -``` -. -β”œβ”€β”€ project - β”œβ”€β”€ __init__.py - β”œβ”€β”€ main.py - └── test_main.py -``` - -## Testing FastAPI Applications - -If you haven't done testing in FastAPI applications, first check the FastAPI docs about Testing. - -Then, we can continue here, the first step is to install the dependencies, `requests` and `pytest`. - -Make sure you create a [virtual environment](../../virtual-environments.md){.internal-link target=_blank}, activate it, and then install them, for example with: - -
- -```console -$ pip install requests pytest - ----> 100% -``` - -
- -## Basic Tests Code - -Let's start with a simple test, with just the basic test code we need the check that the **FastAPI** application is creating a new hero correctly. - -```{ .python .annotate } -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_001.py[ln:1-7]!} - # Some code here omitted, we will see it later πŸ‘ˆ -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_001.py[ln:20-24]!} - # Some code here omitted, we will see it later πŸ‘ˆ -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_001.py[ln:26-32]!} - -# Code below omitted πŸ‘‡ -``` - -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_001.md!} - -/// tip - -Check out the number bubbles to see what is done by each line of code. - -/// - -That's the **core** of the code we need for all the tests later. - -But now, we need to deal with a bit of logistics and details we are not paying attention to just yet. πŸ€“ - -## Testing Database - -This test looks fine, but there's a problem. - -If we run it, it will use the same **production database** that we are using to store our very important **heroes**, and we will end up adding unnecessary data to it, or even worse, in future tests we could end up removing production data. - -So, we should use an independent **testing database**, just for the tests. - -To do this, we need to change the URL used for the database. - -But when the code for the API is executed, it gets a **session** that is already connected to an **engine**, and the **engine** is already using a specific database URL. - -Even if we import the variable from the `main` module and change its value just for the tests, by that point the **engine** is already created with the original value. - -But all our API *path operations* get the *session* using a FastAPI **dependency**, and we can override dependencies in tests. - -Here's where dependencies start to help a lot. - -## Override a Dependency - -Let's override the `get_session()` dependency for the tests. - -This dependency is used by all the *path operations* to get the **SQLModel** session object. - -We will override it to use a different **session** object just for the tests. - -That way we protect the production database and we have better control of the data we are testing. - -```{ .python .annotate hl_lines="4 9-10 12 19" } -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_002.py[ln:1-7]!} - # Some code here omitted, we will see it later πŸ‘ˆ -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_002.py[ln:15-32]!} - -# Code below omitted πŸ‘‡ -``` - -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_002.md!} - -/// tip - -Check out the number bubbles to see what is done by each line of code. - -/// - -## Create the Engine and Session for Testing - -Now let's create that **session** object that will be used during testing. - -It will use its own **engine**, and this new engine will use a new URL for the testing database: - -``` -sqlite:///testing.db -``` - -So, the testing database will be in the file `testing.db`. - -``` { .python .annotate hl_lines="4 8-11 13 16 33"} -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_003.py!} -``` - -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_003.md!} - -### Import Table Models - -Here we create all the tables in the testing database with: - -```Python -SQLModel.metadata.create_all(engine) -``` - -But remember that [Order Matters](../create-db-and-table.md#sqlmodel-metadata-order-matters){.internal-link target=_blank} and we need to make sure all the **SQLModel** models are already defined and **imported** before calling `.create_all()`. - -In this case, it all works for a little subtlety that deserves some attention. - -Because we import something, *anything*, from `.main`, the code in `.main` will be executed, including the definition of the **table models**, and that will automatically register them in `SQLModel.metadata`. - -That way, when we call `.create_all()` all the **table models** are correctly registered in `SQLModel.metadata` and it will all work. πŸ‘Œ - -## Memory Database - -Now we are not using the production database. Instead, we use a **new testing database** with the `testing.db` file, which is great. - -But SQLite also supports having an **in memory** database. This means that all the database is only in memory, and it is never saved in a file on disk. - -After the program terminates, **the in-memory database is deleted**, so it wouldn't help much for a production database. - -But **it works great for testing**, because it can be quickly created before each test, and quickly removed after each test. βœ… - -And also, because it never has to write anything to a file and it's all just in memory, it will be even faster than normally. 🏎 - -/// details | Other alternatives and ideas πŸ‘€ - -Before arriving at the idea of using an **in-memory database** we could have explored other alternatives and ideas. - -The first is that we are not deleting the file after we finish the test, so the next test could have **leftover data**. So, the right thing would be to delete the file right after finishing the test. πŸ”₯ - -But if each test has to create a new file and then delete it afterwards, running all the tests could be **a bit slow**. - -Right now, we have a file `testing.db` that is used by all the tests (we only have one test now, but we will have more). - -So, if we tried to run the tests at the same time **in parallel** to try to speed things up a bit, they would clash trying to use the *same* `testing.db` file. - -Of course, we could also fix that, using some **random name** for each testing database file... but in the case of SQLite, we have an even better alternative by just using an **in-memory database**. ✨ - -/// - -## Configure the In-Memory Database - -Let's update our code to use the in-memory database. - -We just have to change a couple of parameters in the **engine**. - -```{ .python .annotate hl_lines="3 9-13"} -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_004.py[ln:1-13]!} - -# Code below omitted πŸ‘‡ -``` - -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_004.md!} - -/// tip - -Check out the number bubbles to see what is done by each line of code. - -/// - -That's it, now the test will run using the **in-memory database**, which will be faster and probably safer. - -And all the other tests can do the same. - -## Boilerplate Code - -Great, that works, and you could replicate all that process in each of the test functions. - -But we had to add a lot of **boilerplate code** to handle the custom database, creating it in memory, the custom session, and the dependency override. - -Do we really have to duplicate all that for **each test**? No, we can do better! 😎 - -We are using **pytest** to run the tests. And pytest also has a very similar concept to the **dependencies in FastAPI**. - -/// info - -In fact, pytest was one of the things that inspired the design of the dependencies in FastAPI. - -/// - -It's a way for us to declare some **code that should be run before** each test and **provide a value** for the test function (that's pretty much the same as FastAPI dependencies). - -In fact, it also has the same trick of allowing to use `yield` instead of `return` to provide the value, and then **pytest** makes sure that the code after `yield` is executed *after* the function with the test is done. - -In pytest, these things are called **fixtures** instead of *dependencies*. - -Let's use these **fixtures** to improve our code and reduce de duplicated boilerplate for the next tests. - -## Pytest Fixtures - -You can read more about them in the pytest docs for fixtures, but I'll give you a short example for what we need here. - -Let's see the first code example with a fixture: - -``` { .python .annotate } -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_005.py!} -``` - -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_005.md!} - -/// tip - -Check out the number bubbles to see what is done by each line of code. - -/// - -**pytest** fixtures work in a very similar way to FastAPI dependencies, but have some minor differences: - -* In pytest fixtures, we need to add a decorator of `@pytest.fixture()` on top. -* To use a pytest fixture in a function, we have to declare the parameter with the **exact same name**. In FastAPI we have to **explicitly use `Depends()`** with the actual function inside it. - -But apart from the way we declare them and how we tell the framework that we want to have them in the function, they **work in a very similar way**. - -Now we create lot's of tests and re-use that same fixture in all of them, saving us that **boilerplate code**. - -**pytest** will make sure to run them right before (and finish them right after) each test function. So, each test function will actually have its own database, engine, and session. - -## Client Fixture - -Awesome, that fixture helps us prevent a lot of duplicated code. - -But currently, we still have to write some code in the test function that will be repetitive for other tests, right now we: - -* create the **dependency override** -* put it in the `app.dependency_overrides` -* create the `TestClient` -* Clear the dependency override(s) after making the request - -That's still gonna be repetitive in the other future tests. Can we improve it? Yes! πŸŽ‰ - -Each **pytest** fixture (the same way as **FastAPI** dependencies), can require other fixtures. - -So, we can create a **client fixture** that will be used in all the tests, and it will itself require the **session fixture**. - -``` { .python .annotate hl_lines="19-28 31" } -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_006.py!} -``` - -{!./docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_006.md!} - -/// tip - -Check out the number bubbles to see what is done by each line of code. - -/// - -Now we have a **client fixture** that, in turn, uses the **session fixture**. - -And in the actual test function, we just have to declare that we require this **client fixture**. - -## Add More Tests - -At this point, it all might seem like we just did a lot of changes for nothing, to get **the same result**. πŸ€” - -But normally we will create **lots of other test functions**. And now all the boilerplate and complexity is **written only once**, in those two fixtures. - -Let's add some more tests: - -{* ./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py ln[30:58] hl[30,49] *} - -/// tip - -It's always **good idea** to not only test the normal case, but also that **invalid data**, **errors**, and **corner cases** are handled correctly. - -That's why we add these two extra tests here. - -/// - -Now, any additional test functions can be as **simple** as the first one, they just have to **declare the `client` parameter** to get the `TestClient` **fixture** with all the database stuff setup. Nice! 😎 - -## Why Two Fixtures - -Now, seeing the code, we could think, why do we put **two fixtures** instead of **just one** with all the code? And that makes total sense! - -For these examples, **that would have been simpler**, there's no need to separate that code into two fixtures for them... - -But for the next test function, we will require **both fixtures**, the **client** and the **session**. - -{* ./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py ln[1:6,61:81] hl[6,61] *} - -In this test function, we want to check that the *path operation* to **read a list of heroes** actually sends us heroes. - -But if the **database is empty**, we would get an **empty list**, and we wouldn't know if the hero data is being sent correctly or not. - -But we can **create some heroes** in the testing database right before sending the API request. ✨ - -And because we are using the **testing database**, we don't affect anything by creating heroes for the test. - -To do it, we have to: - -* import the `Hero` model -* require both fixtures, the **client** and the **session** -* create some heroes and save them in the database using the **session** - -After that, we can send the request and check that we actually got the data back correctly from the database. πŸ’― - -Here's the important detail to notice: we can require fixtures in other fixtures **and also** in the test functions. - -The function for the **client fixture** and the actual testing function will **both** receive the same **session**. - -## Add the Rest of the Tests - -Using the same ideas, requiring the fixtures, creating data that we need for the tests, etc., we can now add the rest of the tests. They look quite similar to what we have done up to now. - -{* ./docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py ln[84:125] hl[84,99,114] *} - -## Run the Tests - -Now we can run the tests with `pytest` and see the results: - -
- -```console -$ pytest - -============= test session starts ============== -platform linux -- Python 3.10.0, pytest-7.4.4, pluggy-1.5.0 -rootdir: /home/user/code/sqlmodel-tutorial -collected 7 items - ----> 100% - -project/test_main.py ....... [100%] - -============== 7 passed in 0.83s =============== -``` - -
- -## Recap - -Did you read all that? Wow, I'm impressed! 😎 - -Adding tests to your application will give you a lot of **certainty** that everything is **working correctly**, as you intended. - -And tests will be notoriously useful when **refactoring** your code, **changing things**, **adding features**. Because tests can help catch a lot of errors that can be easily introduced by refactoring. - -And they will give you the confidence to work faster and **more efficiently**, because you know that you are checking if you are **not breaking anything**. πŸ˜… - -I think tests are one of those things that bring your code and you as a developer to the next professional level. 😎 - -And if you read and studied all this, you already know a lot of the advanced ideas and tricks that took me years to learn. πŸš€ diff --git a/docs/tutorial/fastapi/update-extra-data.md b/docs/tutorial/fastapi/update-extra-data.md deleted file mode 100644 index 95843144ae..0000000000 --- a/docs/tutorial/fastapi/update-extra-data.md +++ /dev/null @@ -1,143 +0,0 @@ -# Update with Extra Data (Hashed Passwords) with FastAPI - -In the previous chapter I explained to you how to update data in the database from input data coming from a **FastAPI** *path operation*. - -Now I'll explain to you how to add **extra data**, additional to the input data, when updating or creating a model object. - -This is particularly useful when you need to **generate some data** in your code that is **not coming from the client**, but you need to store it in the database. For example, to store a **hashed password**. - -## Password Hashing - -Let's imagine that each hero in our system also has a **password**. - -We should never store the password in plain text in the database, we should only stored a **hashed version** of it. - -"**Hashing**" means converting some content (a password in this case) into a sequence of bytes (just a string) that looks like gibberish. - -Whenever you pass exactly the same content (exactly the same password) you get exactly the same gibberish. - -But you **cannot convert** from the gibberish **back to the password**. - -### Why use Password Hashing - -If your database is stolen, the thief won't have your users' **plaintext passwords**, only the hashes. - -So, the thief won't be able to try to use that password in another system (as many users use the same password everywhere, this would be dangerous). - -/// tip - -You could use passlib to hash passwords. - -In this example we will use a fake hashing function to focus on the data changes. 🀑 - -/// - -## Update Models with Extra Data - -The `Hero` table model will now store a new field `hashed_password`. - -And the data models for `HeroCreate` and `HeroUpdate` will also have a new field `password` that will contain the plain text password sent by clients. - -{* ./docs_src/tutorial/fastapi/update/tutorial002_py310.py ln[5:28] hl[13,17,28] *} - -When a client is creating a new hero, they will send the `password` in the request body. - -And when they are updating a hero, they could also send the `password` in the request body to update it. - -## Hash the Password - -The app will receive the data from the client using the `HeroCreate` model. - -This contains the `password` field with the plain text password, and we cannot use that one. So we need to generate a hash from it. - -{* ./docs_src/tutorial/fastapi/update/tutorial002_py310.py ln[42:44,55:57] hl[57] *} - -## Create an Object with Extra Data - -Now we need to create the database hero. - -In previous examples, we have used something like: - -```Python -db_hero = Hero.model_validate(hero) -``` - -This creates a `Hero` (which is a *table model*) object from the `HeroCreate` (which is a *data model*) object that we received in the request. - -And this is all good... but as `Hero` doesn't have a field `password`, it won't be extracted from the object `HeroCreate` that has it. - -`Hero` actually has a `hashed_password`, but we are not providing it. We need a way to provide it... - -### Dictionary Update - -Let's pause for a second to check this, when working with dictionaries, there's a way to `update` a dictionary with extra data from another dictionary, something like this: - -```Python hl_lines="14" -db_user_dict = { - "name": "Deadpond", - "secret_name": "Dive Wilson", - "age": None, -} - -hashed_password = "fakehashedpassword" - -extra_data = { - "hashed_password": hashed_password, - "age": 32, -} - -db_user_dict.update(extra_data) - -print(db_user_dict) - -# { -# "name": "Deadpond", -# "secret_name": "Dive Wilson", -# "age": 32, -# "hashed_password": "fakehashedpassword", -# } -``` - -This `update` method allows us to add and override things in the original dictionary with the data from another dictionary. - -So now, `db_user_dict` has the updated `age` field with `32` instead of `None` and more importantly, **it has the new `hashed_password` field**. - -### Create a Model Object with Extra Data - -Similar to how dictionaries have an `update` method, **SQLModel** models have a parameter `update` in `Hero.model_validate()` that takes a dictionary with extra data, or data that should take precedence: - -{* ./docs_src/tutorial/fastapi/update/tutorial002_py310.py ln[55:64] hl[60] *} - -Now, `db_hero` (which is a *table model* `Hero`) will extract its values from `hero` (which is a *data model* `HeroCreate`), and then it will **`update`** its values with the extra data from the dictionary `extra_data`. - -It will only take the fields defined in `Hero`, so **it will not take the `password`** from `HeroCreate`. And it will also **take its values** from the **dictionary passed to the `update`** parameter, in this case, the `hashed_password`. - -If there's a field in both `hero` and the `extra_data`, **the value from the `extra_data` passed to `update` will take precedence**. - -## Update with Extra Data - -Now let's say we want to **update a hero** that already exists in the database. - -The same way as before, to avoid removing existing data, we will use `exclude_unset=True` when calling `hero.model_dump()`, to get a dictionary with only the data sent by the client. - -{* ./docs_src/tutorial/fastapi/update/tutorial002_py310.py ln[83:89] hl[89] *} - -Now, this `hero_data` dictionary could contain a `password`. We need to check it, and if it's there, we need to generate the `hashed_password`. - -Then we can put that `hashed_password` in a dictionary. - -And then we can update the `db_hero` object using the method `db_hero.sqlmodel_update()`. - -It takes a model object or dictionary with the data to update the object and also an **additional `update` argument** with extra data. - -{* ./docs_src/tutorial/fastapi/update/tutorial002_py310.py ln[83:99] hl[95] *} - -/// tip - -The method `db_hero.sqlmodel_update()` was added in SQLModel 0.0.16. 😎 - -/// - -## Recap - -You can use the `update` parameter in `Hero.model_validate()` to provide extra data when creating a new object and `Hero.sqlmodel_update()` to provide extra data when updating an existing object. πŸ€“ diff --git a/docs/tutorial/fastapi/update.md b/docs/tutorial/fastapi/update.md deleted file mode 100644 index e3c8ac6ac7..0000000000 --- a/docs/tutorial/fastapi/update.md +++ /dev/null @@ -1,161 +0,0 @@ -# Update Data with FastAPI - -Now let's see how to update data in the database with a **FastAPI** *path operation*. - -## `HeroUpdate` Model - -We want clients to be able to update the `name`, the `secret_name`, and the `age` of a hero. - -But we don't want them to have to include all the data again just to **update a single field**. - -So, we need to make all those fields **optional**. - -And because the `HeroBase` has some of them *required* (without a default value), we will need to **create a new model**. - -/// tip - -Here is one of those cases where it probably makes sense to use an **independent model** instead of trying to come up with a complex tree of models inheriting from each other. - -Because each field is **actually different** (we just set a default value of `None`, but that's already making it different), it makes sense to have them in their own model. - -/// - -So, let's create this new `HeroUpdate` model: - -{* ./docs_src/tutorial/fastapi/update/tutorial001_py310.py ln[5:26] hl[23:26] *} - -This is almost the same as `HeroBase`, but all the fields are optional, so we can't simply inherit from `HeroBase`. - -## Create the Update Path Operation - -Now let's use this model in the *path operation* to update a hero. - -We will use a `PATCH` HTTP operation. This is used to **partially update data**, which is what we are doing. - -{* ./docs_src/tutorial/fastapi/update/tutorial001_py310.py ln[74:89] hl[74:75] *} - -We also read the `hero_id` from the *path parameter* and the request body, a `HeroUpdate`. - -### Read the Existing Hero - -We take a `hero_id` with the **ID** of the hero **we want to update**. - -So, we need to read the hero from the database, with the **same logic** we used to **read a single hero**, checking if it exists, possibly raising an error for the client if it doesn't exist, etc. - -{* ./docs_src/tutorial/fastapi/update/tutorial001_py310.py ln[74:89] hl[77:79] *} - -### Get the New Data - -The `HeroUpdate` model has all the fields with **default values**, because they all have defaults, they are all optional, which is what we want. - -But that also means that if we just call `hero.model_dump()` we will get a dictionary that could potentially have several or all of those values with their defaults, for example: - -```Python -{ - "name": None, - "secret_name": None, - "age": None, -} -``` - -And then, if we update the hero in the database with this data, we would be removing any existing values, and that's probably **not what the client intended**. - -But fortunately Pydantic models (and so SQLModel models) have a parameter we can pass to the `.model_dump()` method for that: `exclude_unset=True`. - -This tells Pydantic to **not include** the values that were **not sent** by the client. Saying it another way, it would **only** include the values that were **sent by the client**. - -So, if the client sent a JSON with no values: - -```JSON -{} -``` - -Then the dictionary we would get in Python using `hero.model_dump(exclude_unset=True)` would be: - -```Python -{} -``` - -But if the client sent a JSON with: - -```JSON -{ - "name": "Deadpuddle" -} -``` - -Then the dictionary we would get in Python using `hero.model_dump(exclude_unset=True)` would be: - -```Python -{ - "name": "Deadpuddle" -} -``` - -Then we use that to get the data that was actually sent by the client: - -{* ./docs_src/tutorial/fastapi/update/tutorial001_py310.py ln[74:89] hl[80] *} - -/// tip -Before SQLModel 0.0.14, the method was called `hero.dict(exclude_unset=True)`, but it was renamed to `hero.model_dump(exclude_unset=True)` to be consistent with Pydantic v2. -/// - -## Update the Hero in the Database - -Now that we have a **dictionary with the data sent by the client**, we can use the method `db_hero.sqlmodel_update()` to update the object `db_hero`. - -{* ./docs_src/tutorial/fastapi/update/tutorial001_py310.py ln[74:89] hl[81] *} - -/// tip - -The method `db_hero.sqlmodel_update()` was added in SQLModel 0.0.16. πŸ€“ - -Before that, you would need to manually get the values and set them using `setattr()`. - -/// - -The method `db_hero.sqlmodel_update()` takes an argument with another model object or a dictionary. - -For each of the fields in the **original** model object (`db_hero` in this example), it checks if the field is available in the **argument** (`hero_data` in this example) and then updates it with the provided value. - -## Remove Fields - -Here's a bonus. 🎁 - -When getting the dictionary of data sent by the client, we only include **what the client actually sent**. - -This sounds simple, but it has some additional nuances that become **nice features**. ✨ - -We are **not simply omitting** the data that has the **default values**. - -And we are **not simply omitting** anything that is `None`. - -This means that if a model in the database **has a value different than the default**, the client could **reset it to the same value as the default**, or even `None`, and we would **still notice it** and **update it accordingly**. πŸ€―πŸš€ - -So, if the client wanted to intentionally remove the `age` of a hero, they could just send a JSON with: - -```JSON -{ - "age": null -} -``` - -And when getting the data with `hero.model_dump(exclude_unset=True)`, we would get: - -```Python -{ - "age": None -} -``` - -So, we would use that value and update the `age` to `None` in the database, **just as the client intended**. - -Notice that `age` here is `None`, and **we still detected it**. - -Also, that `name` was not even sent, and we don't *accidentally* set it to `None` or something. We just didn't touch it because the client didn't send it, so we are **perfectly fine**, even in these corner cases. ✨ - -These are some of the advantages of Pydantic, that we can use with SQLModel. πŸŽ‰ - -## Recap - -Using `.model_dump(exclude_unset=True)` in SQLModel models (and Pydantic models) we can easily update data **correctly**, even in the **edge cases**. 😎 diff --git a/docs/tutorial/index.md b/docs/tutorial/index.md deleted file mode 100644 index dd61e8597e..0000000000 --- a/docs/tutorial/index.md +++ /dev/null @@ -1,33 +0,0 @@ -# Tutorial - User Guide - -In this tutorial you will learn how to use **SQLModel**. - -## Type hints - -If you need a refresher about how to use Python type hints (type annotations), check FastAPI's Python types intro. - -You can also check the mypy cheat sheet. - -**SQLModel** uses type annotations for everything, this way you can use a familiar Python syntax and get all the editor support possible, with autocompletion and in-editor error checking. - -## Intro - -This tutorial shows you how to use **SQLModel** with all its features, step by step. - -Each section gradually builds on the previous ones, but it's structured to separate topics, so that you can go directly to any specific one to solve your specific needs. - -It is also built to work as a future reference. - -So you can come back and see exactly what you need. - -## Run the code - -All the code blocks can be copied and used directly (they are tested Python files). - -It is **HIGHLY encouraged** that you write or copy the code, edit it, and run it locally. - -Using it in your editor is what really shows you the benefits of **SQLModel**, seeing how much code it saves you, and all the editor support you get, with autocompletion and in-editor error checks, preventing lots of bugs. - -Running the examples is what will really help you understand what is going on. - -You can learn a lot more by running some examples and playing around with them than by reading all the docs here. diff --git a/docs/tutorial/indexes.md b/docs/tutorial/indexes.md deleted file mode 100644 index 459ee8ce34..0000000000 --- a/docs/tutorial/indexes.md +++ /dev/null @@ -1,351 +0,0 @@ -# Indexes - Optimize Queries - -We just saw how to get some data `WHERE` a **condition** is true. For example, where the hero **name is "Deadpond"**. - -If we just create the tables and the data as we have been doing, when we `SELECT` some data using `WHERE`, the database would have to **scan** through **each one of the records** to find the ones that **match**. This is not a problem with 3 heroes as in these examples. - -But imagine that your database has **thousands** or **millions** of **records**, if every time you want to find the heroes with the name "Deadpond" it has to scan through **all** of the records to find all the possible matches, then that becomes problematic, as it would be too slow. - -I'll show you how to handle it with a database **index**. - -The change in the code is **extremely small**, but it's useful to understand what's happening behind the scenes, so I'll show you **how it all works** and what it means. - ---- - -If you already executed the previous examples and have a database with data, **remove the database file** before running each example, that way you won't have duplicate data and you will be able to get the same results. - -## No Time to Explain - -Are you already a **SQL expert** and don't have time for all my explanations? - -Fine, in that case, you can **sneak peek** the final code to create indexes here. - -{* ./docs_src/tutorial/indexes/tutorial002_py310.py ln[0] *} - -..but if you are not an expert, **continue reading**, this will probably be useful. πŸ€“ - -## What is an Index - -In general, an **index** is just something we can have to help us **find things faster**. It normally works by having things in **order**. Let's think about some real-life examples before even thinking about databases and code. - -### An Index and a Dictionary - -Imagine a **dictionary**, a book with definitions of words. πŸ“” ...not a Python `dict`. πŸ˜… - -Let's say that you want to **find a word**, for example the word "**database**". You take the dictionary, and open it somewhere, for example in the middle. Maybe you see some definitions of words that start with `m`, like `manual`, so you conclude that you are in the letter `m` in the dictionary. - - - -You know that in the alphabet, the letter `d` for `database` comes **before** the letter `m` for `manual`. - - - -So, you know you have to search in the dictionary **before** the point you currently are. You still don't know where the word `database` is, because you don't know exactly where the letter `d` is in the dictionary, but you know that **it is not after** that point, you can now **discard the right half** of the dictionary in your search. - - - -Next, you **open the dictionary again**, but only taking into account the **half of the dictionary** that can contain the word you want, the **left part of the dictionary**. You open it in the middle of that left part and now you arrive maybe at the letter `f`. - - - -You know that `d` from `database` comes before `f`. So it has to be **before** that. But now you know that `database` **is not after** that point, and you can discard the dictionary from that point onward. - - - -Now you have a **small section of dictionary** to search (only a **quarter** of dictionary can have your word). You take that **quarter** of the pages at the start of the dictionary that can contain your word, and open it in the middle of that section. Maybe you arrive at the letter `c`. - - - -You know the word `database` has to be **after** that and **not before** that point, so you can discard the left part of that block of pages. - - - -You repeat this process **a few more times**, and you finally arrive at the letter `d`, you continue with the same process in that section for the letter `d` and you finally **find the word** `database`. πŸŽ‰ - - - -You had to open the dictionary a few times, maybe **5 or 10**. That's actually **very little work** compared to what it could have been. - -/// note | Technical Details - -Do you like **fancy words**? Cool! Programmers tend to like fancy words. πŸ˜… - -That algorithm I showed you above is called **Binary Search**. - -It's called that because you **search** something by splitting the dictionary (or any ordered list of things) in **two** ("binary" means "two") parts. And you do that process multiple times until you find what you want. - -/// - -### An Index and a Novel - -Let's now imagine you are reading a **novel book**. And someone told you that at some point, they mention a **database**, and you want to find that chapter. - -How do you find the word "*database*" there? You might have to read **the entire book** to find where the word "*database*" is located in the book. So, instead of opening the book 5 or 10 times, you would have to open each of the **500 pages** and read them one by one until you find the word. You might enjoy the book, though. πŸ˜… - -But if we are only interested in **quickly finding information** (as when working with SQL databases), then reading each of the 500 pages is **too inefficient** when there could be an option to open the book in 5 or 10 places and find what you're looking for. - -### A Technical Book with an Index - -Now let's imagine you are reading a technical book. For example, with several topics about programming. And there's a couple of sections where it talks about a **database**. - -This book might have a **book index**: a section in the book that has some **names of topics covered** and the **page numbers** in the book where you can read about them. And the topic names are **sorted** in alphabetic order, pretty much like a dictionary (a book with words, as in the previous example). - -In this case, you can open that book in the end (or in the beginning) to find the **book index** section, it would have only a few pages. And then, you can do the same process as with the **dictionary** example above. - -Open the index, and after **5 or 10 steps**, quickly find the topic "**database**" with the page numbers where that is covered, for example "page 253 in Chapter 5". Now you used the dictionary technique to find the **topic**, and that topic gave you a **page number**. - -Now you know that you need to find "**page 253**". But by looking at the closed book you still don't know where that page is, so you have to **find that page**. To find it, you can do the same process again, but this time, instead of searching for a **topic** in the **index**, you are searching for a **page number** in the **entire book**. And after **5 or 10 more steps**, you find the page 253 in Chapter 5. - - - -After this, even though this book is not a dictionary and has some particular content, you were able to **find the section** in the book that talks about a "**database**" in a **few steps** (say 10 or 20, instead of reading all the 500 pages). - -The main point is that the index is **sorted**, so we can use the same process we used for the **dictionary** to find the topic. And then that gives us a page number, and the **page numbers are also sorted**! πŸ˜… - -When we have a list of sorted things we can apply the same technique, and that's the whole trick here, we use the same technique first for the **topics** in the index and then for the **page numbers** to find the actual chapter. - -Such efficiency! 😎 - -## What are Database Indexes - -**Database indexes** are very similar to **book indexes**. - -Database indexes store some info, some keys, in a way that makes it **easy and fast to find** (for example sorted), and then for each key they **point to some data somewhere else** in the database. - -Let's see a more clear example. Let's say you have this table in a database: - - - - - - - - - - - - - - -
idnamesecret_nameage
1DeadpondDive Wilsonnull
2Spider-BoyPedro Parqueadornull
3Rusty-ManTommy Sharp48
- -And let's imagine you have **many more rows**, many more heroes. Probably **thousands**. - -If you tell the SQL database to get you a hero by a specific name, for example `Spider-Boy` (by using the `name` in the `WHERE` part of the SQL query), the database will have to **scan** all the heroes, checking **one by one** to find all the ones with a name of `Spider-Boy`. - -In this case, there's only one, but there's nothing limiting the database from having **more records with the same name**. And because of that, the database would **continue searching** and checking each one of the records, which would be very slow. - -But now let's say that the database has an index for the column `name`. The index could look something like this, we could imagine that the index is like an additional special table that the database manages automatically: - - - - - - - - - - - - - - -
nameid
Deadpond1
Rusty-Man3
Spider-Boy2
- -It would have each `name` field from the `hero` table **in order**. It would not be sorted by `id`, but by `name` (in alphabetical order, as the `name` is a string). So, first it would have `Deadpond`, then `Rusty-Man`, and last `Spider-Boy`. It would also include the `id` of each hero. Remember that this could have **thousands** of heroes. - -Then the database would be able to use more or less the same ideas in the examples above with the **dictionary** and the **book index**. - -It could start somewhere (for example, in the middle of the index). It could arrive at some hero there in the middle, like `Rusty-Man`. And because the **index** has the `name` fields in order, the database would know that it can **discard all the previous index rows** and **only search** in the following index rows. - - - - - - - - - - - - - - -
nameid
Deadpond1
Rusty-Man3
Spider-Boy2
- -And that way, as with the example with the dictionary above, **instead of reading thousands of heroes**, the database would be able to do a few steps, say **5 or 10 steps**, and arrive at the row of the index that has `Spider-Boy`, even if the table (and index) has thousands of rows: - - - - - - - - - - - - - - -
nameid
Deadpond1
Rusty-Man3
✨ Spider-Boy ✨2
- -Then by looking at **this index row**, it would know that the `id` for `Spider-Boy` in the `hero` table is `2`. - -So then it could **search that `id`** in the `hero` table using more or less the **same technique**. - -That way, in the end, instead of reading thousands of records, the database only had to do **a few steps** to find the hero we wanted. - -## Updating the Index - -As you can imagine, for all this to work, the index would need to be **up to date** with the data in the database. - -If you had to update it **manually** in code, it would be very cumbersome and **error-prone**, as it would be easy to end up in a state where the index is not up to date and points to incorrect data. 😱 - -Here's the good news: when you create an index in a **SQL Database**, the database takes care of **updating** it **automatically** whenever it's necessary. πŸ˜ŽπŸŽ‰ - -If you **add new records** to the `hero` table, the database will **automatically** update the index. It will do the **same process** of **finding** the right place to put the new index data (those **5 or 10 steps** described above), and then it will save the new index information there. The same would happen when you **update** or **delete** data. - -Defining and creating an index is very **easy** with SQL databases. And then **using it** is even easier... it's transparent. The database will figure out which index to use automatically, the SQL queries don't even change. - -So, in SQL databases **indexes are great**! And are super **easy to use**. Why not just have indexes for everything? .....Because indexes also have a "**cost**" in computation and storage (disk space). - -## Index Cost - -There's a **cost** associated with **indexes**. πŸ’° - -When you don't have an index and add a **new row** to the table `hero`, the database has to perform **1 operation** to add the new hero row at the end of the table. - -But if you have an **index** for the **hero names**, now the database has to perform the same **1 operation** to add that row **plus** some extra **5 or 10 operations** in the index, to find the right spot for the name, to then add that **index record** there. - -And if you have an index for the `name`, one for the `age`, and one for the `secret_name`, now the database has to perform the same **1 operation** to add that row **plus** some extra **5 or 10 operations** in the index **times 3**, for each of the indexes. This means that now adding one row takes something like **31 operations**. - -This also means that you are **exchanging** the time it takes to **read** data for the time it takes to **write** data plus some extra **space** in the database. - -If you have queries that get data out of the database comparing each one of those fields (for example using `WHERE`), then it makes total sense to have indexes for each one of them. Because **31 operations** while creating or updating data (plus the space of the index) is much, much better than the possible **500 or 1000 operations** to read all the rows to be able to compare them using each field. - -But if you **never** have queries that find records by the `secret_name` (you never use `secret_name` in the `WHERE` part) it probably doesn't make sense to have an index for the `secret_name` field/column, as that will increase the computational and space **cost** of writing and updating the database. - -## Create an Index with SQL - -Phew, that was a lot of theory and explanations. πŸ˜… - -The most important thing about indexes is **understanding** them, how, and when to use them. - -Let's now see the **SQL** syntax to create an **index**. It is very simple: - -```SQL hl_lines="3" -CREATE INDEX ix_hero_name -ON hero (name) -``` - -This means, more or less: - -> Hey SQL database πŸ‘‹, please `CREATE` an `INDEX` for me. -> -> I want the name of the index to be `ix_hero_name`. -> -> This index should be `ON` the table `hero`, it refers to that table. -> -> The column I want you to use for it is `name`. - -## Declare Indexes with SQLModel - -And now let's see how to define indexes in **SQLModel**. - -The change in code is underwhelming, it's very simple. πŸ˜† - -Here's the `Hero` model we had before: - -{* ./docs_src/tutorial/where/tutorial001_py310.py ln[1:8] hl[6] *} - -Let's now update it to tell **SQLModel** to create an index for the `name` field when creating the table: - -{* ./docs_src/tutorial/indexes/tutorial001_py310.py ln[1:8] hl[6] *} - -We use the same `Field()` again as we did before, and set `index=True`. That's it! πŸš€ - -Notice that we didn't set an argument of `default=None` or anything similar. This means that **SQLModel** (thanks to Pydantic) will keep it as a **required** field. - -/// info - -SQLModel (actually SQLAlchemy) will **automatically generate the index name** for you. - -In this case the generated name would be `ix_hero_name`. - -/// - -## Query Data - -Now, to query the data using the field `name` and the new index we don't have to do anything special or different in the code, it's just **the same code**. - -The SQL database will figure it out **automatically**. ✨ - -This is great because it means that indexes are very **simple to use**. But it might also feel counterintuitive at first, as you are **not doing anything** explicitly in the code to make it obvious that the index is useful, it all happens in the database behind the scenes. - -{* ./docs_src/tutorial/indexes/tutorial001_py310.py ln[34:39] hl[36] *} - -This is exactly the same code as we had before, but now the database will **use the index** underneath. - -## Run the Program - -If you run the program now, you will see an output like this: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// Create the table -CREATE TABLE hero ( - id INTEGER, - name VARCHAR NOT NULL, - secret_name VARCHAR NOT NULL, - age INTEGER, - PRIMARY KEY (id) -) - -// Create the index πŸ€“πŸŽ‰ -CREATE INDEX ix_hero_name ON hero (name) - -// The SELECT with WHERE looks the same -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.name = ? -INFO Engine [no key 0.00014s] ('Deadpond',) - -// The resulting hero -secret_name='Dive Wilson' age=None id=1 name='Deadpond' -``` - -
- -## More Indexes - -We are going to query the `hero` table doing comparisons on the `age` field too, so we should **define an index** for that one as well: - -{* ./docs_src/tutorial/indexes/tutorial002_py310.py ln[1:8] hl[8] *} - -In this case, we want the default value of `age` to continue being `None`, so we set `default=None` when using `Field()`. - -Now when we use **SQLModel** to create the database and tables, it will also create the **indexes** for these two columns in the `hero` table. - -So, when we query the database for the `hero` table and use those **two columns** to define what data we get, the database will be able to **use those indexes** to improve the **reading performance**. πŸš€ - -## Primary Key and Indexes - -You probably noticed that we didn't set `index=True` for the `id` field. - -Because the `id` is already the **primary key**, the database will automatically create an internal **index** for it. - -The database always creates an internal index for **primary keys** automatically, as those are the primary way to organize, store, and retrieve data. πŸ€“ - -But if you want to be **frequently querying** the SQL database for any **other field** (e.g. using any other field in the `WHERE` section), you will probably want to have at least an **index** for that. - -## Recap - -**Indexes** are very important to improve **reading performance** and speed when querying the database. 🏎 - -Creating and using them is very **simple** and easy. The most important part is to understand **how** they work, **when** to create them, and for **which columns**. diff --git a/docs/tutorial/insert.md b/docs/tutorial/insert.md deleted file mode 100644 index c11633d6ce..0000000000 --- a/docs/tutorial/insert.md +++ /dev/null @@ -1,374 +0,0 @@ -# Create Rows - Use the Session - INSERT - -Now that we have a database and a table, we can start adding data. - -Here's a reminder of how the table would look like, this is the data we want to add: - - - - - - - - - - - - - - -
idnamesecret_nameage
1DeadpondDive Wilsonnull
2Spider-BoyPedro Parqueadornull
3Rusty-ManTommy Sharp48
- -## Create Table and Database - -We will continue from where we left off in the last chapter. - -This is the code we had to create the database and table, nothing new here: - -//// tab | Python 3.10+ - -```{.python .annotate hl_lines="20" } -{!./docs_src/tutorial/create_db_and_table/tutorial003_py310.py[ln:1-18]!} - -# More code here later πŸ‘ˆ - -{!./docs_src/tutorial/create_db_and_table/tutorial003_py310.py[ln:21-22]!} -``` - -{!./docs_src/tutorial/create_db_and_table/annotations/en/tutorial003.md!} - -//// - -//// tab | Python 3.8+ - -```{.python .annotate hl_lines="22" } -{!./docs_src/tutorial/create_db_and_table/tutorial003.py[ln:1-20]!} - -# More code here later πŸ‘ˆ - -{!./docs_src/tutorial/create_db_and_table/tutorial003.py[ln:23-24]!} -``` - -{!./docs_src/tutorial/create_db_and_table/annotations/en/tutorial003.md!} - -//// - -Now that we can create the database and the table, we will continue from this point and add more code on the same file to create the data. - -## Create Data with SQL - -Before working with Python code, let's see how we can create data with SQL. - -Let's say we want to insert the record/row for `Deadpond` into our database. - -We can do this with the following SQL code: - -```SQL -INSERT INTO "hero" ("name", "secret_name") -VALUES ("Deadpond", "Dive Wilson"); -``` - -It means, more or less: - -> Hey SQL database πŸ‘‹, please `INSERT` something (create a record/row) `INTO` the table `"hero"`. -> -> I want you to insert a row with some values in these specific columns: -> -> * `"name"` -> * `"secret_name"` -> -> And the values I want you to put in these columns are: -> -> * `"Deadpond"` -> * `"Dive Wilson"` - -### Try it in DB Explorer for SQLite - -You can try that SQL statement in **DB Explorer for SQLite**. - -Make sure to open the same database we already created by clicking Open Database and selecting the same `database.db` file. - -/// tip - -If you don't have that `database.db` file with the table `hero`, you can re-create it by running the Python program at the top. πŸ‘† - -/// - -Then go to the Execute SQL tab and copy the SQL from above. - -It would look like this: - - - -Click the "Execute all" β–Ά button. - -Then you can go to the Browse Data tab, and you will see your newly created record/row: - - - -## Data in a Database and Data in Code - -When working with a database (SQL or any other type) in a programming language, we will always have some data **in memory**, in objects and variables we create in our code, and there will be some data **in the database**. - -We are constantly **getting** *some* of the data from the database and putting it in memory, in variables. - -The same way, we are constantly **creating** variables and objects with data in our code, that we then want to save in the database, so we **send** it somehow. - -In some cases, we can even create some data in memory and then change it and update it before saving it in the database. - -We might even decide with some logic in the code that we no longer want to save the data in the database, and then just remove it. πŸ”₯ And we only handled that data in memory, without sending it back and forth to the database. - -**SQLModel** does all it can (actually via SQLAlchemy) to make this interaction as simple, intuitive, and familiar or "close to programming" as possible. ✨ - -But that division of the two places where some data might be at each moment in time (in memory or in the database) is always there. And it's important for you to have it in mind. πŸ€“ - -## Create Data with Python and **SQLModel** - -Now let's create that same row in Python. - -First, remove that file `database.db` so we can start from a clean slate. - -Because we have Python code executing with data in memory, and the database is an independent system (an external SQLite file, or an external database server), we need to perform two steps: - -* create the data in Python, in memory (in a variable) -* save/send the data to the database - -## Create a Model Instance - -Let's start with the first step, create the data in memory. - -We already created a class `Hero` that represents the `hero` table in the database. - -Each instance we create will represent the data in a row in the database. - -So, the first step is to simply create an instance of `Hero`. - -We'll create 3 right away, for the 3 heroes: - -{* ./docs_src/tutorial/insert/tutorial002_py310.py ln[21:24] *} - -/// tip - -The code above in this file (the omitted code) is just the same code that you see at the top of this chapter. - -The same code we used before to create the `Hero` model. - -/// - -We are putting that in a function `create_heroes()`, to call it later once we finish it. - -If you are trying the code interactively, you could also write that directly. - -## Create a **Session** - -Up to now, we have only used the **engine** to interact with the database. - -The **engine** is that single object that we share with all the code, and that is in charge of communicating with the database, handling the connections (when using a server database like PostgreSQL or MySQL), etc. - -But when working with **SQLModel** you will mostly use another tool that sits on top, the **Session**. - -In contrast to the **engine** that is one for the whole application, we create a new **session** for each group of operations with the database that belong together. - -In fact, the **session** needs and uses an **engine**. - -For example, if we have a web application, we would normally have a single **session** per request. - -We would re-use the same **engine** in all the code, everywhere in the application (shared by all the requests). But for each request, we would create and use a new **session**. And once the request is done, we would close the session. - -The first step is to import the `Session` class: - -{* ./docs_src/tutorial/insert/tutorial001_py310.py ln[1] hl[1] *} - -Then we can create a new session: - -{* ./docs_src/tutorial/insert/tutorial001_py310.py ln[21:26] hl[26] *} - -The new `Session` takes an `engine` as a parameter. And it will use the **engine** underneath. - -/// tip - -We will see a better way to create a **session** using a `with` block later. - -/// - -## Add Model Instances to the Session - -Now that we have some hero model instances (some objects in memory) and a **session**, the next step is to add them to the session: - -{* ./docs_src/tutorial/insert/tutorial001_py310.py ln[21:30] hl[28:30] *} - -By this point, our heroes are *not* stored in the database yet. - -And this is one of the cases where having a **session** independent of an **engine** makes sense. - -The session is holding in memory all the objects that should be saved in the database later. - -And once we are ready, we can **commit** those changes, and then the **session** will use the **engine** underneath to save all the data by sending the appropriate SQL to the database, and that way it will create all the rows. All in a single batch. - -This makes the interactions with the database more efficient (plus some extra benefits). - -/// info | Technical Details - -The session will create a new transaction and execute all the SQL code in that transaction. - -This ensures that the data is saved in a single batch, and that it will all succeed or all fail, but it won't leave the database in a broken state. - -/// - -## Commit the Session Changes - -Now that we have the heroes in the **session** and that we are ready to save all that to the database, we can **commit** the changes: - -{* ./docs_src/tutorial/insert/tutorial001_py310.py ln[21:32] hl[32] *} - -Once this line is executed, the **session** will use the **engine** to save all the data in the database by sending the corresponding SQL. - -## Create Heroes as a Script - -The function to create the heroes is now ready. - -Now we just need to make sure to call it when we run this program with Python directly. - -We already had a main block like: - -```Python -if __name__ == "__main__": - create_db_and_tables() -``` - -We could add the new function there, as: - -```Python -if __name__ == "__main__": - create_db_and_tables() - create_heroes() -``` - -But to keep things a bit more organized, let's instead create a new function `main()` that will contain all the code that should be executed when called as an independent script, and we can put there the previous function `create_db_and_tables()`, and add the new function `create_heroes()`: - -{* ./docs_src/tutorial/insert/tutorial002_py310.py ln[34:36] hl[34,36] *} - -And then we can call that single `main()` function from that main block: - -{* ./docs_src/tutorial/insert/tutorial002_py310.py ln[34:40] hl[40] *} - -By having everything that should happen when called as a script in a single function, we can easily add more code later on. - -And some other code could also import and use this same `main()` function if it was necessary. - -## Run the Script - -Now we can run our program as a script from the console. - -Because we created the **engine** with `echo=True`, it will print out all the SQL code that it is executing: - -
- -```console -$ python app.py -// Some boilerplate, checking that the hero table already exists -INFO Engine BEGIN (implicit) -INFO Engine PRAGMA main.table_info("hero") -INFO Engine [raw sql] () -INFO Engine COMMIT -// BEGIN a transaction automatically ✨ -INFO Engine BEGIN (implicit) -// Our INSERT statement, it uses VALUES (?, ?, ?) as parameters -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -// ...and these are the parameter values πŸš€ -INFO Engine [generated in 0.00013s] ('Deadpond', 'Dive Wilson', None) -// Again, for Spider-Boy -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [cached since 0.000755s ago] ('Spider-Boy', 'Pedro Parqueador', None) -// And now for Rusty-Man -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [cached since 0.001014s ago] ('Rusty-Man', 'Tommy Sharp', 48) -// All good? Yes, commit this transaction! πŸŽ‰ -INFO Engine COMMIT -``` - -
- -If you have ever used Git, this works very similarly. - -We use `session.add()` to add new objects (model instances) to the session (similar to `git add`). - -And that ends up in a group of data ready to be saved, but not saved yet. - -We can make more modifications, add more objects, etc. - -And once we are ready, we can **commit** all the changes in a single step (similar to `git commit`). - -## Close the Session - -The **session** holds some resources, like connections from the engine. - -So once we are done with the session, we should **close** it to make it release those resources and finish its cleanup: - -{* ./docs_src/tutorial/insert/tutorial001_py310.py ln[21:34] hl[34] *} - -But what happens if we forget to close the session? - -Or if there's an exception in the code and it never reaches the `session.close()`? - -For that, there's a better way to create and close the session, using a `with` block. πŸ‘‡ - -## A Session in a `with` Block - -It's good to know how the `Session` works and how to create and close it manually. It might be useful if, for example, you want to explore the code in an interactive session (for example with Jupyter). - -But there's a better way to handle the session, using a `with` block: - -{* ./docs_src/tutorial/insert/tutorial002_py310.py ln[21:31] hl[26:31] *} - -This is the same as creating the session manually and then manually closing it. But here, using a `with` block, it will be automatically created when **starting** the `with` block and assigned to the variable `session`, and it will be automatically closed after the `with` block is **finished**. - -And it will work even if there's an exception in the code. 😎 - -## Review All the Code - -Let's give this whole file a final look. πŸ” - -You already know all of the first part for creating the `Hero` model class, the **engine**, and creating the database and table. - -Let's focus on the new code: - -//// tab | Python 3.10+ - -```{.python .annotate } -{!./docs_src/tutorial/insert/tutorial003_py310.py!} -``` - -{!./docs_src/tutorial/insert/annotations/en/tutorial003.md!} - -//// - -//// tab | Python 3.8+ - -```{.python .annotate } -{!./docs_src/tutorial/insert/tutorial003.py!} -``` - -{!./docs_src/tutorial/insert/annotations/en/tutorial003.md!} - -//// - -/// tip - -Review what each line does by clicking each number bubble in the code. πŸ‘† - -/// - -You can now put it in a `app.py` file and run it with Python. And you will see an output like the one shown above. - -After that, if you open the database with **DB Browser for SQLite**, you will see the data you just created in the Browse Data tab: - - - -## What's Next - -Now you know how to add rows to the database. πŸŽ‰ - -Now is a good time to understand better why the `id` field **can't be `NULL`** on the database because it's a **primary key**, but actually **can be `None`** in the Python code. - -I'll tell you about that in the next chapter. πŸš€ diff --git a/docs/tutorial/limit-and-offset.md b/docs/tutorial/limit-and-offset.md deleted file mode 100644 index 215f57d1cd..0000000000 --- a/docs/tutorial/limit-and-offset.md +++ /dev/null @@ -1,223 +0,0 @@ -# Read a Range of Data - LIMIT and OFFSET - -Now you know how to get a single row with `.one()`, `.first()`, and `session.get()`. - -And you also know how to get multiple rows while filtering them using `.where()`. - -Now let's see how to get only a **range of results**. - -table with first 3 rows selected - -## Create Data - -We will continue with the same code as before, but we'll modify it a little the `select_heroes()` function to simplify the example and focus on what we want to achieve here. - -Again, we will create several heroes to have some data to select from: - -{* ./docs_src/tutorial/offset_and_limit/tutorial001_py310.py ln[21:39] hl[22:28] *} - -## Review Select All - -This is the code we had to select all the heroes in the `select()` examples: - -{* ./docs_src/tutorial/select/tutorial003_py310.py ln[34:39] hl[34:39] *} - -But this would get us **all** the heroes at the same time, in a database that could have thousands, that could be problematic. - -## Select with Limit - -We currently have 7 heroes in the database. But we could as well have thousands, so let's limit the results to get only the first 3: - -{* ./docs_src/tutorial/offset_and_limit/tutorial001_py310.py ln[42:47] hl[44] *} - -The special **select** object we get from `select()` also has a method `.limit()` that we can use to limit the results to a certain number. - -In this case, instead of getting all the 7 rows, we are limiting them to only get the first 3. - -table with first 3 rows selected - -## Run the Program on the Command Line - -If we run it on the command line, it will output: - -
- -```console -$ python app.py - -// Previous output omitted πŸ™ˆ - -// Select with LIMIT -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero - LIMIT ? OFFSET ? -INFO Engine [no key 0.00014s] (3, 0) - -// Print the heroes received, only 3 -[ - Hero(age=None, secret_name='Dive Wilson', id=1, name='Deadpond'), - Hero(age=None, secret_name='Pedro Parqueador', id=2, name='Spider-Boy'), - Hero(age=48, secret_name='Tommy Sharp', id=3, name='Rusty-Man') -] -``` - -
- -Great! We got only 3 heroes as we wanted. - -/// tip - -We will check out that SQL code more in a bit. - -/// - -## Select with Offset and Limit - -Now we can limit the results to get only the first 3. - -But imagine we are in a user interface showing the results in batches of 3 heroes at a time. - -/// tip - -This is commonly called "pagination". Because the user interface would normally show a "page" of a predefined number of heroes at a time. - -And then you can interact with the user interface to get the next page, and so on. - -/// - -How do we get the next 3? - -table with next rows selected, from 4 to 6 - -We can use `.offset()`: - -{* ./docs_src/tutorial/offset_and_limit/tutorial002_py310.py ln[42:47] hl[44] *} - -The way this works is that the special **select** object we get from `select()` has methods like `.where()`, `.offset()` and `.limit()`. - -Each of those methods applies the change in the internal special select statement object, and also **return the same object**, this way, we can continue using more methods on it, like in the example above that we use both `.offset()` and `.limit()`. - -**Offset** means "skip this many rows", and as we want to skip the ones we already saw, the first three, we use `.offset(3)`. - -## Run the Program with Offset on the Command Line - -Now we can run the program on the command line, and it will output: - -
- -```console -$python app.py - -// Previous output omitted πŸ™ˆ - -// Select with LIMIT and OFFSET -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero - LIMIT ? OFFSET ? -INFO Engine [no key 0.00020s] (3, 3) - -// Print the 3 heroes received, the second batch -[ - Hero(age=32, secret_name='Natalia Roman-on', id=4, name='Tarantula'), - Hero(age=35, secret_name='Trevor Challa', id=5, name='Black Lion'), - Hero(age=36, secret_name='Steve Weird', id=6, name='Dr. Weird') -] -``` - -
- -## Select Next Batch - -Then to get the next batch of 3 rows we would offset all the ones we already saw, the first 6: - -{* ./docs_src/tutorial/offset_and_limit/tutorial003_py310.py ln[42:47] hl[44] *} - -The database right now has **only 7 rows**, so this query can only get 1 row. - -table with the last row (7th) selected - -But don't worry, the database won't throw an error trying to get 3 rows when there's only one (as would happen with a Python list). - -The database knows that we want to **limit** the number of results, but it doesn't necessarily have to find that many results. - -## Run the Program with the Last Batch on the Command Line - -And if we run it in the command line, it will output: - -
- -```console -$ python app.py - -// Previous output omitted πŸ™ˆ - -// Select last batch with LIMIT and OFFSET -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero - LIMIT ? OFFSET ? -INFO Engine [no key 0.00038s] (3, 6) - -// Print last batch of heroes, only one -[ - Hero(age=93, secret_name='Esteban Rogelios', id=7, name='Captain North America') -] -``` - -
- -## SQL with LIMIT and OFFSET - -You probably noticed the new SQL keywords `LIMIT` and `OFFSET`. - -You can use them in SQL, at the end of the other parts: - -```SQL -SELECT id, name, secret_name, age -FROM hero -LIMIT 3 OFFSET 6 -``` - -If you try that in **DB Browser for SQLite**, you will get the same result: - -DB Browser for SQLite showing the result of the SQL query - -## Combine Limit and Offset with Where - -Of course, you can also combine `.limit()` and `.offset()` with `.where()` and other methods you will learn about later: - -{* ./docs_src/tutorial/offset_and_limit/tutorial004_py310.py ln[42:47] hl[44] *} - -## Run the Program with Limit, Offset, and Where on the Command Line - -If we run it on the command line, it will find all the heroes in the database with an age above 32. That would normally be 4 heroes. - -But we are starting to include after an offset of 1 (so we don't count the first one), and we are limiting the results to only get the first 2 after that: - -
- -```console -$ python app.py - -// Previous output omitted πŸ™ˆ - -// Select with WHERE and LIMIT and OFFSET -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.age > ? - LIMIT ? OFFSET ? -INFO Engine [no key 0.00022s] (32, 2, 1) - -// Print the heroes received, only 2 -[ - Hero(age=36, id=6, name='Dr. Weird', secret_name='Steve Weird'), - Hero(age=48, id=3, name='Rusty-Man', secret_name='Tommy Sharp') -] -``` - -
- -## Recap - -Independently of how you filter the data with `.where()` or other methods, you can limit the query to get at maximum some number of results with `.limit()`. - -And the same way, you can skip the first results with `.offset()`. diff --git a/docs/tutorial/many-to-many/create-data.md b/docs/tutorial/many-to-many/create-data.md deleted file mode 100644 index 3c8c0cfa6a..0000000000 --- a/docs/tutorial/many-to-many/create-data.md +++ /dev/null @@ -1,130 +0,0 @@ -# Create Data with Many-to-Many Relationships - -Let's continue from where we left and create some data. - -We'll create data for this same **many-to-many** relationship with a link table: - -many-to-many table relationships - -We'll continue from where we left off with the previous code. - -{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[0] *} - -## Create Heroes - -As we have done before, we'll create a function `create_heroes()` and we'll create some teams and heroes in it: - -{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[36:54] hl[44] *} - -This is very similar to what we have done before. - -We create a couple of teams, and then three heroes. - -The only new detail is that instead of using an argument `team` we now use `teams`, because that is the name of the new **relationship attribute**. And more importantly, we pass a **list of teams** (even if it contains a single team). - -See how **Deadpond** now belongs to the two teams? - -## Commit, Refresh, and Print - -Now let's do as we have done before, `commit` the **session**, `refresh` the data, and print it: - -{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[36:69] hl[55:58,60:62,64:69] *} - -## Add to Main - -As before, add the `create_heroes()` function to the `main()` function to make sure it is called when running this program from the command line: - -{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[72:74] *} - -## Run the Program - -If we run the program from the command line, it would output: - -
- -```console -$ python app.py - -// Previous output omitted πŸ™ˆ - -// Automatically start a new transaction -INFO Engine BEGIN (implicit) -// Insert the hero data first -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [generated in 0.00041s] ('Deadpond', 'Dive Wilson', None) -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [cached since 0.001942s ago] ('Rusty-Man', 'Tommy Sharp', 48) -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [cached since 0.002541s ago] ('Spider-Boy', 'Pedro Parqueador', None) -// Insert the team data second -INFO Engine INSERT INTO team (name, headquarters) VALUES (?, ?) -INFO Engine [generated in 0.00037s] ('Z-Force', 'Sister Margaret's Bar') -INFO Engine INSERT INTO team (name, headquarters) VALUES (?, ?) -INFO Engine [cached since 0.001239s ago] ('Preventers', 'Sharp Tower') -// Insert the link data last, to be able to re-use the created IDs -INFO Engine INSERT INTO heroteamlink (team_id, hero_id) VALUES (?, ?) -INFO Engine [generated in 0.00026s] ((2, 3), (1, 1), (2, 1), (2, 2)) -// Commit and save the data in the database -INFO Engine COMMIT - -// Automatically start a new transaction -INFO Engine BEGIN (implicit) -// Refresh the data -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.id = ? -INFO Engine [generated in 0.00019s] (1,) -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.001959s ago] (2,) -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.003215s ago] (3,) - -// Print Deadpond -Deadpond: name='Deadpond' age=None id=1 secret_name='Dive Wilson' - -// Accessing the .team attribute triggers a refresh -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team, heroteamlink -WHERE ? = heroteamlink.hero_id AND team.id = heroteamlink.team_id -INFO Engine [generated in 0.00025s] (1,) - -// Print Deadpond's teams, 2 teams! πŸŽ‰ -Deadpond teams: [Team(id=1, name='Z-Force', headquarters='Sister Margaret's Bar'), Team(id=2, name='Preventers', headquarters='Sharp Tower')] - -// Print Rusty-Man -Rusty-Man: name='Rusty-Man' age=48 id=2 secret_name='Tommy Sharp' - -// Accessing the .team attribute triggers a refresh -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team, heroteamlink -WHERE ? = heroteamlink.hero_id AND team.id = heroteamlink.team_id -INFO Engine [cached since 0.001716s ago] (2,) - -// Print Rusty-Man teams, just one, but still a list -Rusty-Man Teams: [Team(id=2, name='Preventers', headquarters='Sharp Tower')] - -// Print Spider-Boy -Spider-Boy: name='Spider-Boy' age=None id=3 secret_name='Pedro Parqueador' - -// Accessing the .team attribute triggers a refresh -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team, heroteamlink -WHERE ? = heroteamlink.hero_id AND team.id = heroteamlink.team_id -INFO Engine [cached since 0.002739s ago] (3,) - -// Print Spider-Boy's teams, just one, but still a list -Spider-Boy Teams: [Team(id=2, name='Preventers', headquarters='Sharp Tower')] - -// Automatic roll back any previous automatic transaction, at the end of the with block -INFO Engine ROLLBACK -``` - -
- -## Recap - -After setting up the model link, using it with **relationship attributes** is fairly straightforward, just Python objects. ✨ diff --git a/docs/tutorial/many-to-many/create-models-with-link.md b/docs/tutorial/many-to-many/create-models-with-link.md deleted file mode 100644 index af6563e1ab..0000000000 --- a/docs/tutorial/many-to-many/create-models-with-link.md +++ /dev/null @@ -1,124 +0,0 @@ -# Create Models with a Many-to-Many Link - -We'll now support **many-to-many** relationships using a **link table** like this: - -many-to-many table relationships - -Let's start by defining the class models, including the **link table** model. - -## Link Table Model - -As we want to support a **many-to-many** relationship, now we need a **link table** to connect them. - -We can create it just as any other **SQLModel**: - -{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[1:6] hl[4:6] *} - -This is a **SQLModel** class model table like any other. - -It has two fields, `team_id` and `hero_id`. - -They are both **foreign keys** to their respective tables. We'll create those models in a second, but you already know how that works. - -And **both fields are primary keys**. We hadn't used this before. πŸ€“ - -## Team Model - -Let's see the `Team` model, it's almost identical as before, but with a little change: - -{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[9:14] hl[14] *} - -The **relationship attribute `heroes`** is still a list of heroes, annotated as `list["Hero"]`. Again, we use `"Hero"` in quotes because we haven't declared that class yet by this point in the code (but as you know, editors and **SQLModel** understand that). - -We use the same **`Relationship()`** function. - -We use **`back_populates="teams"`**. Before we referenced an attribute `team`, but as now we can have many, we'll rename it to `teams` when creating the `Hero` model. - -And here's the important part to allow the **many-to-many** relationship, we use **`link_model=HeroTeamLink`**. That's it. ✨ - -## Hero Model - -Let's see the other side, here's the `Hero` model: - -{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[17:23] hl[23] *} - -We **removed** the previous `team_id` field (column) because now the relationship is done via the link table. πŸ”₯ - -The relationship attribute is now named **`teams`** instead of `team`, as now we support multiple teams. - -It no longer has a type of `Team | None` but a list of teams, the type is now declared as **`list[Team]`**. - -We are using the **`Relationship()`** here too. - -We still have **`back_populates="heroes"`** as before. - -And now we have a **`link_model=HeroTeamLink`**. ✨ - -## Create the Tables - -The same as before, we will have the rest of the code to create the **engine**, and a function to create all the tables `create_db_and_tables()`. - -{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[26:33] hl[32] *} - -And as in previous examples, we will add that function to a function `main()`, and we will call that `main()` function in the main block: - -{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[72:73,77:78] hl[73] *} - -## Run the Code - -If you run the code in the command line, it would output: - -
- -```console -$ python app.py - -// Boilerplate omitted πŸ˜‰ - -INFO Engine -CREATE TABLE team ( - id INTEGER, - name VARCHAR NOT NULL, - headquarters VARCHAR NOT NULL, - PRIMARY KEY (id) -) - - -INFO Engine [no key 0.00033s] () -INFO Engine -CREATE TABLE hero ( - id INTEGER, - name VARCHAR NOT NULL, - secret_name VARCHAR NOT NULL, - age INTEGER, - PRIMARY KEY (id) -) - - -INFO Engine [no key 0.00016s] () -INFO Engine - -// Our shinny new link table ✨ -CREATE TABLE heroteamlink ( - team_id INTEGER, - hero_id INTEGER, - PRIMARY KEY (team_id, hero_id), - FOREIGN KEY(team_id) REFERENCES team (id), - FOREIGN KEY(hero_id) REFERENCES hero (id) -) - - -INFO Engine [no key 0.00031s] () -INFO Engine COMMIT - -``` - -
- -## Recap - -We can support **many-to-many** relationships between tables by declaring a link table. - -We can create it the same way as with other **SQLModel** classes, and then use it in the `link_model` parameter to `Relationship()`. - -Now let's work with data using these models in the next chapters. πŸ€“ diff --git a/docs/tutorial/many-to-many/index.md b/docs/tutorial/many-to-many/index.md deleted file mode 100644 index 5cb3067e4a..0000000000 --- a/docs/tutorial/many-to-many/index.md +++ /dev/null @@ -1,182 +0,0 @@ -# Many to Many - Intro - -We saw how to work with One-to-Many relationships in the data. - -But how do you handle **Many-to-Many** relationships? - -Let's explore them. πŸš€ - -## Starting from One-to-Many - -Let's start with the familiar and simpler option of **One-to-Many**. - -We have one table with teams and one with heroes, and for each **one** team, we can have **many** heroes. - -As each team could have multiple heroes, we wouldn't be able to put the Hero IDs in columns for all of them in the `team` table. - -But as each hero can belong **only to one** team, we have a **single column** in the heroes table to point to the specific team (to a specific row in the `team` table). - -The `team` table looks like this: - - - - - - - - - - - -
idnameheadquarters
1PreventersSharp Tower
2Z-ForceSister Margaret's Bar
- -/// tip - -Notice that it doesn't have any foreign key to other tables. - -/// - -And the `hero` table looks like this: - - - - - - - - - - - - - - -
idnamesecret_nameageteam_id
1DeadpondDive Wilsonnull2
2Spider-BoyPedro Parqueadornull1
3Rusty-ManTommy Sharp481
- -We have a column in the `hero` table for the `team_id` that points to the ID of a specific team in the `team` table. - -This is how we connect each `hero` with a `team`: - -table relationships - -Notice that each hero can only have **one** connection. But each team can receive **many** connections. In particular, the team **Preventers** has two heroes. - -## Introduce Many-to-Many - -But let's say that as **Deadpond** is a great character, they recruit him to the new **Preventers** team, but he's still part of the **Z-Force** team too. - -So, now, we need to be able to have a hero that is connected to **many** teams. And then, each team, should still be able to receive **many** heroes. So we need a **Many-to-Many** relationship. - -A naive approach that wouldn't work very well is to add more columns to the `hero` table. Imagine we add two extra columns. Now we could connect a single `hero` to 3 teams in total, but not more. So we haven't really solved the problem of supporting **many** teams, only a very limited fixed number of teams. - -We can do better! πŸ€“ - -## Link Table - -We can create another table that would represent the link between the `hero` and `team` tables. - -All this table contains is two columns, `hero_id` and `team_id`. - -Both columns are **foreign keys** pointing to the ID of a specific row in the `hero` and `team` tables. - -As this will represent the **hero-team-link**, let's call the table `heroteamlink`. - -It would look like this: - -many-to-many table relationships - -Notice that now the table `hero` **doesn't have a `team_id`** column anymore, it is replaced by this link table. - -And the `team` table, just as before, doesn't have any foreign key either. - -Specifically, the new link table `heroteamlink` would be: - - - - - - - - - - - - - - - - - -
hero_idteam_id
11
12
21
31
- -/// info - -Other names used for this **link table** are: - -* association table -* secondary table -* junction table -* intermediate table -* join table -* through table -* relationship table -* connection table - -I'm using the term "link table" because it's short, doesn't collide with other terms already used (e.g. "relationship"), it's easy to remember how to write it, etc. - -/// - -## Link Primary Key - -Cool, we have a link table with **just two columns**. But remember that SQL databases [require each row to have a **primary key**](../../databases.md#identifications-primary-key){.internal-link target=_blank} that **uniquely identifies** the row in that table? - -Now, what is the **primary key** in this table? - -How to we identify each unique row? - -Should we add another column just to be the **primary key** of this link table? Nope! We don't have to do that. πŸ‘Œ - -**Both columns are the primary key** of each row in this table (and each row just has those two columns). ✨ - -A primary key is a way to **uniquely identify** a particular row in a **single table**. But it doesn't have to be a single column. - -A primary key can be a group of the columns in a table, which combined are unique in this table. - -Check the table above again, see that **each row has a unique combination** of `hero_id` and `team_id`? - -We cannot have duplicated primary keys, which means that we cannot have duplicated links between `hero` and `team`, exactly what we want! - -For example, the database will now prevent an error like this, with a duplicated row: - - - - - - - - - - - - - - - - - - - - -
hero_idteam_id
11
12
21
31
3 🚨1 🚨
- -It wouldn't make sense to have a hero be part of the **same team twice**, right? - -Now, just by using the two columns as the primary keys of this table, SQL will take care of **preventing us from duplicating** a link between `hero` and `team`. βœ… - -## Recap - -An intro with a recap! That's weird... but anyway. 🀷 - -Now you have the theory about the **many-to-many** relationships, and how to solve them with tables in SQL. πŸ€“ - -Now let's check how to write the SQL and the code to work with them. πŸš€ diff --git a/docs/tutorial/many-to-many/link-with-extra-fields.md b/docs/tutorial/many-to-many/link-with-extra-fields.md deleted file mode 100644 index 7c7756bcd2..0000000000 --- a/docs/tutorial/many-to-many/link-with-extra-fields.md +++ /dev/null @@ -1,312 +0,0 @@ -# Link Model with Extra Fields - -In the previous example we never interacted directly with the `HeroTeamLink` model, it was all through the automatic **many-to-many** relationship. - -But what if we needed to have additional data to describe the link between the two models? - -Let's say that we want to have an extra field/column to say if a hero **is still training** in that team or if they are already going on missions and stuff. - -Let's see how to achieve that. - -## Link Model with Two One-to-Many - -The way to handle this is to explicitly use the link model, to be able to get and modify its data (apart from the foreign keys pointing to the two models for `Hero` and `Team`). - -In the end, the way it works is just like two **one-to-many** relationships combined. - -A row in the table `heroteamlink` points to **one** particular hero, but a single hero can be connected to **many** hero-team links, so it's **one-to-many**. - -And also, the same row in the table `heroteamlink` points to **one** team, but a single team can be connected to **many** hero-team links, so it's also **one-to-many**. - -/// tip - -The previous many-to-many relationship was also just two one-to-many relationships combined, but now it's going to be much more explicit. - -/// - -## Update Link Model - -Let's update the `HeroTeamLink` model. - -We will add a new field `is_training`. - -And we will also add two **relationship attributes**, for the linked `team` and `hero`: - -{* ./docs_src/tutorial/many_to_many/tutorial003_py310.py ln[4:10] hl[7,9:10] *} - -The new **relationship attributes** have their own `back_populates` pointing to new relationship attributes we will create in the `Hero` and `Team` models: - -* `team`: has `back_populates="hero_links"`, because in the `Team` model, the attribute will contain the links to the **team's heroes**. -* `hero`: has `back_populates="team_links"`, because in the `Hero` model, the attribute will contain the links to the **hero's teams**. - -/// info - -In SQLAlchemy this is called an Association Object or Association Model. - -I'm calling it **Link Model** just because that's easier to write avoiding typos. But you are also free to call it however you want. πŸ˜‰ - -/// - -## Update Team Model - -Now let's update the `Team` model. - -We no longer have the `heroes` relationship attribute, and instead we have the new `hero_links` attribute: - -{* ./docs_src/tutorial/many_to_many/tutorial003_py310.py ln[13:18] hl[18] *} - -## Update Hero Model - -The same with the `Hero` model. - -We change the `teams` relationship attribute for `team_links`: - -{* ./docs_src/tutorial/many_to_many/tutorial003_py310.py ln[21:27] hl[27] *} - -## Create Relationships - -Now the process to create relationships is very similar. - -But now we create the **explicit link models** manually, pointing to their hero and team instances, and specifying the additional link data (`is_training`): - -{* ./docs_src/tutorial/many_to_many/tutorial003_py310.py ln[40:79] hl[58:67,69:72] *} - -We are just adding the link model instances to the session, because the link model instances are connected to the heroes and teams, they will be also automatically included in the session when we commit. - -## Run the Program - -Now, if we run the program, it will show almost the same output as before, because it is generating almost the same SQL, but this time including the new `is_training` column: - -
- -```console -$ python app.py - -// Previous output omitted πŸ™ˆ - -// Automatically start a new transaction -INFO Engine BEGIN (implicit) - -// Insert the heroes -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [generated in 0.00025s] ('Deadpond', 'Dive Wilson', None) -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [cached since 0.00136s ago] ('Spider-Boy', 'Pedro Parqueador', None) -INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) -INFO Engine [cached since 0.001858s ago] ('Rusty-Man', 'Tommy Sharp', 48) - -// Insert the teams -INFO Engine INSERT INTO team (name, headquarters) VALUES (?, ?) -INFO Engine [generated in 0.00019s] ('Z-Force', 'Sister Margaret's Bar') -INFO Engine INSERT INTO team (name, headquarters) VALUES (?, ?) -INFO Engine [cached since 0.0007985s ago] ('Preventers', 'Sharp Tower') - -// Insert the hero-team links -INFO Engine INSERT INTO heroteamlink (team_id, hero_id, is_training) VALUES (?, ?, ?) -INFO Engine [generated in 0.00023s] ((1, 1, 0), (2, 1, 1), (2, 2, 1), (2, 3, 0)) -// Save the changes in the transaction in the database -INFO Engine COMMIT - -// Automatically start a new transaction -INFO Engine BEGIN (implicit) - -// Automatically fetch the data on attribute access -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team -WHERE team.id = ? -INFO Engine [generated in 0.00028s] (1,) -INFO Engine SELECT heroteamlink.team_id AS heroteamlink_team_id, heroteamlink.hero_id AS heroteamlink_hero_id, heroteamlink.is_training AS heroteamlink_is_training -FROM heroteamlink -WHERE ? = heroteamlink.team_id -INFO Engine [generated in 0.00026s] (1,) -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [generated in 0.00024s] (1,) - -// Print Z-Force hero data, including link data -Z-Force hero: name='Deadpond' age=None id=1 secret_name='Dive Wilson' is training: False - -// Automatically fetch the data on attribute access -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team -WHERE team.id = ? -INFO Engine [cached since 0.008822s ago] (2,) -INFO Engine SELECT heroteamlink.team_id AS heroteamlink_team_id, heroteamlink.hero_id AS heroteamlink_hero_id, heroteamlink.is_training AS heroteamlink_is_training -FROM heroteamlink -WHERE ? = heroteamlink.team_id -INFO Engine [cached since 0.005778s ago] (2,) - -// Print Preventers hero data, including link data -Preventers hero: name='Deadpond' age=None id=1 secret_name='Dive Wilson' is training: True - -// Automatically fetch the data on attribute access -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.004196s ago] (2,) - -// Print Preventers hero data, including link data -Preventers hero: name='Spider-Boy' age=None id=2 secret_name='Pedro Parqueador' is training: True - -// Automatically fetch the data on attribute access -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.006005s ago] (3,) - -// Print Preventers hero data, including link data -Preventers hero: name='Rusty-Man' age=48 id=3 secret_name='Tommy Sharp' is training: False -``` - -
- -## Add Relationships - -Now, to add a new relationship, we have to create a new `HeroTeamLink` instance pointing to the hero and the team, add it to the session, and commit it. - -Here we do that in the `update_heroes()` function: - -{* ./docs_src/tutorial/many_to_many/tutorial003_py310.py ln[82:97] hl[89:94] *} - -## Run the Program with the New Relationship - -If we run that program, we will see the output: - -
- -```console -$ python app.py - -// Previous output omitted πŸ™ˆ - -// Automatically start a new transaction -INFO Engine BEGIN (implicit) - -// Select the hero -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.name = ? -INFO Engine [no key 0.00014s] ('Spider-Boy',) - -// Select the team -INFO Engine SELECT team.id, team.name, team.headquarters -FROM team -WHERE team.name = ? -INFO Engine [no key 0.00012s] ('Z-Force',) - -// Create the link -INFO Engine INSERT INTO heroteamlink (team_id, hero_id, is_training) VALUES (?, ?, ?) -INFO Engine [generated in 0.00023s] (1, 2, 1) - -// Automatically refresh the data on attribute access -INFO Engine SELECT heroteamlink.team_id AS heroteamlink_team_id, heroteamlink.hero_id AS heroteamlink_hero_id, heroteamlink.is_training AS heroteamlink_is_training -FROM heroteamlink -WHERE ? = heroteamlink.team_id -INFO Engine [cached since 0.01514s ago] (1,) -INFO Engine COMMIT -INFO Engine BEGIN (implicit) -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.08953s ago] (2,) -INFO Engine SELECT heroteamlink.team_id AS heroteamlink_team_id, heroteamlink.hero_id AS heroteamlink_hero_id, heroteamlink.is_training AS heroteamlink_is_training -FROM heroteamlink -WHERE ? = heroteamlink.hero_id -INFO Engine [generated in 0.00018s] (2,) - -// Print updated hero links -Updated Spider-Boy's Teams: [ - HeroTeamLink(team_id=2, is_training=True, hero_id=2), - HeroTeamLink(team_id=1, is_training=True, hero_id=2) -] - -// Automatically refresh team data on attribute access -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team -WHERE team.id = ? -INFO Engine [cached since 0.1084s ago] (1,) -INFO Engine SELECT heroteamlink.team_id AS heroteamlink_team_id, heroteamlink.hero_id AS heroteamlink_hero_id, heroteamlink.is_training AS heroteamlink_is_training -FROM heroteamlink -WHERE ? = heroteamlink.team_id -INFO Engine [cached since 0.1054s ago] (1,) - -// Print team hero links -Z-Force heroes: [ - HeroTeamLink(team_id=1, is_training=False, hero_id=1), - HeroTeamLink(team_id=1, is_training=True, hero_id=2) -] -``` - -
- -## Update Relationships with Links - -Now let's say that **Spider-Boy** has been training enough in the **Preventers**, and they say he can join the team full time. - -So now we want to update the status of `is_training` to `False`. - -We can do that by iterating on the links: - -{* ./docs_src/tutorial/many_to_many/tutorial003_py310.py ln[82:83,99:107] hl[99:101] *} - -## Run the Program with the Updated Relationships - -And if we run the program now, it will output: - -
- -```console -$ python app.py - -// Previous output omitted πŸ™ˆ - -// Automatically fetch team data on attribute access -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team -WHERE team.id = ? -INFO Engine [generated in 0.00015s] (2,) - -// Update link row -INFO Engine UPDATE heroteamlink SET is_training=? WHERE heroteamlink.team_id = ? AND heroteamlink.hero_id = ? -INFO Engine [generated in 0.00020s] (0, 2, 2) - -// Save current transaction to database -INFO Engine COMMIT - -// Automatically start a new transaction -INFO Engine BEGIN (implicit) - -// Automatically fetch data on attribute access -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.2004s ago] (2,) -INFO Engine SELECT heroteamlink.team_id AS heroteamlink_team_id, heroteamlink.hero_id AS heroteamlink_hero_id, heroteamlink.is_training AS heroteamlink_is_training -FROM heroteamlink -WHERE ? = heroteamlink.hero_id -INFO Engine [cached since 0.1005s ago] (2,) -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team -WHERE team.id = ? -INFO Engine [cached since 0.09707s ago] (2,) - -// Print Spider-Boy team, including link data, if is training -Spider-Boy team: headquarters='Sharp Tower' id=2 name='Preventers' is training: False - -// Automatically fetch data on attribute access -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team -WHERE team.id = ? -INFO Engine [cached since 0.2097s ago] (1,) - -// Print Spider-Boy team, including link data, if is training -Spider-Boy team: headquarters='Sister Margaret's Bar' id=1 name='Z-Force' is training: True -INFO Engine ROLLBACK -``` - -
- -## Recap - -If you need to store more information about a **many-to-many** relationship you can use an explicit link model with extra data in it. πŸ€“ diff --git a/docs/tutorial/many-to-many/update-remove-relationships.md b/docs/tutorial/many-to-many/update-remove-relationships.md deleted file mode 100644 index ebc9ba3a85..0000000000 --- a/docs/tutorial/many-to-many/update-remove-relationships.md +++ /dev/null @@ -1,187 +0,0 @@ -# Update and Remove Many-to-Many Relationships - -Now we'll see how to update and remove these **many-to-many** relationships. - -We'll continue from where we left off with the previous code. - -{* ./docs_src/tutorial/many_to_many/tutorial001_py310.py ln[0] *} - -## Get Data to Update - -Let's now create a function `update_heroes()`. - -We'll get **Spider-Boy** and the **Z-Force** team. - -As you already know how these goes, I'll use the **short version** and get the data in a single Python statement. - -And because we are now using `select()`, we also have to import it. - -{* ./docs_src/tutorial/many_to_many/tutorial002_py310.py ln[1,72:77] hl[1,72:77] *} - -And of course, we have to add `update_heroes()` to our `main()` function: - -{* ./docs_src/tutorial/many_to_many/tutorial002_py310.py ln[94:101] hl[97] *} - -## Add Many-to-Many Relationships - -Now let's imagine that **Spider-Boy** thinks that the **Z-Force** team is super cool and decides to go there and join them. - -We can use the same **relationship attributes** to include `hero_spider_boy` in the `team_z_force.heroes`. - -{* ./docs_src/tutorial/many_to_many/tutorial002_py310.py ln[72:84] hl[79:81,83:84] *} - -/// tip - -Because we are accessing an attribute in the models right after we commit, with `hero_spider_boy.teams` and `team_z_force.heroes`, the data is refreshed automatically. - -So we don't have to call `session.refresh()`. - -/// - -We then commit the change, refresh, and print the updated **Spider-Boy**'s heroes to confirm. - -Notice that we only `add` **Z-Force** to the session, then we commit. - -We never add **Spider-Boy** to the session, and we never even refresh it. But we still print his teams. - -This still works correctly because we are using `back_populates` in the `Relationship()` in the models. That way, **SQLModel** (actually SQLAlchemy) can keep track of the changes and updates, and make sure they also happen on the relationships in the other related models. πŸŽ‰ - -## Run the Program - -You can confirm it's all working by running the program in the command line: - -
- -```console -$ python app.py - -// Previous output omitted πŸ™ˆ - -// Create the new many-to-many relationship -INFO Engine INSERT INTO heroteamlink (team_id, hero_id) VALUES (?, ?) -INFO Engine [generated in 0.00020s] (1, 3) -INFO Engine COMMIT - -// Start a new automatic transaction -INFO Engine BEGIN (implicit) - -// Automatically refresh the data while accessing the attribute .teams -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [generated in 0.00044s] (3,) -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team, heroteamlink -WHERE ? = heroteamlink.hero_id AND team.id = heroteamlink.team_id -INFO Engine [cached since 0.1648s ago] (3,) - -// Print Spider-Boy teams, including Z-Force πŸŽ‰ -Updated Spider-Boy's Teams: [ - Team(id=2, name='Preventers', headquarters='Sharp Tower'), - Team(id=1, name='Z-Force', headquarters='Sister Margaret's Bar') -] - -// Automatically refresh the data while accessing the attribute .heores -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero, heroteamlink -WHERE ? = heroteamlink.team_id AND hero.id = heroteamlink.hero_id -INFO Engine [cached since 0.1499s ago] (1,) - -// Print Z-Force heroes, including Spider-Boy πŸŽ‰ -Z-Force heroes: [ - Hero(name='Deadpond', age=None, id=1, secret_name='Dive Wilson'), - Hero(name='Spider-Boy', age=None, id=3, secret_name='Pedro Parqueador', teams=[ - Team(id=2, name='Preventers', headquarters='Sharp Tower'), - Team(id=1, name='Z-Force', headquarters='Sister Margaret's Bar', heroes=[...]) - ]) -] -``` - -
- -## Remove Many-to-Many Relationships - -Now let's say that right after joining the team, **Spider-Boy** realized that their "life preserving policies" are much more relaxed than what he's used to. πŸ’€ - -And their *occupational safety and health* is also not as great... πŸ’₯ - -So, **Spider-Boy** decides to leave **Z-Force**. - -Let's update the relationships to remove `team_z_force` from `hero_spider_boy.teams`. - -Because `hero_spider_boy.teams` is just a list (a special list managed by SQLAlchemy, but a list), we can use the standard list methods. - -In this case, we use the method `.remove()`, that takes an item and removes it from the list. - -{* ./docs_src/tutorial/many_to_many/tutorial002_py310.py ln[72:91] hl[86:88,90:91] *} - -And this time, just to show again that by using `back_populates` **SQLModel** (actually SQLAlchemy) takes care of connecting the models by their relationships, even though we performed the operation from the `hero_spider_boy` object (modifying `hero_spider_boy.teams`), we are adding `team_z_force` to the **session**. And we commit that, without even add `hero_spider_boy`. - -This still works because by updating the teams in `hero_spider_boy`, because they are synchronized with `back_populates`, the changes are also reflected in `team_z_force`, so it also has changes to be saved in the DB (that **Spider-Boy** was removed). - -And then we add the team, and commit the changes, which updates the `team_z_force` object, and because it changed the table that also had a connection with the `hero_spider_boy`, it is also marked internally as updated, so it all works. - -And then we just print them again to confirm that everything worked correctly. - -## Run the Program Again - -To confirm that this last part worked, you can run the program again, it will output something like: - -
- -```console -$ python app.py - -// Previous output omitted πŸ™ˆ - -// Delete the row in the link table -INFO Engine DELETE FROM heroteamlink WHERE heroteamlink.team_id = ? AND heroteamlink.hero_id = ? -INFO Engine [generated in 0.00043s] (1, 3) -// Save the changes -INFO Engine COMMIT - -// Automatically start a new transaction -INFO Engine BEGIN (implicit) - -// Automatically refresh the data while accessing the attribute .heroes -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team -WHERE team.id = ? -INFO Engine [generated in 0.00029s] (1,) -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero, heroteamlink -WHERE ? = heroteamlink.team_id AND hero.id = heroteamlink.hero_id -INFO Engine [cached since 0.5625s ago] (1,) - -// Print the Z-Force heroes after reverting the changes -Reverted Z-Force's heroes: [ - Hero(name='Deadpond', age=None, id=1, secret_name='Dive Wilson') -] - -// Automatically refresh the data while accessing the attribute .teams -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [cached since 0.4209s ago] (3,) -INFO Engine SELECT team.id AS team_id, team.name AS team_name, team.headquarters AS team_headquarters -FROM team, heroteamlink -WHERE ? = heroteamlink.hero_id AND team.id = heroteamlink.team_id -INFO Engine [cached since 0.5842s ago] (3,) - -// Print Spider-Boy's teams after reverting the changes -Reverted Spider-Boy's teams: [ - Team(id=2, name='Preventers', headquarters='Sharp Tower') -] - -// Automatically roll back any possible previously unsaved transaction -INFO Engine ROLLBACK - -``` - -
- -## Recap - -Updating and removing many-to-many relationships is quite straightforward after setting up the **link model** and the relationship attributes. - -You can just use common list operation. πŸš€ diff --git a/docs/tutorial/one.md b/docs/tutorial/one.md deleted file mode 100644 index 54cdbbea93..0000000000 --- a/docs/tutorial/one.md +++ /dev/null @@ -1,275 +0,0 @@ -# Read One Row - -You already know how to filter rows to select using `.where()`. - -And you saw how when executing a `select()` it normally returns an **iterable** object. - -Or you can call `results.all()` to get a **list** of all the rows right away, instead of an iterable. - -But in many cases you really just want to read a **single row**, and having to deal with an iterable or a list is not as convenient. - -Let's see the utilities to read a single row. - -## Continue From Previous Code - -We'll continue with the same examples we have been using in the previous chapters to create and select data and we'll keep updating them. - -{* ./docs_src/tutorial/indexes/tutorial002_py310.py ln[0] *} - -If you already executed the previous examples and have a database with data, **remove the database file** before running each example, that way you won't have duplicate data and you will be able to get the same results. - -## Read the First Row - -We have been iterating over the rows in a `result` object like: - -{* ./docs_src/tutorial/indexes/tutorial002_py310.py ln[42:47] hl[46:47] *} - -But let's say that we are not interested in all the rows, just the **first** one. - -We can call the `.first()` method on the `results` object to get the first row: - -{* ./docs_src/tutorial/one/tutorial001_py310.py ln[42:47] hl[46] *} - -This will return the first object in the `results` (if there was any). - -That way, we don't have to deal with an iterable or a list. - -/// tip - -Notice that `.first()` is a method of the `results` object, not of the `select()` statement. - -/// - -Although this query would find two rows, by using `.first()` we get only the first row. - -If we run it in the command line it would output: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// The SELECT with WHERE -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.age <= ? -INFO Engine [no key 0.00021s] (35,) - -// Only print the first item -Hero: secret_name='Natalia Roman-on' age=32 id=4 name='Tarantula' -``` - -
- -## First or `None` - -It would be possible that the SQL query doesn't find any row. - -In that case, `.first()` will return `None`: - -{* ./docs_src/tutorial/one/tutorial002_py310.py ln[42:47] hl[44,46] *} - -In this case, as there's no hero with an age less than 25, `.first()` will return `None`. - -When we run it in the command line it will output: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// The SELECT with WHERE -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.age <= ? -INFO Engine [no key 0.00021s] (35,) - -// Now rows found, first is None -Hero: None -``` - -
- -## Exactly One - -There might be cases where we want to ensure that there's exactly **one** row matching the query. - -And if there was more than one, it would mean that there's an error in the system, and we should terminate with an error. - -In that case, instead of `.first()` we can use `.one()`: - -{* ./docs_src/tutorial/one/tutorial003_py310.py ln[42:47] hl[46] *} - -Here we know that there's only one `"Deadpond"`, and there shouldn't be any more than one. - -If we run it once will output: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// The SELECT with WHERE -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.name = ? -INFO Engine [no key 0.00015s] ('Deadpond',) - -// Only one row found, we're good βœ… -Hero: secret_name='Dive Wilson' age=None id=1 name='Deadpond' -``` - -
- -But if we run it again, as it will create and insert all the heroes in the database again, they will be duplicated, and there will be more than one `"Deadpond"`. 😱 - -So, running it again, without first deleting the file `database.db` will output: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// The SELECT with WHERE -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.name = ? -INFO Engine [no key 0.00015s] ('Deadpond',) - -// Oh, no, the database is in a broken state, with duplicates! 🚨 -Traceback (most recent call last): - -// Some details about the error omitted - -sqlalchemy.exc.MultipleResultsFound: Multiple rows were found when exactly one was required -``` - -
- -## Exactly One with More Data - -Of course, even if we don't duplicate the data, we could get the same error if we send a query that finds more than one row and expect exactly one with `.one()`: - -{* ./docs_src/tutorial/one/tutorial004_py310.py ln[42:47] hl[44,46] *} - -That would find 2 rows, and would end up with the same error. - -## Exactly One with No Data - -And also, if we get no rows at all with `.one()`, it will also raise an error: - -{* ./docs_src/tutorial/one/tutorial005_py310.py ln[42:47] hl[44,46] *} - -In this case, as there are no heroes with an age less than 25, `.one()` will raise an error. - -This is what we would get if we run it in the command line: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// SELECT with WHERE -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.age < ? -INFO Engine [no key 0.00014s] (25,) - -// Oh, no, we expected one row but there aren't any! 🚨 -Traceback (most recent call last): - -// Some details about the error omitted - -sqlalchemy.exc.NoResultFound: No row was found when one was required -``` - -
- -## Compact Version - -Of course, with `.first()` and `.one()` you would also probably write all that in a more compact form most of the time, all in a single line (or at least a single Python statement): - -{* ./docs_src/tutorial/one/tutorial006_py310.py ln[42:45] hl[44] *} - -That would result in the same as some examples above. - -## Select by Id with `.where()` - -In many cases you might want to select a single row by its Id column with the **primary key**. - -You could do it the same way we have been doing with a `.where()` and then getting the first item with `.first()`: - -{* ./docs_src/tutorial/one/tutorial007_py310.py ln[42:47] hl[44,46] *} - -That would work correctly, as expected. But there's a shorter version. πŸ‘‡ - -## Select by Id with `.get()` - -As selecting a single row by its Id column with the **primary key** is a common operation, there's a shortcut for it: - -{* ./docs_src/tutorial/one/tutorial008_py310.py ln[42:45] hl[44] *} - -`session.get(Hero, 1)` is an equivalent to creating a `select()`, then filtering by Id using `.where()`, and then getting the first item with `.first()`. - -If you run it, it will output: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// SELECT with WHERE -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [generated in 0.00021s] (1,) - -// The printed result -Hero: secret_name='Dive Wilson' age=None id=1 name='Deadpond' -``` - -
- -## Select by Id with `.get()` with No Data - -`.get()` behaves similar to `.first()`, if there's no data it will simply return `None` (instead of raising an error): - -{* ./docs_src/tutorial/one/tutorial009_py310.py ln[42:45] hl[44] *} - -Running that will output: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// SELECT with WHERE -INFO Engine BEGIN (implicit) -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age -FROM hero -WHERE hero.id = ? -INFO Engine [generated in 0.00024s] (9001,) - -// No data found, so the value is None -Hero: None -``` - -
- -## Recap - -As querying the SQL database for a single row is a common operation, you now have several tools to do it in a short and simple way. πŸŽ‰ diff --git a/docs/tutorial/relationship-attributes/back-populates.md b/docs/tutorial/relationship-attributes/back-populates.md deleted file mode 100644 index 9bdcbf0c28..0000000000 --- a/docs/tutorial/relationship-attributes/back-populates.md +++ /dev/null @@ -1,215 +0,0 @@ -# Relationship back_populates - -Now you know how to use the **relationship attributes** to manipulate connected data in the database! πŸŽ‰ - -Let's now take a small step back and review how we defined those `Relationship()` attributes again, let's clarify that `back_populates` argument. πŸ€“ - -## Relationship with `back_populates` - -So, what is that `back_populates` argument in each `Relationship()`? - -The value is a string with the name of the attribute in the **other** model class. - - - -That tells **SQLModel** that if something changes in this model, it should change that attribute in the other model, and it will work even before committing with the session (that would force a refresh of the data). - -Let's understand that better with an example. - -## An Incomplete Relationship - -Let's see how that works by writing an **incomplete** version first, without `back_populates`: - -{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[1:19] hl[9,19] *} - -## Read Data Objects - -Now, we will get the **Spider-Boy** hero and, *independently*, the **Preventers** team using two `select`s. - -As you already know how this works, I won't separate that in a select `statement`, `results`, etc. Let's use the shorter form in a single call: - -{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:111] hl[105:107,109:111] *} - -/// tip - -When writing your own code, this is probably the style you will use most often, as it's shorter, more convenient, and you still get all the power of autocompletion and inline errors. - -/// - -## Print the Data - -Now, let's print the current **Spider-Boy**, the current **Preventers** team, and particularly, the current **Preventers** list of heroes: - -{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:115] hl[113:115] *} - -Up to this point, it's all good. 😊 - -In particular, the result of printing `preventers_team.heroes` is: - -``` hl_lines="3" -Preventers Team Heroes: [ - Hero(name='Rusty-Man', age=48, id=2, secret_name='Tommy Sharp', team_id=2), - Hero(name='Spider-Boy', age=None, id=3, secret_name='Pedro Parqueador', team_id=2), - Hero(name='Tarantula', age=32, id=6, secret_name='Natalia Roman-on', team_id=2), - Hero(name='Dr. Weird', age=36, id=7, secret_name='Steve Weird', team_id=2), - Hero(name='Captain North America', age=93, id=8, secret_name='Esteban Rogelios', team_id=2) -] -``` - -Notice that we have **Spider-Boy** there. - -## Update Objects Before Committing - -Now let's update **Spider-Boy**, removing him from the team by setting `hero_spider_boy.team = None` and then let's print this object again: - -{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:104,117:121] hl[117,121] *} - -The first important thing is, we *haven't committed* the hero yet, so accessing the list of heroes would not trigger an automatic refresh. - -But in our code, in this exact point in time, we already said that **Spider-Boy** is no longer part of the **Preventers**. πŸ”₯ - -/// tip - -We could revert that later by not committing the **session**, but that's not what we are interested in here. - -/// - -Here, at this point in the code, in memory, the code expects **Preventers** to *not include* **Spider-Boy**. - -The output of printing `hero_spider_boy` without team is: - -``` -Spider-Boy without team: name='Spider-Boy' age=None id=3 secret_name='Pedro Parqueador' team_id=2 team=None -``` - -Cool, the team is set to `None`, the `team_id` attribute still has the team ID until we save it. But that's okay as we are now working mainly with the **relationship attributes** and the objects. βœ… - -But now, what happens when we print the `preventers_team.heroes`? - -``` hl_lines="3" -Preventers Team Heroes again: [ - Hero(name='Rusty-Man', age=48, id=2, secret_name='Tommy Sharp', team_id=2), - Hero(name='Spider-Boy', age=None, id=3, secret_name='Pedro Parqueador', team_id=2, team=None), - Hero(name='Tarantula', age=32, id=6, secret_name='Natalia Roman-on', team_id=2), - Hero(name='Dr. Weird', age=36, id=7, secret_name='Steve Weird', team_id=2), - Hero(name='Captain North America', age=93, id=8, secret_name='Esteban Rogelios', team_id=2) -] -``` - -Oh, no! 😱 **Spider-Boy** is still listed there! - -## Commit and Print - -Now, if we commit it and print again: - -{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:104,123:130] hl[123:124,130] *} - -When we access `preventers_team.heroes` after the `commit`, that triggers a refresh, so we get the latest list, without **Spider-Boy**, so that's fine again: - -``` -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age, hero.team_id AS hero_team_id -FROM hero -WHERE ? = hero.team_id -2021-08-13 11:15:24,658 INFO sqlalchemy.engine.Engine [cached since 0.1924s ago] (2,) - -Preventers Team Heroes after commit: [ - Hero(name='Rusty-Man', age=48, id=2, secret_name='Tommy Sharp', team_id=2), - Hero(name='Tarantula', age=32, id=6, secret_name='Natalia Roman-on', team_id=2), - Hero(name='Dr. Weird', age=36, id=7, secret_name='Steve Weird', team_id=2), - Hero(name='Captain North America', age=93, id=8, secret_name='Esteban Rogelios', team_id=2) -] -``` - -There's no **Spider-Boy** after committing, so that's good. 😊 - -But we still have that inconsistency in that previous point above. - -If we use the objects before committing, we could end up having errors. πŸ˜” - -Let's fix that. πŸ€“ - -## Fix It Using `back_populates` - -That's what `back_populates` is for. ✨ - -Let's add it back: - -{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[1:19] hl[9,19] *} - -And we can keep the rest of the code the same: - -{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[103:104,117:121] hl[117,121] *} - -/// tip - -This is the same section where we updated `hero_spider_boy.team` to `None` but we *haven't committed* that change yet. - -The same section that caused a problem before. - -/// - -## Review the Result - -This time, **SQLModel** (actually SQLAlchemy) will be able to notice the change, and **automatically update the list of heroes** in the team, even before we commit. - -That second print would output: - -``` -Preventers Team Heroes again: [ - Hero(name='Rusty-Man', age=48, id=2, secret_name='Tommy Sharp', team_id=2), - Hero(name='Tarantula', age=32, id=6, secret_name='Natalia Roman-on', team_id=2), - Hero(name='Dr. Weird', age=36, id=7, secret_name='Steve Weird', team_id=2), - Hero(name='Captain North America', age=93, id=8, secret_name='Esteban Rogelios', team_id=2) -] -``` - -Notice that now **Spider-Boy** is not there, we fixed it with `back_populates`! πŸŽ‰ - -## The Value of `back_populates` - -Now that you know why `back_populates` is there, let's review the exact value again. - -It's quite simple code, it's just a string, but it might be confusing to think exactly *what* string should go there: - -{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[1:19] hl[9,19] *} - -The string in `back_populates` is the name of the attribute *in the other* model, that will reference *the current* model. - - - -So, in the class `Team`, we have an attribute `heroes` and we declare it with `Relationship(back_populates="team")`. - -{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[4:9] hl[9] *} - -The string in `back_populates="team"` refers to the attribute `team` in the class `Hero` (the other class). - -And, in the class `Hero`, we declare an attribute `team`, and we declare it with `Relationship(back_populates="heroes")`. - -So, the string `"heroes"` refers to the attribute `heroes` in the class `Team`. - -{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[12:19] hl[19] *} - -/// tip - -Each **relationship attribute** points to the other one, in the other model, using `back_populates`. - -/// - -Although it's simple code, it can be confusing to think about 😡, because the same line has concepts related to both models in multiple places: - -* Just by being in the **current** model, the line has something to do with the current model. -* The name of the attribute is about the **other** model. -* The type annotation is about the **other** model. -* And the `back_populates` refers to an attribute in the **other** model, that points to the **current** model. - -## A Mental Trick to Remember `back_populates` - -A mental trick you can use to remember is that the string in `back_populates` is always about the current model class you are editing. πŸ€“ - -So, if you are in the class `Hero`, the value of `back_populates` for any relationship attribute connecting to **any** other table (to any other model, it could be `Team`, `Weapon`, `Powers`, etc) will still always refer to this same class. - -So, `back_populates` would most probably be something like `"hero"` or `"heroes"`. - - - -{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py310.py ln[27:39] hl[27,34,37,39] *} diff --git a/docs/tutorial/relationship-attributes/cascade-delete-relationships.md b/docs/tutorial/relationship-attributes/cascade-delete-relationships.md deleted file mode 100644 index 3b6fe9156c..0000000000 --- a/docs/tutorial/relationship-attributes/cascade-delete-relationships.md +++ /dev/null @@ -1,651 +0,0 @@ -# Cascade Delete Relationships - -What happens if we **delete** a team that has a **relationship** with heroes? - -Should those heroes be **automatically deleted** too? That's called a "**cascade**", because the initial deletion causes a cascade of other deletions. - -Should their `team_id` instead be set to `NULL` in the database? - -Let's see how to configure that with **SQLModel**. - -/// info - -This feature, including `cascade_delete`, `ondelete`, and `passive_deletes`, is available since SQLModel version `0.0.21`. - -/// - -## Initial Heroes and Teams - -Let's say that we have these **teams** and **heroes**. - -### Team Table - -| id | name | headquarters | -| ---- | ---------- | --------------------- | -| 1 | Z-Force | Sister Margaret's Bar | -| 2 | Preventers | Sharp Tower | -| 3 | Wakaland | Wakaland Capital City | - -### Hero Table - -| id | name | secret_name | age | team_id | -| ---- | --------------- | ---------------- | ---- | ------- | -| 1 | Deadpond | Dive WIlson | | 1 | -| 2 | Rusty-Man | Tommy Sharp | 48 | 2 | -| 3 | Spider-Boy | Pedro Parqueador | | 2 | -| 4 | Black Lion | Trevor Challa | 35 | 3 | -| 5 | Princess Sure-E | Sure-E | | 3 | - -### Visual Teams and Heroes - -We could visualize them like this: - -```mermaid -flowchart TB - subgraph "Z-Force" - d("Deadpond") - end - subgraph "Preventers" - r("Rusty-Man") - s("Spider-Boy") - end - subgraph "Wakaland" - b("Black Lion") - p("Princess Sure-E") - end -``` - -## Delete a Team with Heroes - -When we **delete a team**, we have to do something with the associated heroes. - -By default, their foreign key pointing to the team will be set to `NULL` in the database. - -But let's say we want the associated heroes to be **automatically deleted**. - -For example, we could delete the team `Wakaland`: - -```mermaid -flowchart TB - subgraph zforce["Z-Force"] - d("Deadpond") - end - subgraph preventers["Preventers"] - r("Rusty-Man") - s("Spider-Boy") - end - subgraph wakaland["Wakaland"] - b("Black Lion") - p("Princess Sure-E") - end - style wakaland fill:#fee,stroke:#900 -``` - -And we would want the heroes `Black Lion` and `Princess Sure-E` to be **automatically deleted** too. - -So we would end up with these teams and heroes: - -```mermaid -flowchart TB - subgraph zforce["Team Z-Force"] - d("Deadpond") - end - subgraph preventers["Team Preventers"] - r("Rusty-Man") - s("Spider-Boy") - end -``` - -## Configure Automatic Deletion - -There are **two places** where this automatic deletion is configured: - -* in **Python code** -* in the **database** - -## Delete in Python with `cascade_delete` - -When creating a `Relationship()`, we can set `cascade_delete=True`. - -This configures SQLModel to **automatically delete** the related records (heroes) **when the initial one is deleted** (a team). - -{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py ln[1:9] hl[9] *} - -With this configuration, when we delete a team, SQLModel (actually SQLAlchemy) will: - -* Make sure the objects for the **related records are loaded**, in this case, the `heroes`. If they are not loaded, it will send a `SELECT` query to the database to get them. -* Send a `DELETE` query to the database **including each related record** (each hero). -* Finally, **delete the initial record** (the team) with another `DELETE` query. - -This way, the internal **Python code** will take care of deleting the related records, by emitting the necessary SQL queries for each of them. - -/// tip - -The `cascade_delete` parameter is set in the `Relationship()`, on the model that **doesn't have a foreign key**. - -/// - -/// note | Technical Details - -Setting `cascade_delete=True` in the `Relationship()` will configure SQLAlchemy to use `cascade="all, delete-orphan"`, which is the most common and useful configuration when wanting to cascade deletes. - -You can read more about it in the SQLAlchemy docs. - -/// - -## Delete in the Database with `ondelete` - -In the previous section we saw that using `cascade_delete` handles automatic deletions from the Python code. - -But what happens if someone **interacts with the database directly**, not using our code, and **deletes a team with SQL**? - -For those cases, we can configure the database to **automatically delete** the related records with the `ondelete` parameter in `Field()`. - -### `ondelete` Options - -The `ondelete` parameter will set a SQL `ON DELETE` in the **foreign key column** in the database. - -`ondelete` can have these values: - -* `CASCADE`: **Automatically delete this record** (hero) when the related one (team) is deleted. -* `SET NULL`: Set this **foreign key** (`hero.team_id`) field to `NULL` when the related record is deleted. -* `RESTRICT`: **Prevent** the deletion of this record (hero) if there is a foreign key value by raising an error. - -## Set `ondelete` to `CASCADE` - -If we want to configure the database to **automatically delete** the related records when the parent is deleted, we can set `ondelete="CASCADE"`. - -{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py ln[1:19] hl[18] *} - -Now, when we **create the tables** in the database, the `team_id` column in the `Hero` table will have an `ON DELETE CASCADE` in its definition at the database level. - -This will **configure the database** to **automatically delete** the records (heroes) when the related record (team) is deleted. - -/// tip - -The `ondelete` parameter is set in the `Field()`, on the model that **has a foreign key**. - -/// - -## Using `cascade_delete` or `ondelete` - -At this point, you might be wondering if you should use `cascade_delete` or `ondelete`. The answer is: **both**! πŸ€“ - -The `ondelete` will **configure the database**, in case someone interacts with it directly. - -But `cascade_delete` is still needed to tell SQLAlchemy that it should delete the **Python objects** in memory. - -### Foreign Key Constraint Support - -Some databases don't support foreign key constraints. - -For example, **SQLite** doesn't support them by default. They have to be manually enabled with a custom SQL command: - -``` -PRAGMA foreign_keys = ON; -``` - -So, in general it's a good idea to have both `cascade_delete` and `ondelete` configured. - -/// tip - -You will learn more about how to **disable the default** automatic SQLModel (SQLAlchemy) behavior and **only rely on the database** down below, in the section about `passive_deletes`. - -/// - -### `cascade_delete` on `Relationship()` and `ondelete` on `Field()` - -Just a note to remember... πŸ€“ - -* `ondelete` is put on the `Field()` with a **foreign key**. On the **"many"** side in "one-to-many" relationships. - -```Python -class Hero(SQLModel, table=True): - ... - - team_id: int = Field(foreign_key="team.id", ondelete="CASCADE") -``` - -* `cascade_delete` is put on the `Relationship()`. Normally on the **"one"** side in "one-to-many" relationships, the side **without a foreign key**. - -```Python -class Team(SQLModel, table=True): - ... - - heroes: list[Hero] = Relationship(cascade_delete=True) -``` - -## Remove a Team and its Heroes - -Now, when we **delete a team**, we don't need to do anything else, it's **automatically** going to **delete its heroes**. - -{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py ln[76:82] hl[80] *} - -## Confirm Heroes are Deleted - -We can confirm that **after deleting the team** `Wakaland`, the heroes `Black Lion` and `Princess Sure-E` are **also deleted**. - -If we try to select them from the database, we will **no longer find them**. - -{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py ln[85:95] hl[87,90,92,95] *} - -## Run the Program with `cascade_delete=True` and `ondelete="CASCADE"` - -We can confirm everything is working by running the program. - -
- -```console -$ python app.py - -// Some boilerplate and previous output omitted πŸ˜‰ - -// The team table is created as before -CREATE TABLE team ( - id INTEGER NOT NULL, - name VARCHAR NOT NULL, - headquarters VARCHAR NOT NULL, - PRIMARY KEY (id) -) - -// The hero table is created with the ON DELETE CASCADE πŸŽ‰ -// In SQLite, it also includes REFERENCES team (id), this is needed by SQLite to work with the ON DELETE CASCADE properly. -// SQLAlchemy takes care of setting it up for us to make sure it works πŸ€“ -CREATE TABLE hero ( - id INTEGER NOT NULL, - name VARCHAR NOT NULL, - secret_name VARCHAR NOT NULL, - age INTEGER, - team_id INTEGER, - PRIMARY KEY (id), - FOREIGN KEY(team_id) REFERENCES team (id) ON DELETE CASCADE -) - -// We select the team Wakaland -INFO Engine SELECT team.id, team.name, team.headquarters -FROM team -WHERE team.name = ? -INFO Engine [generated in 0.00014s] ('Wakaland',) - -// Then, because of cascade_delete, right before deleting Wakaland, SQLAlchemy loads the heroes -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age, hero.team_id AS hero_team_id -FROM hero -WHERE ? = hero.team_id -INFO Engine [generated in 0.00020s] (3,) - -// Next, before deleting the Wakaland team, it sends a DELETE statement including each related hero: Black Lion and Princess Sure-E, with IDs 4 and 5 -INFO Engine DELETE FROM hero WHERE hero.id = ? -INFO Engine [generated in 0.00022s] [(4,), (5,)] - -// After that, it will send the delete for the team Wakaland with ID 3 -INFO Engine DELETE FROM team WHERE team.id = ? -INFO Engine [generated in 0.00017s] (3,) - -// Print the deleted team -Deleted team: name='Wakaland' id=3 headquarters='Wakaland Capital City' - -// Finally, we try to select the heroes from Wakaland, Black Lion and Princess Sure-E and print them, but they are now deleted -Black Lion not found: None -Princess Sure-E not found: None -``` - -
- -## `ondelete` with `SET NULL` - -We can configure the database to **set the foreign key** (the `team_id` in the `hero` table) to **`NULL`** when the related record (in the `team` table) is deleted. - -In this case, the side with `Relationship()` won't have `cascade_delete`, but the side with `Field()` and a `foreign_key` will have `ondelete="SET NULL"`. - -{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py ln[1:21] hl[19] *} - -The configuration above is setting the `team_id` column from the `Hero` table to have an `ON DELETE SET NULL`. - -This way, when someone deletes a team from the database using SQL directly, the database will go to the heroes for that team and set `team_id` to `NULL` (if the database supports it). - -/// tip - -The foreign key should allow `None` values (`NULL` in the database), otherwise you would end up having an Integrity Error by violating the `NOT NULL` constraint. - -So `team_id` needs to have a type with `None`, like: - -```Python -team_id: int | None -``` - -/// - -### Not Using `ondelete="SET NULL"` - -What happens if you don't use `ondelete="SET NULL"`, don't set anything on `cascade_delete`, and delete a team? - -The default behavior is that SQLModel (actually SQLAlchemy) will go to the heroes and set their `team_id` to `NULL` from the **Python code**. - -So, **by default**, those `team_id` fields will be **set to `NULL`**. - -But if someone goes to the database and **manually deletes a team**, the heroes could end up with a `team_id` pointing to a non-existing team. - -Adding the `ondelete="SET NULL"` configures the database itself to also set those fields to `NULL`. - -But if you delete a team from code, by default, SQLModel (actually SQLAlchemy) will update those `team_id` fields to `NULL` even before the database `SET NULL` takes effect. - -### Removing a Team with `SET NULL` - -Removing a team has the **same code** as before, the only thing that changes is the configuration underneath in the database. - -{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py ln[78:84] hl[82] *} - -The result would be these tables. - -#### Team Table after `SET NULL` - -| id | name | headquarters | -| ---- | ---------- | --------------------- | -| 1 | Z-Force | Sister Margaret's Bar | -| 2 | Preventers | Sharp Tower | - -#### Hero Table after `SET NULL` - -| id | name | secret_name | age | team_id | -| ---- | --------------- | ---------------- | ---- | ------- | -| 1 | Deadpond | Dive Wilson | | 1 | -| 2 | Rusty-Man | Tommy Sharp | 48 | 2 | -| 3 | Spider-Boy | Pedro Parqueador | | 2 | -| 4 | Black Lion | Trevor Challa | 35 | NULL | -| 5 | Princess Sure-E | Sure-E | | NULL | - -#### Visual Teams and Heroes after `SET NULL` - -We could visualize them like this: - -```mermaid -flowchart TB - subgraph "Z-Force" - d("Deadpond") - end - subgraph "Preventers" - r("Rusty-Man") - s("Spider-Boy") - end - b("Black Lion") - p("Princess Sure-E") -``` - -### Run the program with `SET NULL` - -Let's confirm it all works by running the program now: - -
- -```console -$ python app.py - -// Some boilerplate and previous output omitted πŸ˜‰ - -// The hero table is created with the ON DELETE SET NULL πŸŽ‰ -// In SQLite, it also includes: REFERENCES team (id). This REFERENCES is needed by SQLite to work with the ON DELETE CASCADE properly. -// SQLModel with SQLAlchemy takes care of setting it up for us to make sure it works πŸ€“ -CREATE TABLE hero ( - id INTEGER NOT NULL, - name VARCHAR NOT NULL, - secret_name VARCHAR NOT NULL, - age INTEGER, - team_id INTEGER, - PRIMARY KEY (id), - FOREIGN KEY(team_id) REFERENCES team (id) ON DELETE SET NULL -) - -// We select the team Wakaland -INFO Engine SELECT team.id, team.name, team.headquarters -FROM team -WHERE team.id = ? -INFO Engine [generated in 0.00010s] (3,) -Team Wakaland: id=3 name='Wakaland' headquarters='Wakaland Capital City' - -// Then, right before deleting Wakaland, the heroes are loaded automatically -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age, hero.team_id AS hero_team_id -FROM hero -WHERE ? = hero.team_id -INFO Engine [generated in 0.00020s] (3,) - -// Next, before deleting the Wakaland team, it sends an UPDATE statement including each related hero: Black Lion and Princess Sure-E, with IDs 4 and 5, to set their team_id to NULL. This is not the SET NULL we added, this is just the default SQLModel (SQLAlchemy) behavior. -INFO Engine UPDATE hero SET team_id=? WHERE hero.id = ? -INFO Engine [generated in 0.00009s] [(None, 4), (None, 5)] - -// After that, it will send the delete for the team Wakaland with ID 3 -INFO Engine DELETE FROM team WHERE team.id = ? -INFO Engine [generated in 0.00017s] (3,) - -// Print the deleted team -Deleted team: name='Wakaland' id=3 headquarters='Wakaland Capital City' - -// Finally, we select the heroes Black Lion and Princess Sure-E and print them, they no longer have a team -Black Lion has no team: age=35 id=4 name='Black Lion' secret_name='Trevor Challa' team_id=None -Princess Sure-E has no team: age=None id=5 name='Princess Sure-E' secret_name='Sure-E' team_id=None -``` - -
- -The team `Wakaland` was deleted and all of its heroes were left without a team, or in other words, with their `team_id` set to `NULL`, but still kept in the database! πŸ€“ - -## Let the Database Handle it with `passive_deletes` - -In the previous examples we configured `ondelete` with `CASCADE` and `SET NULL` to configure the database to handle the deletion of related records automatically. But we actually **never used that functionality** ourselves, because SQLModel (SQLAlchemy) **by default loads** the related records and **deletes** them or updates them with **NULL** before sending the `DELETE` for the team. - -If you know your database would be able to correctly handle the deletes or updates on its own, just with `ondelete="CASCADE"` or `ondelete="SET NULL"`, you can use `passive_deletes="all"` in the `Relationship()` to tell SQLModel (actually SQLAlchemy) to **not delete or update** those records (for heroes) before sending the `DELETE` for the team. - -### Enable Foreign Key Support in SQLite - -To be able to test this out with SQLite, we first need to enable foreign key support. - -{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py ln[30:33] hl[33] *} - -/// info - -You can learn more about SQLite, foreign keys, and this SQL command on the SQLAlchemy docs. - -/// - -### Use `passive_deletes="all"` - -Now let's update the table model for `Team` to use `passive_deletes="all"` in the `Relationship()` for heroes. - -We will also use `ondelete="SET NULL"` in the `Hero` model table, in the foreign key `Field()` for the `team_id` to make the database set those fields to `NULL` automatically. - -{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py ln[1:21] hl[9,19] *} - -### Run the Program with `passive_deletes` - -Now, if we run the program, we will see that SQLModel (SQLAlchemy) is no longer loading and updating the heroes, it just sends the `DELETE` for the team. - -
- -```console -$ python app.py - -// Some boilerplate and previous output omitted πŸ˜‰ - -// The hero table is created with the ON DELETE SET NULL as before -CREATE TABLE hero ( - id INTEGER NOT NULL, - name VARCHAR NOT NULL, - secret_name VARCHAR NOT NULL, - age INTEGER, - team_id INTEGER, - PRIMARY KEY (id), - FOREIGN KEY(team_id) REFERENCES team (id) ON DELETE SET NULL -) - -// For SQLite, we also send the custom command to enable foreign key support -INFO Engine PRAGMA foreign_keys=ON - -// We select and print the team Wakaland -Team Wakaland: id=3 name='Wakaland' headquarters='Wakaland Capital City' - -// We won't see another SELECT for the heroes, nor an UPDATE or DELETE. SQLModel (with SQLAlchemy) won't try to load and update (or delete) the related records for heroes, it will just send the DELETE for the team right away. -INFO Engine DELETE FROM team WHERE team.id = ? -INFO Engine [generated in 0.00013s] (3,) - -// At this point, because we enabled foreign key support for SQLite, the database will take care of updating the records for heroes automatically, setting their team_id to NULL - -// Print the deleted team -Deleted team: name='Wakaland' id=3 headquarters='Wakaland Capital City' - -// Finally, we select the heroes Black Lion and Princess Sure-E and print them, they no longer have a team -Black Lion has no team: age=35 id=4 name='Black Lion' secret_name='Trevor Challa' team_id=None -Princess Sure-E has no team: age=None id=5 name='Princess Sure-E' secret_name='Sure-E' team_id=None -``` - -
- -## `ondelete` with `RESTRICT` - -We can also configure the database to **prevent the deletion** of a record (a team) if there are related records (heroes). - -In this case, when someone attempts to **delete a team with heroes** in it, the database will **raise an error**. - -And because this is configured in the database, it will happen even if someone **interacts with the database directly using SQL** (if the database supports it). - -/// tip - -For SQLite, this also needs enabling foreign key support. - -/// - -### Enable Foreign Key Support in SQLite for `RESTRICT` - -As `ondelete="RESTRICT"` is mainly a database-level constraint, let's enable foreign key support in SQLite first to be able to test it. - -{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py ln[30:33] hl[33] *} - -### Use `ondelete="RESTRICT"` - -Let's set `ondelete="RESTRICT"` in the foreign key `Field()` for the `team_id` in the `Hero` model table. - -And in the `Team` model table, we will use `passive_deletes="all"` in the `Relationship()` for heroes, this way the default behavior of setting foreign keys from deleted models to `NULL` will be disabled, and when we try to delete a team with heroes, the database will **raise an error**. - -/// tip - -Notice that we don't set `cascade_delete` in the `Team` model table. - -/// - -{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py ln[1:21] hl[9,19] *} - -### Run the Program with `RESTRICT`, See the Error - -Now, if we run the program and try to delete a team with heroes, we will see an error. - -
- -```console -$ python app.py - -// Some boilerplate and previous output omitted πŸ˜‰ - -// The hero table is created with the ON DELETE RESTRICT -CREATE TABLE hero ( - id INTEGER NOT NULL, - name VARCHAR NOT NULL, - secret_name VARCHAR NOT NULL, - age INTEGER, - team_id INTEGER, - PRIMARY KEY (id), - FOREIGN KEY(team_id) REFERENCES team (id) ON DELETE RESTRICT -) - -// Now, when we reach the point of deleting a team with heroes, we will see an error -Traceback (most recent call last): - File "/home/user/code... - -sqlite3.IntegrityError: FOREIGN KEY constraint failed - -// More error output here... - -sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) FOREIGN KEY constraint failed -[SQL: DELETE FROM team WHERE team.id = ?] -[parameters: (3,)] -``` - -
- -Great! The database didn't let us commit the mistake of deleting a team with heroes. πŸ€“ - -/// tip - -If you want to test if the `PRAGMA foreign_keys=ON` is necessary, **comment that line** and run it again, you will **not see an error**. 😱 - -The same with `passive_deletes="all"`, if you **comment that line**, SQLModel (SQLAlchemy) will load and update the heroes before deleting the team, set their foreign key `team_id` to `NULL` and **the constraint won't work as expected**, you will not see an error. πŸ˜… - -/// - -### Update Heroes Before Deleting the Team - -After having the `ondelete="RESTRICT"` in place, SQLite configured to support foreign keys, and `passive_deletes="all"` in the `Relationship()`, if we try to delete a team with heroes, we will see an error. - -If we want to delete the team, we need to **update the heroes first** and set their `team_id` to `None` (or `NULL` in the database). - -By calling the method `.clear()` from a list, we remove all its items. So, by calling `team.heroes.clear()` and saving that to the database, we disassociate the heroes from the team, that will set their `team_id` to `None`. - -/// tip - -Calling `team.heroes.clear()` is very similar to what SQLModel (actually SQLAlchemy) would have done if we didn't have `passive_deletes="all"` configured. - -/// - -{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py310.py ln[80:88] hl[84] *} - -### Run the Program Deleting Heroes First - -Now, if we run the program and delete the heroes first, we will be able to delete the team without any issues. - -
- -```console -$ python app.py - -// Some boilerplate and previous output omitted πŸ˜‰ - -// The hero table is created with the ON DELETE RESTRICT -CREATE TABLE hero ( - id INTEGER NOT NULL, - name VARCHAR NOT NULL, - secret_name VARCHAR NOT NULL, - age INTEGER, - team_id INTEGER, - PRIMARY KEY (id), - FOREIGN KEY(team_id) REFERENCES team (id) ON DELETE RESTRICT -) - -// We manually disassociate the heroes from the team -INFO Engine UPDATE hero SET team_id=? WHERE hero.id = ? -INFO Engine [generated in 0.00008s] [(None, 4), (None, 5)] - -// We print the team from which we removed heroes -Team with removed heroes: name='Wakaland' id=3 headquarters='Wakaland Capital City' - -// Now we can delete the team -INFO Engine DELETE FROM team WHERE team.id = ? -INFO Engine [generated in 0.00008s] (3,) -INFO Engine COMMIT -Deleted team: name='Wakaland' id=3 headquarters='Wakaland Capital City' - -// The heroes Black Lion and Princess Sure-E are no longer associated with the team -Black Lion has no team: secret_name='Trevor Challa' name='Black Lion' team_id=None age=35 id=4 -Princess Sure-E has no team: secret_name='Sure-E' name='Princess Sure-E' team_id=None age=None id=5 -``` - -
- -## Conclusion - -In many cases, **you don't really need to configure anything**. 😎 - -In some cases, when you want to **cascade** the delete of a record to its related records automatically (delete a team with its heroes), you can: - -* Use `cascade_delete=True` in the `Relationship()` on the side **without a foreign key** -* And use `ondelete="CASCADE"` in the `Field()` with the **foreign key** - -That will **cover most of the use cases**. πŸš€ - -And if you need something else, you can refer the additional options described above. πŸ€“ diff --git a/docs/tutorial/relationship-attributes/create-and-update-relationships.md b/docs/tutorial/relationship-attributes/create-and-update-relationships.md deleted file mode 100644 index d0de56735d..0000000000 --- a/docs/tutorial/relationship-attributes/create-and-update-relationships.md +++ /dev/null @@ -1,81 +0,0 @@ -# Create and Update Relationships - -Let's see now how to create data with relationships using these new **relationship attributes**. ✨ - -## Create Instances with Fields - -Let's check the old code we used to create some heroes and teams: - -{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[29:58] hl[35,38,44,50] *} - -There are several things to **notice** here. - -First, we **create** some `Team` instance objects. We want to use the IDs of these teams when creating the `Hero` instances, in the `team_id` field. - -But model instances **don't have an ID** generated by the database until we `add` and `commit` them to the **session**. Before that, they are just `None`, and we want to use the actual IDs. - -So, we have to `add` them and `commit` the session first, before we start creating the `Hero` instances, to be able to **use their IDs**. - -Then, we use those IDs when creating the `Hero` instances. We `add` the new heroes to the session, and then we `commit` them. - -So, we are **committing twice**. And we have to remember to `add` some things first, and then `commit`, and do all that **in the right order**, otherwise we could end up using a `team.id` that is currently `None` because it hasn't been saved. - -This is the first area where these **relationship attributes** can help. πŸ€“ - -## Create Instances with Relationship Attributes - -Now let's do all that, but this time using the new, shiny `Relationship` attributes: - -{* ./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py ln[32:55] hl[38,41,47] *} - -Now we can create the `Team` instances and pass them directly to the new `team` argument when creating the `Hero` instances, as `team=team_preventers` instead of `team_id=team_preventers.id`. - -And thanks to SQLAlchemy and how it works underneath, these teams don't even need to have an ID yet, but because we are assigning the whole object to each hero, those teams **will be automatically created** in the database, the automatic ID will be generated, and will be set in the `team_id` column for each of the corresponding hero rows. - -In fact, now we don't even have to put the teams explicitly in the session with `session.add(team)`, because these `Team` instances are **already associated** with heroes that **we do** `add` to the session. - -SQLAlchemy knows that it also has to include those teams in the next commit to be able to save the heroes correctly. - -And then, as you can see, we only have to do one `commit()`. - -## Assign a Relationship - -The same way we could assign an integer with a `team.id` to a `hero.team_id`, we can also assign the `Team` instance to the `hero.team`: - -{* ./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py ln[32:33,57:61] hl[57] *} - -## Create a Team with Heroes - -Before, we created some `Team` instances and passed them in the `team=` argument when creating `Hero` instances. - -We could also create the `Hero` instances first, and then pass them in the `heroes=` argument that takes a list, when creating a `Team` instance: - -{* ./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py ln[32:33,63:73] hl[68,70:71] *} - -Here we create two heroes first, **Black Lion** and **Princess Sure-E**, and then we pass them in the `heroes` argument. - -Notice that, the same as before, we only have to `add` the `Team` instance to the session, and because the heroes are connected to it, they will be automatically saved too when we `commit`. - -## Include Relationship Objects in the Many Side - -We said before that this is a **many-to-one** relationship, because there can be **many** heroes that belong to **one** team. - -We can also connect data with these relationship attributes on the **many** side. - -As the attribute `team.heroes` behaves like a list, we can simply append to it. - -Let's create some more heroes and add them to the `team_preventers.heroes` list attribute: - -{* ./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py ln[32:33,75:91] hl[81:85] *} - -The attribute `team_preventers.heroes` behaves like a list. But it's a special type of list, because when we modify it adding heroes to it, **SQLModel** (actually SQLAlchemy) **keeps track of the necessary changes** to be done in the database. - -Then we `add()` the team to the session and `commit()` it. - -And in the same way as before, we don't even have to `add()` the independent heroes to the session, because they are **connected to the team**. - -## Recap - -We can use common Python objects and attributes to create and update data connections with these **relationship attributes**. 😎 - -Next we'll see how to use these relationship attributes to read connected data. 🀝 diff --git a/docs/tutorial/relationship-attributes/define-relationships-attributes.md b/docs/tutorial/relationship-attributes/define-relationships-attributes.md deleted file mode 100644 index c5307d3663..0000000000 --- a/docs/tutorial/relationship-attributes/define-relationships-attributes.md +++ /dev/null @@ -1,97 +0,0 @@ -# Define Relationships Attributes - -Now we are finally in one of the most exciting parts of **SQLModel**. - -Relationship Attributes. ✨ - -We currently have a `team` table: - - - - - - - - - - - -
idnameheadquarters
1PreventersSharp Tower
2Z-ForceSister Margaret's Bar
- -And a `hero` table: - - - - - - - - - - - - - - -
idnamesecret_nameageteam_id
1DeadpondDive Wilsonnull2
2Rusty-ManTommy Sharp481
3Spider-BoyPedro Parqueadornull1
- -Now that you know how these tables work underneath and how the model classes represent them, it's time to add a little convenience that will make many operations in code simpler. - -## Declare Relationship Attributes - -Up to now, we have only used the `team_id` column to connect the tables when querying with `select()`: - -{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[1:16] hl[16] *} - -This is a **plain field** like all the others, all representing a **column in the table**. - -But now let's add a couple of new special attributes to these model classes, let's add `Relationship` attributes. - -First, import `Relationship` from `sqlmodel`: - -{* ./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py ln[1] hl[1] *} - -Next, use that `Relationship` to declare a new attribute in the model classes: - -{* ./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py ln[1:19] hl[9,19] *} - -## What Are These Relationship Attributes - -These new attributes are not the same as fields, they **don't represent a column** directly in the database, and their value is not a singular value like an integer. Their value is the actual **entire object** that is related. - -So, in the case of a `Hero` instance, if you call `hero.team`, you will get the entire `Team` instance object that this hero belongs to. ✨ - -For example, you could check if a `hero` belongs to any `team` (if `.team` is not `None`) and then print the team's `name`: - -```Python -if hero.team: - print(hero.team.name) -``` - -## Relationship Attributes or `None` - -Notice that in the `Hero` class, the type annotation for `team` is `Team | None`. - -This means that this attribute could be `None`, or it could be a full `Team` object. - -This is because the related **`team_id` could also be `None`** (or `NULL` in the database). - -If it was required for a `Hero` instance to belong to a `Team`, then the `team_id` would be `int` instead of `int | None`, its `Field` would be `Field(foreign_key="team.id")` instead of `Field(default=None, foreign_key="team.id")` and the `team` attribute would be a `Team` instead of `Team | None`. - -## Relationship Attributes With Lists - -And in the `Team` class, the `heroes` attribute is annotated as a list of `Hero` objects, because that's what it will have. - -**SQLModel** (actually SQLAlchemy) is smart enough to know that the relationship is established by the `team_id`, as that's the foreign key that points from the `hero` table to the `team` table, so we don't have to specify that explicitly here. - -/// tip - -There's a couple of things we'll check again in some of the next chapters, about the `list["Hero"]` and the `back_populates`. - -But for now, let's first see how to use these relationship attributes. - -/// - -## Next Steps - -Now let's see some real examples of how to use these new **relationship attributes** in the next chapters. ✨ diff --git a/docs/tutorial/relationship-attributes/index.md b/docs/tutorial/relationship-attributes/index.md deleted file mode 100644 index f63b2669e3..0000000000 --- a/docs/tutorial/relationship-attributes/index.md +++ /dev/null @@ -1,17 +0,0 @@ -# Relationship Attributes - Intro - -In the previous chapters we discussed how to manage databases with tables that have **relationships** by using fields (columns) with **foreign keys** pointing to other columns. - -And then we read the data together with `select()` and using `.where()` or `.join()` to connect it. - -Now we will see how to use **Relationship Attributes**, an extra feature of **SQLModel** (and SQLAlchemy), to work with the data in the database in a much more familiar way, and closer to normal Python code. - -/// info - -When I say "**relationship**" I mean the standard dictionary term, of data related to other data. - -I'm not using the term "**relation**" that is the technical, academical, SQL term for a single table. - -/// - -And using those **relationship attributes** is where a tool like **SQLModel** really shines. ✨ diff --git a/docs/tutorial/relationship-attributes/read-relationships.md b/docs/tutorial/relationship-attributes/read-relationships.md deleted file mode 100644 index a59ec8351e..0000000000 --- a/docs/tutorial/relationship-attributes/read-relationships.md +++ /dev/null @@ -1,68 +0,0 @@ -# Read Relationships - -Now that we know how to connect data using **relationship Attributes**, let's see how to get and read the objects from a relationship. - -## Select a Hero - -First, add a function `select_heroes()` where we get a hero to start working with, and add that function to the `main()` function: - -{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py ln[94:98,108:111] hl[94:98,111] *} - -## Select the Related Team - Old Way - -Now that we have a hero, we can get the team this hero belongs to. - -With what we have learned **up to now**, we could use a `select()` statement, then execute it with `session.exec()`, and then get the `.first()` result, for example: - -{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py ln[94:103] hl[100:103] *} - -## Get Relationship Team - New Way - -But now that we have the **relationship attributes**, we can just access them, and **SQLModel** (actually SQLAlchemy) will go and fetch the corresponding data from the database, and make it available in the attribute. ✨ - -So, the highlighted block above, has the same results as the block below: - -{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py ln[94:98,105] hl[105] *} - -/// tip - -The automatic data fetching will work as long as the starting object (in this case the `Hero`) is associated with an **open** session. - -For example, here, **inside** a `with` block with a `Session` object. - -/// - -## Get a List of Relationship Objects - -And the same way, when we are working on the **many** side of the **one-to-many** relationship, we can get a list of of the related objects just by accessing the relationship attribute: - -{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py ln[94:100] hl[100] *} - -That would print a list with all the heroes in the Preventers team: - -
- -```console -$ python app.py - -// Automatically fetch the heroes -INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age, hero.team_id AS hero_team_id -FROM hero -WHERE ? = hero.team_id -INFO Engine [cached since 0.8774s ago] (2,) - -// Print the list of Preventers -Preventers heroes: [ - Hero(name='Rusty-Man', age=48, id=2, secret_name='Tommy Sharp', team_id=2), - Hero(name='Spider-Boy', age=None, id=3, secret_name='Pedro Parqueador', team_id=2), - Hero(name='Tarantula', age=32, id=6, secret_name='Natalia Roman-on', team_id=2), - Hero(name='Dr. Weird', age=36, id=7, secret_name='Steve Weird', team_id=2), - Hero(name='Captain North America', age=93, id=8, secret_name='Esteban Rogelios', team_id=2) -] -``` - -
- -## Recap - -With **relationship attributes** you can use the power of common Python objects to easily access related data from the database. 😎 diff --git a/docs/tutorial/relationship-attributes/remove-relationships.md b/docs/tutorial/relationship-attributes/remove-relationships.md deleted file mode 100644 index ff408e20b9..0000000000 --- a/docs/tutorial/relationship-attributes/remove-relationships.md +++ /dev/null @@ -1,21 +0,0 @@ -# Remove Relationships - -Now let's say that **Spider-Boy** tells **Rusty-Man** something like: - -> I don't feel so good Mr. Sharp - -And then for some reason needs to leave the **Preventers** for some years. 😭 - -We can remove the relationship by setting it to `None`, the same as with the `team_id`, it also works with the new relationship attribute `.team`: - -{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py ln[103:114] hl[109] *} - -And of course, we should remember to add this `update_heroes()` function to `main()` so that it runs when we call this program from the command line: - -{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py ln[117:121] hl[121] *} - -## Recap - -This chapter was too short for a recap, wasn't it? πŸ€” - -Anyway, **relationship attributes** make it easy and intuitive to work with relationships stored in the database. πŸŽ‰ diff --git a/docs/tutorial/relationship-attributes/type-annotation-strings.md b/docs/tutorial/relationship-attributes/type-annotation-strings.md deleted file mode 100644 index 74b97f08d1..0000000000 --- a/docs/tutorial/relationship-attributes/type-annotation-strings.md +++ /dev/null @@ -1,23 +0,0 @@ -## About the String in `list["Hero"]` - -In the first Relationship attribute, we declare it with `list["Hero"]`, putting the `Hero` in quotes instead of just normally there: - -{* ./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py ln[1:19] hl[9] *} - -What's that about? Can't we just write it normally as `list[Hero]`? - -By that point, in that line in the code, the Python interpreter **doesn't know of any class `Hero`**, and if we put it just there, it would try to find it unsuccessfully, and then fail. 😭 - -But by putting it in quotes, in a string, the interpreter sees it as just a string with the text `"Hero"` inside. - -But the editor and other tools can see that **the string is actually a type annotation inside**, and provide all the autocompletion, type checks, etc. πŸŽ‰ - -And of course, **SQLModel** can also understand it in the string correctly. ✨ - -That is actually part of Python, it's the current official solution to handle it. - -/// info - -There's a lot of work going on in Python itself to make that simpler and more intuitive, and find ways to make it possible to not wrap the class in a string. - -/// diff --git a/docs/tutorial/select.md b/docs/tutorial/select.md deleted file mode 100644 index cd996932c6..0000000000 --- a/docs/tutorial/select.md +++ /dev/null @@ -1,422 +0,0 @@ -# Read Data - SELECT - -We already have a database and a table with some data in it that looks more or less like this: - - - - - - - - - - - - - - -
idnamesecret_nameage
1DeadpondDive Wilsonnull
2Spider-BoyPedro Parqueadornull
3Rusty-ManTommy Sharp48
- -Things are getting more exciting! Let's now see how to read data from the database! 🀩 - -## Continue From Previous Code - -Let's continue from the last code we used to create some data. - -{* ./docs_src/tutorial/insert/tutorial002_py310.py ln[0] *} - -We are creating a **SQLModel** `Hero` class model and creating some records. - -We will need the `Hero` model and the **engine**, but we will create a new session to query data in a new function. - -## Read Data with SQL - -Before writing Python code let's do a quick review of how querying data with SQL looks like: - -```SQL -SELECT id, name, secret_name, age -FROM hero -``` - -It means, more or less: - -> Hey SQL database πŸ‘‹, please go and `SELECT` some data for me. -> -> I'll first tell you the columns I want: -> -> * `id` -> * `name` -> * `secret_name` -> * `age` -> -> And I want you to get them `FROM` the table called `"hero"`. - -Then the database will go and get the data and return it to you in a table like this: - - - - - - - - - - - - - - -
idnamesecret_nameage
1DeadpondDive Wilsonnull
2Spider-BoyPedro Parqueadornull
3Rusty-ManTommy Sharp48
- -You can try that out in **DB Browser for SQLite**: - - - -/// warning - -Here we are getting all the rows. - -If you have thousands of rows, that could be expensive to compute for the database. - -You would normally want to filter the rows to receive only the ones you want. But we'll learn about that later in the next chapter. - -/// - -### A SQL Shortcut - -If we want to get all the columns like in this case above, in SQL there's a shortcut, instead of specifying each of the column names we could write a `*`: - -```SQL -SELECT * -FROM hero -``` - -That would end up in the same result. Although we won't use that for **SQLModel**. - -### `SELECT` Fewer Columns - -We can also `SELECT` fewer columns, for example: - -```SQL -SELECT id, name -FROM hero -``` - -Here we are only selecting the `id` and `name` columns. - -And it would result in a table like this: - - - - - - - - - - - - - - -
idname
1Deadpond
2Spider-Boy
3Rusty-Man
- -And here is something interesting to notice. SQL databases store their data in tables. And they also always communicate their results in **tables**. - -### `SELECT` Variants - -The SQL language allows several **variations** in several places. - -One of those variations is that in `SELECT` statements you can use the names of the columns directly, or you can prefix them with the name of the table and a dot. - -For example, the same SQL code above could be written as: - -```SQL -SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -``` - -This will be particularly important later when working with multiple tables at the same time that could have the same name for some columns. - -For example `hero.id` and `team.id`, or `hero.name` and `team.name`. - -Another variation is that most of the SQL keywords like `SELECT` can also be written in lowercase, like `select`. - -### Result Tables Don't Have to Exist - -This is the interesting part. The tables returned by SQL databases **don't have to exist** in the database as independent tables. πŸ§™ - -For example, in our database, we only have one table that has all the columns, `id`, `name`, `secret_name`, `age`. And here we are getting a result table with fewer columns. - -One of the main points of SQL is to be able to keep the data structured in different tables, without repeating data, etc, and then query the database in many ways and get many different tables as a result. - -## Read Data with **SQLModel** - -Now let's do the same query to read all the heroes, but with **SQLModel**. - -## Create a **Session** - -The first step is to create a **Session**, the same way we did when creating the rows. - -We will start with that in a new function `select_heroes()`: - -{* ./docs_src/tutorial/select/tutorial001_py310.py ln[34:35] hl[34:35] *} - -## Create a `select` Statement - -Next, pretty much the same way we wrote a SQL `SELECT` statement above, now we'll create a **SQLModel** `select` statement. - -First we have to import `select` from `sqlmodel` at the top of the file: - -{* ./docs_src/tutorial/select/tutorial001_py310.py ln[1] hl[1] *} - -And then we will use it to create a `SELECT` statement in Python code: - -{* ./docs_src/tutorial/select/tutorial001_py310.py ln[1,34:36] hl[36] *} - -It's a very simple line of code that conveys a lot of information: - -```Python -statement = select(Hero) -``` - -This is equivalent to the first SQL `SELECT` statement above: - -```SQL -SELECT id, name, secret_name, age -FROM hero -``` - -We pass the class model `Hero` to the `select()` function. And that tells it that we want to select all the columns necessary for the `Hero` class. - -And notice that in the `select()` function we don't explicitly specify the `FROM` part. It is already obvious to **SQLModel** (actually to SQLAlchemy) that we want to select `FROM` the table `hero`, because that's the one associated with the `Hero` class model. - -/// tip - -The value of the `statement` returned by `select()` is a special object that allows us to do other things. - -I'll tell you about that in the next chapters. - -/// - -## Execute the Statement - -Now that we have the `select` statement, we can execute it with the **session**: - -{* ./docs_src/tutorial/select/tutorial001_py310.py ln[34:37] hl[37] *} - -This will tell the **session** to go ahead and use the **engine** to execute that `SELECT` statement in the database and bring the results back. - -Because we created the **engine** with `echo=True`, it will show the SQL it executes in the output. - -This `session.exec(statement)` will generate this 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] () -``` - -The database returns the table with all the data, just like above when we wrote SQL directly: - - - - - - - - - - - - - - -
idnamesecret_nameage
1DeadpondDive Wilsonnull
2Spider-BoyPedro Parqueadornull
3Rusty-ManTommy Sharp48
- -## Iterate Through the Results - -The `results` object is an iterable that can be used to go through each one of the rows. - -Now we can put it in a `for` loop and print each one of the heroes: - -{* ./docs_src/tutorial/select/tutorial001_py310.py ln[34:39] hl[38:39] *} - -This will print the 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' -``` - -## Add `select_heroes()` to `main()` - -Now include a call to `select_heroes()` in the `main()` function so that it is executed when we run the program from the command line: - -{* ./docs_src/tutorial/select/tutorial001_py310.py ln[34:45] hl[45] *} - -## Review The Code - -Great, you're now being able to read the data from the database! πŸŽ‰ - -Let's review the code up to this point: - -//// tab | Python 3.10+ - -```{ .python .annotate } -{!./docs_src/tutorial/select/tutorial002_py310.py!} -``` - -{!./docs_src/tutorial/select/annotations/en/tutorial002.md!} - -//// - -//// tab | Python 3.8+ - -```{ .python .annotate } -{!./docs_src/tutorial/select/tutorial002.py!} -``` - -{!./docs_src/tutorial/select/annotations/en/tutorial002.md!} - -//// - -/// tip - -Check out the number bubbles to see what is done by each line of code. - -/// - -Here it starts to become more evident why we should have a single **engine** for the whole application, but different **sessions** for each group of operations. - -This new session we created uses the *same* **engine**, but it's a new and independent **session**. - -The code above creating the models could, for example, live in a function handling web API requests and creating models. - -And the second section reading data from the database could be in another function for other requests. - -So, both sections could be in **different places** and would need their own sessions. - -/// info - -To be fair, in this example all that code could actually share the same **session**, there's actually no need to have two here. - -But it allows me to show you how they could be separated and to reinforce the idea that you should have **one engine** per application, and **multiple sessions**, one per each group of operations. - -/// - -## Get a List of `Hero` Objects - -Up to now we are using the `results` to iterate over them. - -But for different reasons you might want to have the full **list of `Hero`** objects right away instead of just an *iterable*. For example, if you want to return them in a web API. - -The special `results` object also has a method `results.all()` that returns a list with all the objects: - -{* ./docs_src/tutorial/select/tutorial003_py310.py ln[34:39] hl[38] *} - -With this now we have all the heroes in a list in the `heroes` variable. - -After printing it, we would see something like: - -``` -[ - Hero(id=1, name='Deadpond', age=None, secret_name='Dive Wilson'), - Hero(id=2, name='Spider-Boy', age=None, secret_name='Pedro Parqueador'), - Hero(id=3, name='Rusty-Man', age=48, secret_name='Tommy Sharp') -] -``` - -/// info - -It would actually look more compact, I'm formatting it a bit for you to see that it is actually a list with all the data. - -/// - -## Compact Version - -I have been creating several variables to be able to explain to you what each thing is doing. - -But knowing what is each object and what it is all doing, we can simplify it a bit and put it in a more compact form: - -{* ./docs_src/tutorial/select/tutorial004_py310.py ln[34:37] hl[36] *} - -Here we are putting it all on a single line, you will probably put the select statements in a single line like this more often. - -## SQLModel or SQLAlchemy - Technical Details - -**SQLModel** is actually, more or less, just **SQLAlchemy** and **Pydantic** underneath, combined together. - -It uses and returns the same types of objects and is compatible with both libraries. - -Nevertheless, **SQLModel** defines a few of its own internal parts to improve the developer experience. - -In this chapter we are touching some of them. - -### SQLModel's `select` - -When importing from `sqlmodel` the `select()` function, you are using **SQLModel**'s version of `select`. - -SQLAchemy also has its own `select`, and SQLModel's `select` uses SQLAlchemy's `select` internally. - -But SQLModel's version does a lot of **tricks** with type annotations to make sure you get the best **editor support** possible, no matter if you use **VS Code**, **PyCharm**, or something else. ✨ - -/// info - -There was a lot of work and research, with different versions of the internal code, to improve this as much as possible. πŸ€“ - -/// - -### SQLModel's `session.exec` - -πŸ“’ This is one to pay special attention to. - -SQLAlchemy's own `Session` has a method `session.execute()`. It doesn't have a `session.exec()` method. - -If you see SQLAlchemy tutorials, they will always use `session.execute()`. - -**SQLModel**'s own `Session` inherits directly from SQLAlchemy's `Session`, and adds this additional method `session.exec()`. Underneath, it uses the same `session.execute()`. - -But `session.exec()` does several **tricks** combined with the tricks in `session()` to give you the **best editor support**, with **autocompletion** and **inline errors** everywhere, even after getting data from a select. ✨ - -For example, in SQLAlchemy you would need to add a `.scalars()` here: - -```Python -heroes = session.execute(select(Hero)).scalars().all() -``` - -But you would have to remove it when selecting multiple things (we'll see that later). - -SQLModel's `session.exec()` takes care of that for you, so you don't have to add the `.scalars()`. - -This is something that SQLAlchemy currently can't provide, because the regular `session.execute()` supports several other use cases, including legacy ones, so it can't have all the internal type annotations and tricks to support this. - -On top of that, **SQLModel**'s `session.exec()` also does some tricks to reduce the amount of code you have to write and to make it as intuitive as possible. - -But SQLModel's `Session` still has access to `session.execute()` too. - -/// tip - -Your editor will give you autocompletion for both `session.exec()` and `session.execute()`. - -πŸ“’ Remember to **always use `session.exec()`** to get the best editor support and developer experience. - -/// - -### Caveats of **SQLModel** Flavor - -SQLModel is designed to have the best **developer experience** in a narrow set of **very common use cases**. ✨ - -You can still combine it with SQLAlchemy directly and use **all the features** of SQLAlchemy when you need to, including lower level more "pure" SQL constructs, exotic patterns, and even legacy ones. πŸ€“ - -But **SQLModel**'s design (e.g. type annotations) assume you are using it in the ways I explain here in the documentation. - -Thanks to this, you will get as much **autocompletion** and **inline errors** as possible. πŸš€ - -But this also means that if you use SQLModel with some more **exotic patterns** from SQLAlchemy, your editor might tell you that *there's an error*, while in fact, the code would still work. - -That's the trade-off. 🀷 - -But for the situations where you need those exotic patterns, you can always use SQLAlchemy directly combined with SQLModel (using the same models, etc). diff --git a/docs/tutorial/update.md b/docs/tutorial/update.md deleted file mode 100644 index 8e60536815..0000000000 --- a/docs/tutorial/update.md +++ /dev/null @@ -1,319 +0,0 @@ -# Update Data - UPDATE - -Now let's see how to update data using **SQLModel**. - -## Continue From Previous Code - -As before, we'll continue from where we left off with the previous code. - -{* ./docs_src/tutorial/indexes/tutorial002_py310.py ln[0] *} - -Remember to remove the `database.db` file before running the examples to get the same results. - -## Update with SQL - -Let's quickly check how to update data with SQL: - -```SQL hl_lines="1-2" -UPDATE hero -SET age=16 -WHERE name = "Spider-Boy" -``` - -This means, more or less: - -> Hey SQL database πŸ‘‹, I want to `UPDATE` the table called `hero`. -> -> Please `SET` the value of the `age` column to `16`... -> -> ...for each of the rows `WHERE` the value of the column `name` is equal to `"Spider-Boy"`. - -In a similar way to `SELECT` statements, the first part defines the columns to work with: what are the columns that have to be updated and to which value. The rest of the columns stay as they were. - -And the second part, with the `WHERE`, defines to which rows it should apply that update. - -In this case, as we only have one hero with the name `"Spider-Boy"`, it will only apply the update in that row. - -/// info - -Notice that in the `UPDATE` the single equals sign (`=`) means **assignment**, setting a column to some value. - -And in the `WHERE` the same single equals sign (`=`) is used for **comparison** between two values, to find rows that match. - -This is in contrast to Python and most programming languages, where a single equals sign (`=`) is used for assignment, and two equal signs (`==`) are used for comparisons. - -/// - -You can try that in **DB Browser for SQLite**: - - - -After that update, the data in the table will look like this, with the new age for Spider-Boy: - - - - - - - - - - - - - - -
idnamesecret_nameage
1DeadpondDive Wilsonnull
2Spider-BoyPedro Parqueador16 ✨
3Rusty-ManTommy Sharp48
- -/// tip - -It will probably be more common to find the row to update by `id`, for example: - -```SQL -UPDATE hero -SET age=16 -WHERE id = 2 -``` - -But in the example above I used `name` to make it more intuitive. - -/// - -Now let's do the same update in code, with **SQLModel**. - -To get the same results, delete the `database.db` file before running the examples. - -## Read From the Database - -We'll start by selecting the hero `"Spider-Boy"`, this is the one we will update: - -{* ./docs_src/tutorial/update/tutorial001_py310.py ln[42:47] hl[44] *} - -Let's not forget to add that `update_heroes()` function to the `main()` function so that we call it when executing the program from the command line: - -{* ./docs_src/tutorial/update/tutorial001_py310.py ln[56:63] hl[59] *} - -Up to that point, running that in the command line will output: - -
- -```console -$ python app.py - -// Some boilerplate and previous output omitted πŸ˜‰ - -// The SELECT with WHERE -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.name = ? -INFO Engine [no key 0.00017s] ('Spider-Boy',) - -// Print the hero as obtained from the database -Hero: name='Spider-Boy' secret_name='Pedro Parqueador' age=None id=2 -``` - -
- -/// tip - -Notice that by this point, the hero still doesn't have an age. - -/// - -## Set a Field Value - -Now that you have a `hero` object, you can simply set the value of the field (the attribute representing a column) that you want. - -In this case, we will set the `age` to `16`: - -{* ./docs_src/tutorial/update/tutorial001_py310.py ln[42:49] hl[49] *} - -## Add the Hero to the Session - -Now that the hero object in memory has a change, in this case a new value for the `age`, we need to add it to the session. - -This is the same we did when creating new hero instances: - -{* ./docs_src/tutorial/update/tutorial001_py310.py ln[42:50] hl[50] *} - -## Commit the Session - -To save the current changes in the session, **commit** it. - -This will save the updated hero in the database: - -{* ./docs_src/tutorial/update/tutorial001_py310.py ln[42:51] hl[51] *} - -It will also save anything else that was added to the session. - -For example, if you were also creating new heroes and had added those objects to the session before, they would now be saved too in this single commit. - -This commit will generate this output: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// Previous output omitted πŸ™ˆ - -// The SQL to update the hero in the database -INFO Engine UPDATE hero SET age=? WHERE hero.id = ? -INFO Engine [generated in 0.00017s] (16, 2) -INFO Engine COMMIT -``` - -
- -## Refresh the Object - -At this point, the hero is updated in the database and it has the new data saved there. - -The data in the object would be automatically refreshed if we accessed an attribute, like `hero.name`. - -But in this example we are not accessing any attribute, we will only print the object. And we also want to be explicit, so we will `.refresh()` the object directly: - -{* ./docs_src/tutorial/update/tutorial001_py310.py ln[42:52] hl[52] *} - -This refresh will trigger the same SQL query that would be automatically triggered by accessing an attribute. So it will generate this output: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// Previous output omitted πŸ™ˆ - -// The SQL to SELECT the fresh hero data -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.id = ? -INFO Engine [generated in 0.00018s] (2,) -``` - -
- -## Print the Updated Object - -Now we can just print the hero: - -{* ./docs_src/tutorial/update/tutorial001_py310.py ln[42:53] hl[53] *} - -Because we refreshed it right after updating it, it has fresh data, including the new `age` we just updated. - -So, printing it will show the new `age`: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// Previous output omitted πŸ™ˆ - -// Print the hero with the new age -Updated hero: name='Spider-Boy' secret_name='Pedro Parqueador' age=16 id=2 -``` - -
- -## Review the Code - -Now let's review all that code: - -//// tab | Python 3.10+ - -```{ .python .annotate hl_lines="42-53" } -{!./docs_src/tutorial/update/tutorial002_py310.py!} -``` - -{!./docs_src/tutorial/update/annotations/en/tutorial002.md!} - -//// - -//// tab | Python 3.8+ - -```{ .python .annotate hl_lines="44-55" } -{!./docs_src/tutorial/update/tutorial002.py!} -``` - -{!./docs_src/tutorial/update/annotations/en/tutorial002.md!} - -//// - -/// tip - -Check out the number bubbles to see what is done by each line of code. - -/// - -## Multiple Updates - -The update process with **SQLModel** is more or less the same as with creating new objects, you add them to the session, and then commit them. - -This also means that you can update several fields (attributes, columns) at once, and you can also update several objects (heroes) at once: - -//// tab | Python 3.10+ - -```{ .python .annotate hl_lines="15-17 19-21 23" } -# Code above omitted πŸ‘† - -{!./docs_src/tutorial/update/tutorial004_py310.py[ln:42-68]!} - -# Code below omitted πŸ‘‡ -``` - -{!./docs_src/tutorial/update/annotations/en/tutorial004.md!} - -//// - -//// tab | Python 3.8+ - -```{ .python .annotate hl_lines="15-17 19-21 23" } -# Code above omitted πŸ‘† - -{!./docs_src/tutorial/update/tutorial004.py[ln:44-70]!} - -# Code below omitted πŸ‘‡ -``` - -{!./docs_src/tutorial/update/annotations/en/tutorial004.md!} - -//// - -/// details | πŸ‘€ Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/update/tutorial004_py310.py!} -``` - -//// - -//// tab | Python 3.8+ - -```Python -{!./docs_src/tutorial/update/tutorial004.py!} -``` - -//// - -/// - -/// tip - -Review what each line does by clicking each number bubble in the code. πŸ‘† - -/// - -## Recap - -Update **SQLModel** objects just as you would with other Python objects. 🐍 - -Just remember to `add` them to a **session**, and then `commit` it. And if necessary, `refresh` them. diff --git a/docs/tutorial/where.md b/docs/tutorial/where.md deleted file mode 100644 index b6d08e72fa..0000000000 --- a/docs/tutorial/where.md +++ /dev/null @@ -1,741 +0,0 @@ -# Filter Data - WHERE - -In the previous chapter we saw how to `SELECT` data from the database. - -We did it using pure **SQL** and using **SQLModel**. - -But we always got all the rows, the whole table: - - - - - - - - - - - - - - -
idnamesecret_nameage
1DeadpondDive Wilsonnull
2Spider-BoyPedro Parqueadornull
3Rusty-ManTommy Sharp48
- -In most of the cases we will want to get only one row, or only a group of rows. - -We will see how to do that now, to filter data and get only the rows **where** a condition is true. - -## Continue From Previous Code - -We'll continue with the same examples we have been using in the previous chapters to create and select data. - -And now we will update `select_heroes()` to filter the data. - -{* ./docs_src/tutorial/select/tutorial001_py310.py ln[0] *} - -If you already executed the previous examples and have a database with data, **remove the database file** before running each example, that way you won't have duplicate data and you will be able to get the same results. - -## Filter Data with SQL - -Let's check first how to filter data with **SQL** using the `WHERE` keyword. - -```SQL hl_lines="3" -SELECT id, name, secret_name, age -FROM hero -WHERE name = "Deadpond" -``` - -The first part means the same as before: - -> Hey SQL database πŸ‘‹, please go and `SELECT` some data for me. -> -> I'll first tell you the columns I want: -> -> * `id` -> * `name` -> * `secret_name` -> * `age` -> -> And I want you to get them `FROM` the table called `"hero"`. - -Then the `WHERE` keyword adds the following: - -> So, SQL database, I already told you what columns to `SELECT` and where to select them `FROM`. -> But I don't want you to bring me all the rows, I only want the rows `WHERE` the `name` column has a value of `"Deadpond"`. - -Then the database will bring a table like this: - - - - - - - - -
idnamesecret_nameage
1DeadpondDive Wilsonnull
- -/// tip - -Even if the result is only one row, the database always returns a **table**. - -In this case, a table with only one row. - -/// - -You can try that out in **DB Browser for SQLite**: - - - -### `WHERE` and `FROM` are "clauses" - -These additional keywords with some sections like `WHERE` and `FROM` that go after `SELECT` (or others) have a technical name, they are called **clauses**. - -There are others clauses too, with their own SQL keywords. - -I won't use the term **clause** too much here, but it's good for you to know it as it will probably show up in other tutorials you could study later. πŸ€“ - -## `SELECT` and `WHERE` - -Here's a quick tip that helps me think about it. - -* **`SELECT`** is used to tell the SQL database what **columns** to return. -* **`WHERE`** is used to tell the SQL database what **rows** to return. - -The size of the table in the two dimensions depend mostly on those two keywords. - -### `SELECT` Land - -If the table has too many or too few **columns**, that's changed in the **`SELECT`** part. - -Starting with some table: - - - - - - - - - - - - - - -
idnamesecret_nameage
1DeadpondDive Wilsonnull
2Spider-BoyPedro Parqueadornull
3Rusty-ManTommy Sharp48
- -...and changing the number of **columns**: - - - - - - - - - - - - - - -
name
Deadpond
Spider-Boy
Rusty-Man
- -...is all `SELECT` land. - -### `WHERE` Land - -If the table has too many or too few **rows**, that's changed in the **`WHERE`** part. - -Starting with some table: - - - - - - - - - - - - - - -
idnamesecret_nameage
1DeadpondDive Wilsonnull
2Spider-BoyPedro Parqueadornull
3Rusty-ManTommy Sharp48
- -...and changing the number of **rows**: - - - - - - - - -
idnamesecret_nameage
2Spider-BoyPedro Parqueadornull
- -...is all `WHERE` land. - -## Review `SELECT` with **SQLModel** - -Let's review some of the code we used to read data with **SQLModel**. - -We care specially about the **select** statement: - -{* ./docs_src/tutorial/select/tutorial001_py310.py ln[34:39] hl[36] *} - -## Filter Rows Using `WHERE` with **SQLModel** - -Now, the same way that we add `WHERE` to a SQL statement to filter rows, we can add a `.where()` to a **SQLModel** `select()` statement to filter rows, which will filter the objects returned: - -{* ./docs_src/tutorial/where/tutorial001_py310.py ln[34:39] hl[36] *} - -It's a very small change, but it's packed of details. Let's explore them. - -## `select()` Objects - -The object returned by `select(Hero)` is a special type of object with some methods. - -One of those methods is `.where()` used to (unsurprisingly) add a `WHERE` to the SQL statement in that **select** object. - -There are other methods that we will explore later. πŸ’‘ - -Most of these methods return the same object again after modifying it. - -So we could call one after the other: - -```Python -statement = select(Hero).where(Hero.name == "Deadpond").where(Hero.age == 48) -``` - -## Calling `.where()` - -Now, this `.where()` method is special and very powerful. It is tightly integrated with **SQLModel** (actually SQLAlchemy) to let you use very familiar Python syntax and code. - -Notice that we didn't call it with a single equal (`=`) sign, and with something like: - -```Python -# Not supported 🚨 -select(Hero).where(name="Deadpond") -``` - -That would have been shorter, of course, but it would have been much more error prone and limited. I'll show you why in a bit. - -Instead, we used two `==`: - -```Python -select(Hero).where(Hero.name == "Deadpond") -``` - -So, what's happening there? - -## `.where()` and Expressions - -In the example above we are using two equal signs (`==`). That's called the "**equality operator**". - -/// tip - -An **operator** is just a symbol that is put beside one value or in the middle of two values to do something with them. - -`==` is called the **equality** operator because it checks if two things are **equal**. - -/// - -When writing Python, if you write something using this equality operator (`==`) like: - -```Python -some_name == "Deadpond" -``` - -...that's called an equality "**comparison**", and it normally results in a value of: - -```Python -True -``` - -...or - -```Python -False -``` - -/// tip - -`<`, `>`, `==`, `>=`, `<=`, and `!=` are all **operators** used for **comparisons**. - -/// - -But SQLAlchemy adds some magic to the columns/fields in a **model class** to make those Python comparisons have super powers. - -So, if you write something like: - -```Python -Hero.name == "Deadpond" -``` - -...that doesn't result in a value of `True` or `False`. 🀯 - -Instead, it results in a special type of object. If you tried that in an interactive Python session, you'd see something like: - -```Python ->>> Hero.name == "Deadpond" - -``` - -So, that result value is an **expression** object. πŸ’‘ - -And `.where()` takes one (or more) of these **expression** objects to update the SQL statement. - -## Model Class Attributes, Expressions, and Instances - -Now, let's stop for a second to make a clear distinction that is very important and easy to miss. - -**Model class attributes** for each of the columns/fields are special and can be used for expressions. - -But that's only for the **model class attributes**. 🚨 - -**Instance** attributes behave like normal Python values. βœ… - -So, using the class (`Hero`, with capital `H`) in a Python comparison: - -```Python -Hero.name == "Deadpond" -``` - -...results in one of those **expression** objects to be used with `.where()`: - -```Python - -``` - -But if you take an instance: - -```Python -some_hero = Hero(name="Deadpond", secret_name="Dive Wilson") -``` - -...and use it in a comparison: - -```Python -some_hero.name == "Deadpond" -``` - -...that results in a Python value of: - -```Python -True -``` - -...or if it was a different object with a different name, it could have been: - -```Python -False -``` - -The difference is that one is using the **model class**, the other is using an **instance**. - -## Class or Instance - -It's quite probable that you will end up having some variable `hero` (with lowercase `h`) like: - -```Python -hero = Hero(name="Deadpond", secret_name="Dive Wilson") -``` - -And now the class is `Hero` (with capital `H`) and the instance is `hero` (with a lowercase `h`). - -So now you have `Hero.name` and `hero.name` that look very similar, but are two different things: - -```Python ->>> Hero.name == "Deadpond" - - ->>> hero.name == "Deadpond" -True -``` - -It's just something to pay attention to. πŸ€“ - -But after understanding that difference between classes and instances it can feel natural, and you can do very powerful things. πŸš€ - -For example, as `hero.name` works like a `str` and `Hero.name` works like a special object for comparisons, you could write some code like: - -```Python -select(Hero).where(Hero.name == hero.name) -``` - -That would mean: - -> Hey SQL Database πŸ‘‹, please `SELECT` all the columns -> -> `FROM` the table for the model class `Hero` (the table `"hero"`) -> -> `WHERE` the column `"name"` is equal to the name of this hero instance I have here: `hero.name` (in the example above, the value `"Deadpond"`). - -## `.where()` and Expressions Instead of Keyword Arguments - -Now, let me tell you why I think that for this use case of interacting with SQL databases it's better to have these expressions: - -```Python -# Expression ✨ -select(Hero).where(Hero.name == "Deadpond") -``` - -...instead of keyword arguments like this: - -```Python -# Not supported, keyword argument 🚨 -select(Hero).where(name="Deadpond") -``` - -Of course, the keyword arguments would have been a bit shorter. - -But with the **expressions** your editor can help you a lot with autocompletion and inline error checks. ✨ - -Let me give you an example. Let's imagine that keyword arguments were supported in SQLModel and you wanted to filter using the secret identity of Spider-Boy. - -You could write: - -```Python -# Don't copy this 🚨 -select(Hero).where(secret_identity="Pedro Parqueador") -``` - -The editor would see the code, and because it doesn't have any information of which keyword arguments are allowed and which not, it would have no way to help you **detect the error**. - -Maybe your code could even run and seem like it's all fine, and then some months later you would be wondering why your app *never finds rows* although you were sure that there was one `"Pedro Parqueador"`. 😱 - -And maybe finally you would realize that we wrote the code using `secret_identity` which is not a column in the table. We should have written `secret_name` instead. - -Now, with the expressions, your editor would show you an error right away if you tried this: - -```Python -# Expression ✨ -select(Hero).where(Hero.secret_identity == "Pedro Parqueador") -``` - -Even better, it would autocomplete the correct one for you, to get: - -```Python -select(Hero).where(Hero.secret_name == "Pedro Parqueador") -``` - -I think that alone, having better editor support, autocompletion, and inline errors, is enough to make it worth having expressions instead of keyword arguments. ✨ - -/// tip - -**Expressions** also provide more features for other types of comparisons, shown down below. πŸ‘‡ - -/// - -## Exec the Statement - -Now that we know how `.where()` works, let's finish the code. - -It's actually the same as in previous chapters for selecting data: - -{* ./docs_src/tutorial/where/tutorial001_py310.py ln[34:39] hl[37:39] *} - -We take that statement, that now includes a `WHERE`, and we `exec()` it to get the results. - -And in this case the results will be just one: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// Now the important part, the SELECT with WHERE πŸ’‘ - -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.name = ? -INFO Engine [no key 0.00014s] ('Deadpond',) - -// Here's the only printed hero -secret_name='Dive Wilson' age=None id=1 name='Deadpond' -``` - -
- - -/// tip - -The `results` object is an iterable to be used in a `for` loop. - -Even if we got only one row, we iterate over that `results` object. Just as if it was a list of one element. - -We'll see other ways to get the data later. - -/// - -## Other Comparisons - -Here's another great advantage of these special **expressions** passed to `.where()`. - -Above, we have been using an "equality" comparison (using `==`), only checking if two things are the same value. - -But we can use other standard Python comparisons. ✨ - -### Not Equal - -We could get the rows where a column is **not** equal to a value using `!=`: - -{* ./docs_src/tutorial/where/tutorial002_py310.py ln[34:39] hl[36] *} - -That would output: - -``` -secret_name='Pedro Parqueador' age=None id=2 name='Spider-Boy' -secret_name='Tommy Sharp' age=48 id=3 name='Rusty-Man' -``` - -### Pause to Add Data - -Let's update the function `create_heroes()` and add some more rows to make the next comparison examples clearer: - -{* ./docs_src/tutorial/where/tutorial003_py310.py ln[21:39] hl[22:28,31:37] *} - -Now that we have several heroes with different ages, it's gonna be more obvious what the next comparisons do. - -### More Than - -Now let's use `>` to get the rows where a column is **more than** a value: - -{* ./docs_src/tutorial/where/tutorial003_py310.py ln[42:47] hl[44] *} - -That would output: - -``` -age=48 id=3 name='Rusty-Man' secret_name='Tommy Sharp' -age=36 id=6 name='Dr. Weird' secret_name='Steve Weird' -age=93 id=7 name='Captain North America' secret_name='Esteban Rogelios' -``` - -/// tip - -Notice that it didn't select `Black Lion`, because the age is not *strictly* greater than `35`. - -/// - -### More Than or Equal - -Let's do that again, but with `>=` to get the rows where a column is **more than or equal** to a value: - -{* ./docs_src/tutorial/where/tutorial004_py310.py ln[42:47] hl[44] *} - -Because we are using `>=`, the age `35` will be included in the output: - -``` hl_lines="2" -age=48 id=3 name='Rusty-Man' secret_name='Tommy Sharp' -age=35 id=5 name='Black Lion' secret_name='Trevor Challa' -age=36 id=6 name='Dr. Weird' secret_name='Steve Weird' -age=93 id=7 name='Captain North America' secret_name='Esteban Rogelios' -``` - -/// tip - -This time we got `Black Lion` too because although the age is not *strictly* greater than `35`it is *equal* to `35`. - -/// - -### Less Than - -Similarly, we can use `<` to get the rows where a column is **less than** a value: - -{* ./docs_src/tutorial/where/tutorial005_py310.py ln[42:47] hl[44] *} - -And we get the younger one with an age in the database: - -``` -age=32 id=4 name='Tarantula' secret_name='Natalia Roman-on' -``` - -/// tip - -We could imagine that **Spider-Boy** is even **younger**. But because we don't know the age, it is `NULL` in the database (`None` in Python), it doesn't match any of these age comparisons with numbers. - -/// - -### Less Than or Equal - -Finally, we can use `<=` to get the rows where a column is **less than or equal** to a value: - -{* ./docs_src/tutorial/where/tutorial006_py310.py ln[42:47] hl[44] *} - -And we get the younger ones, `35` and below: - -``` hl_lines="2" -age=32 id=4 name='Tarantula' secret_name='Natalia Roman-on' -age=35 id=5 name='Black Lion' secret_name='Trevor Challa' -``` - -/// tip - -We get `Black Lion` here too because although the age is not *strictly* less than `35` it is *equal* to `35`. - -/// - -### Benefits of Expressions - -Here's a good moment to see that being able to use these pure Python expressions instead of keyword arguments can help a lot. ✨ - -We can use the same standard Python comparison operators like `<`, `<=`, `>`, `>=`, `==`, etc. - -## Multiple `.where()` - -Because `.where()` returns the same special select object back, we can add more `.where()` calls to it: - -{* ./docs_src/tutorial/where/tutorial007_py310.py ln[42:47] hl[44] *} - -This will select the rows `WHERE` the `age` is **greater than or equal** to `35`, `AND` also the `age` is **less than** `40`. - -The equivalent SQL would be: - -```SQL hl_lines="3" -SELECT id, name, secret_name, age -FROM hero -WHERE age >= 35 AND age < 40 -``` - -This uses `AND` to put both comparisons together. - -We can then run it to see the output from the program: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// The SELECT statement with WHERE, also using AND -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.age >= ? AND hero.age < ? -INFO Engine [no key 0.00014s] (35, 40) - -// The two heroes printed -age=35 id=5 name='Black Lion' secret_name='Trevor Challa' -age=36 id=6 name='Dr. Weird' secret_name='Steve Weird' - -``` - -
- -## `.where()` With Multiple Expressions - -As an alternative to using multiple `.where()` we can also pass several expressions to a single `.where()`: - -{* ./docs_src/tutorial/where/tutorial008_py310.py ln[42:47] hl[44] *} - -This is the same as the above, and will result in the same output with the two heroes: - -``` -age=35 id=5 name='Black Lion' secret_name='Trevor Challa' -age=36 id=6 name='Dr. Weird' secret_name='Steve Weird' -``` - -## `.where()` With Multiple Expressions Using `OR` - -These last examples use `where()` with multiple expressions. And then those are combined in the final SQL using `AND`, which means that *all* of the expressions must be true in a row for it to be included in the results. - -But we can also combine expressions using `OR`. Which means that **any** (but not necessarily all) of the expressions should be true in a row for it to be included. - -To do it, you can import `or_`: - -{* ./docs_src/tutorial/where/tutorial009_py310.py ln[1] hl[1] *} - -And then pass both expressions to `or_()` and put it inside `.where()`. - -For example, here we select the heroes that are the youngest OR the oldest: - -{* ./docs_src/tutorial/where/tutorial009_py310.py ln[42:47] hl[44] *} - -When we run it, this generates the output: - -
- -```console -$ python app.py - -// Some boilerplate output omitted πŸ˜‰ - -// The SELECT statement with WHERE, also using OR πŸ” -INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age -FROM hero -WHERE hero.age <= ? OR hero.age > ? -INFO Engine [no key 0.00021s] (35, 90) - -// The results include the youngest and oldest ✨ -secret_name='Natalia Roman-on' age=32 id=4 name='Tarantula' -secret_name='Trevor Challa' age=35 id=5 name='Black Lion' -secret_name='Esteban Rogelios' age=93 id=7 name='Captain North America' -``` - -
- -## Type Annotations and Errors - -There's a chance that your editor gives you an error when using these comparisons, like: - -```Python -Hero.age > 35 -``` - -It would be an error telling you that - -> `Hero.age` is potentially `None`, and you cannot compare `None` with `>` - -This is because as we are using pure and plain Python annotations for the fields, `age` is indeed annotated as `int | None`. - -By using this simple and standard Python type annotations we get the benefit of the extra simplicity and the inline error checks when creating or using instances. ✨ - -And when we use these special **class attributes** in a `.where()`, during execution of the program, the special class attribute will know that the comparison only applies for the values that are not `NULL` in the database, and it will work correctly. - -But the editor doesn't know that it's a special **class attribute**, so it tries to help us preventing an error (that in this case is a false alarm). - -Nevertheless, we can easily fix. πŸŽ‰ - -We can tell the editor that this class attribute is actually a special **SQLModel** column (instead of an instance attribute with a normal value). - -To do that, we can import `col()` (as short for "column"): - -{* ./docs_src/tutorial/where/tutorial011_py310.py ln[1] hl[1] *} - -And then put the **class attribute** inside `col()` when using it in a `.where()`: - -{* ./docs_src/tutorial/where/tutorial011_py310.py ln[42:47] hl[44] *} - -So, now the comparison is not: - -```Python -Hero.age > 35 -``` - -...but: - -```Python -col(Hero.age) > 35 -``` - -And with that the editor knows this code is actually fine, because this is a special **SQLModel** column. - -/// tip - -That `col()` will come handy later, giving autocompletion to several other things we can do with these special **class attributes** for columns. - -But we'll get there later. - -/// - -## Recap - -You can use `.where()` with powerful expressions using **SQLModel** columns (the special class attributes) to filter the rows that you want. πŸš€ - -Up to now, the database would have been **looking through each one of the records** (rows) to find the ones that match what you want. If you have thousands or millions of records, this could be very **slow**. 😱 - -In the next section I'll tell you how to add **indexes** to the database, this is what will make the queries **very efficient**. 😎 diff --git a/docs/virtual-environments.md b/docs/virtual-environments.md deleted file mode 100644 index 131cd53d76..0000000000 --- a/docs/virtual-environments.md +++ /dev/null @@ -1,844 +0,0 @@ -# Virtual Environments - -When you work in Python projects you probably should use a **virtual environment** (or a similar mechanism) to isolate the packages you install for each project. - -/// info - -If you already know about virtual environments, how to create them and use them, you might want to skip this section. πŸ€“ - -/// - -/// tip - -A **virtual environment** is different than an **environment variable**. - -An **environment variable** is a variable in the system that can be used by programs. - -A **virtual environment** is a directory with some files in it. - -/// - -/// info - -This page will teach you how to use **virtual environments** and how they work. - -If you are ready to adopt a **tool that manages everything** for you (including installing Python), try uv. - -/// - -## Create a Project - -First, create a directory for your project. - -What I normally do is that I create a directory named `code` inside my home/user directory. - -And inside of that I create one directory per project. - -
- -```console -// Go to the home directory -$ cd -// Create a directory for all your code projects -$ mkdir code -// Enter into that code directory -$ cd code -// Create a directory for this project -$ mkdir awesome-project -// Enter into that project directory -$ cd awesome-project -``` - -
- -## Create a Virtual Environment - -When you start working on a Python project **for the first time**, create a virtual environment **inside your project**. - -/// tip - -You only need to do this **once per project**, not every time you work. - -/// - -//// tab | `venv` - -To create a virtual environment, you can use the `venv` module that comes with Python. - -
- -```console -$ python -m venv .venv -``` - -
- -/// details | What that command means - -* `python`: use the program called `python` -* `-m`: call a module as a script, we'll tell it which module next -* `venv`: use the module called `venv` that normally comes installed with Python -* `.venv`: create the virtual environment in the new directory `.venv` - -/// - -//// - -//// tab | `uv` - -If you have `uv` installed, you can use it to create a virtual environment. - -
- -```console -$ uv venv -``` - -
- -/// tip - -By default, `uv` will create a virtual environment in a directory called `.venv`. - -But you could customize it passing an additional argument with the directory name. - -/// - -//// - -That command creates a new virtual environment in a directory called `.venv`. - -/// details | `.venv` or other name - -You could create the virtual environment in a different directory, but there's a convention of calling it `.venv`. - -/// - -## Activate the Virtual Environment - -Activate the new virtual environment so that any Python command you run or package you install uses it. - -/// tip - -Do this **every time** you start a **new terminal session** to work on the project. - -/// - -//// tab | Linux, macOS - -
- -```console -$ source .venv/bin/activate -``` - -
- -//// - -//// tab | Windows PowerShell - -
- -```console -$ .venv\Scripts\Activate.ps1 -``` - -
- -//// - -//// tab | Windows Bash - -Or if you use Bash for Windows (e.g. Git Bash): - -
- -```console -$ source .venv/Scripts/activate -``` - -
- -//// - -/// tip - -Every time you install a **new package** in that environment, **activate** the environment again. - -This makes sure that if you use a **terminal (CLI) program** installed by that package, you use the one from your virtual environment and not any other that could be installed globally, probably with a different version than what you need. - -/// - -## Check the Virtual Environment is Active - -Check that the virtual environment is active (the previous command worked). - -/// tip - -This is **optional**, but it's a good way to **check** that everything is working as expected and you are using the virtual environment you intended. - -/// - -//// tab | Linux, macOS, Windows Bash - -
- -```console -$ which python - -/home/user/code/awesome-project/.venv/bin/python -``` - -
- -If it shows the `python` binary at `.venv/bin/python`, inside of your project (in this case `awesome-project`), then it worked. πŸŽ‰ - -//// - -//// tab | Windows PowerShell - -
- -```console -$ Get-Command python - -C:\Users\user\code\awesome-project\.venv\Scripts\python -``` - -
- -If it shows the `python` binary at `.venv\Scripts\python`, inside of your project (in this case `awesome-project`), then it worked. πŸŽ‰ - -//// - -## Upgrade `pip` - -/// tip - -If you use `uv` you would use it to install things instead of `pip`, so you don't need to upgrade `pip`. 😎 - -/// - -If you are using `pip` to install packages (it comes by default with Python), you should **upgrade** it to the latest version. - -Many exotic errors while installing a package are solved by just upgrading `pip` first. - -/// tip - -You would normally do this **once**, right after you create the virtual environment. - -/// - -Make sure the virtual environment is active (with the command above) and then run: - -
- -```console -$ python -m pip install --upgrade pip - ----> 100% -``` - -
- -## Add `.gitignore` - -If you are using **Git** (you should), add a `.gitignore` file to exclude everything in your `.venv` from Git. - -/// tip - -If you used `uv` to create the virtual environment, it already did this for you, you can skip this step. 😎 - -/// - -/// tip - -Do this **once**, right after you create the virtual environment. - -/// - -
- -```console -$ echo "*" > .venv/.gitignore -``` - -
- -/// details | What that command means - -* `echo "*"`: will "print" the text `*` in the terminal (the next part changes that a bit) -* `>`: anything printed to the terminal by the command to the left of `>` should not be printed but instead written to the file that goes to the right of `>` -* `.gitignore`: the name of the file where the text should be written - -And `*` for Git means "everything". So, it will ignore everything in the `.venv` directory. - -That command will create a file `.gitignore` with the content: - -```gitignore -* -``` - -/// - -## Install Packages - -After activating the environment, you can install packages in it. - -/// tip - -Do this **once** when installing or upgrading the packages your project needs. - -If you need to upgrade a version or add a new package you would **do this again**. - -/// - -### Install Packages Directly - -If you're in a hurry and don't want to use a file to declare your project's package requirements, you can install them directly. - -/// tip - -It's a (very) good idea to put the packages and versions your program needs in a file (for example `requirements.txt` or `pyproject.toml`). - -/// - -//// tab | `pip` - -
- -```console -$ pip install sqlmodel - ----> 100% -``` - -
- -//// - -//// tab | `uv` - -If you have `uv`: - -
- -```console -$ uv pip install sqlmodel ----> 100% -``` - -
- -//// - -### Install from `requirements.txt` - -If you have a `requirements.txt`, you can now use it to install its packages. - -//// tab | `pip` - -
- -```console -$ pip install -r requirements.txt ----> 100% -``` - -
- -//// - -//// tab | `uv` - -If you have `uv`: - -
- -```console -$ uv pip install -r requirements.txt ----> 100% -``` - -
- -//// - -/// details | `requirements.txt` - -A `requirements.txt` with some packages could look like: - -```requirements.txt -sqlmodel==0.13.0 -rich==13.7.1 -``` - -/// - -## Run Your Program - -After you activated the virtual environment, you can run your program, and it will use the Python inside of your virtual environment with the packages you installed there. - -
- -```console -$ python main.py - -Hello World -``` - -
- -## Configure Your Editor - -You would probably use an editor, make sure you configure it to use the same virtual environment you created (it will probably autodetect it) so that you can get autocompletion and inline errors. - -For example: - -* VS Code -* PyCharm - -/// tip - -You normally have to do this only **once**, when you create the virtual environment. - -/// - -## Deactivate the Virtual Environment - -Once you are done working on your project you can **deactivate** the virtual environment. - -
- -```console -$ deactivate -``` - -
- -This way, when you run `python` it won't try to run it from that virtual environment with the packages installed there. - -## Ready to Work - -Now you're ready to start working on your project. - - - -/// tip - -Do you want to understand what's all that above? - -Continue reading. πŸ‘‡πŸ€“ - -/// - -## Why Virtual Environments - -To work with SQLModel you need to install Python. - -After that, you would need to **install** SQLModel and any other **packages** you want to use. - -To install packages you would normally use the `pip` command that comes with Python (or similar alternatives). - -Nevertheless, if you just use `pip` directly, the packages would be installed in your **global Python environment** (the global installation of Python). - -### The Problem - -So, what's the problem with installing packages in the global Python environment? - -At some point, you will probably end up writing many different programs that depend on **different packages**. And some of these projects you work on will depend on **different versions** of the same package. 😱 - -For example, you could create a project called `philosophers-stone`, this program depends on another package called **`harry`, using the version `1`**. So, you need to install `harry`. - -```mermaid -flowchart LR - stone(philosophers-stone) -->|requires| harry-1[harry v1] -``` - -Then, at some point later, you create another project called `prisoner-of-azkaban`, and this project also depends on `harry`, but this project needs **`harry` version `3`**. - -```mermaid -flowchart LR - azkaban(prisoner-of-azkaban) --> |requires| harry-3[harry v3] -``` - -But now the problem is, if you install the packages globally (in the global environment) instead of in a local **virtual environment**, you will have to choose which version of `harry` to install. - -If you want to run `philosophers-stone` you will need to first install `harry` version `1`, for example with: - -
- -```console -$ pip install "harry==1" -``` - -
- -And then you would end up with `harry` version `1` installed in your global Python environment. - -```mermaid -flowchart LR - subgraph global[global env] - harry-1[harry v1] - end - subgraph stone-project[philosophers-stone project] - stone(philosophers-stone) -->|requires| harry-1 - end -``` - -But then if you want to run `prisoner-of-azkaban`, you will need to uninstall `harry` version `1` and install `harry` version `3` (or just installing version `3` would automatically uninstall version `1`). - -
- -```console -$ pip install "harry==3" -``` - -
- -And then you would end up with `harry` version `3` installed in your global Python environment. - -And if you try to run `philosophers-stone` again, there's a chance it would **not work** because it needs `harry` version `1`. - -```mermaid -flowchart LR - subgraph global[global env] - harry-1[harry v1] - style harry-1 fill:#ccc,stroke-dasharray: 5 5 - harry-3[harry v3] - end - subgraph stone-project[philosophers-stone project] - stone(philosophers-stone) -.-x|⛔️| harry-1 - end - subgraph azkaban-project[prisoner-of-azkaban project] - azkaban(prisoner-of-azkaban) --> |requires| harry-3 - end -``` - -/// tip - -It's very common in Python packages to try the best to **avoid breaking changes** in **new versions**, but it's better to be safe, and install newer versions intentionally and when you can run the tests to check everything is working correctly. - -/// - -Now, imagine that with **many** other **packages** that all your **projects depend on**. That's very difficult to manage. And you would probably end up running some projects with some **incompatible versions** of the packages, and not knowing why something isn't working. - -Also, depending on your operating system (e.g. Linux, Windows, macOS), it could have come with Python already installed. And in that case it probably had some packages pre-installed with some specific versions **needed by your system**. If you install packages in the global Python environment, you could end up **breaking** some of the programs that came with your operating system. - -## Where are Packages Installed - -When you install Python, it creates some directories with some files in your computer. - -Some of these directories are the ones in charge of having all the packages you install. - -When you run: - -
- -```console -// Don't run this now, it's just an example πŸ€“ -$ pip install sqlmodel ----> 100% -``` - -
- -That will download a compressed file with the SQLModel code, normally from PyPI. - -It will also **download** files for other packages that SQLModel depends on. - -Then it will **extract** all those files and put them in a directory in your computer. - -By default, it will put those files downloaded and extracted in the directory that comes with your Python installation, that's the **global environment**. - -## What are Virtual Environments - -The solution to the problems of having all the packages in the global environment is to use a **virtual environment for each project** you work on. - -A virtual environment is a **directory**, very similar to the global one, where you can install the packages for a project. - -This way, each project will have its own virtual environment (`.venv` directory) with its own packages. - -```mermaid -flowchart TB - subgraph stone-project[philosophers-stone project] - stone(philosophers-stone) --->|requires| harry-1 - subgraph venv1[.venv] - harry-1[harry v1] - end - end - subgraph azkaban-project[prisoner-of-azkaban project] - azkaban(prisoner-of-azkaban) --->|requires| harry-3 - subgraph venv2[.venv] - harry-3[harry v3] - end - end - stone-project ~~~ azkaban-project -``` - -## What Does Activating a Virtual Environment Mean - -When you activate a virtual environment, for example with: - -//// tab | Linux, macOS - -
- -```console -$ source .venv/bin/activate -``` - -
- -//// - -//// tab | Windows PowerShell - -
- -```console -$ .venv\Scripts\Activate.ps1 -``` - -
- -//// - -//// tab | Windows Bash - -Or if you use Bash for Windows (e.g. Git Bash): - -
- -```console -$ source .venv/Scripts/activate -``` - -
- -//// - -That command will create or modify some [environment variables](environment-variables.md){.internal-link target=_blank} that will be available for the next commands. - -One of those variables is the `PATH` variable. - -/// tip - -You can learn more about the `PATH` environment variable in the [Environment Variables](environment-variables.md#path-environment-variable){.internal-link target=_blank} section. - -/// - -Activating a virtual environment adds its path `.venv/bin` (on Linux and macOS) or `.venv\Scripts` (on Windows) to the `PATH` environment variable. - -Let's say that before activating the environment, the `PATH` variable looked like this: - -//// tab | Linux, macOS - -```plaintext -/usr/bin:/bin:/usr/sbin:/sbin -``` - -That means that the system would look for programs in: - -* `/usr/bin` -* `/bin` -* `/usr/sbin` -* `/sbin` - -//// - -//// tab | Windows - -```plaintext -C:\Windows\System32 -``` - -That means that the system would look for programs in: - -* `C:\Windows\System32` - -//// - -After activating the virtual environment, the `PATH` variable would look something like this: - -//// tab | Linux, macOS - -```plaintext -/home/user/code/awesome-project/.venv/bin:/usr/bin:/bin:/usr/sbin:/sbin -``` - -That means that the system will now start looking first for programs in: - -```plaintext -/home/user/code/awesome-project/.venv/bin -``` - -before looking in the other directories. - -So, when you type `python` in the terminal, the system will find the Python program in - -```plaintext -/home/user/code/awesome-project/.venv/bin/python -``` - -and use that one. - -//// - -//// tab | Windows - -```plaintext -C:\Users\user\code\awesome-project\.venv\Scripts;C:\Windows\System32 -``` - -That means that the system will now start looking first look for programs in: - -```plaintext -C:\Users\user\code\awesome-project\.venv\Scripts -``` - -before looking in the other directories. - -So, when you type `python` in the terminal, the system will find the Python program in - -```plaintext -C:\Users\user\code\awesome-project\.venv\Scripts\python -``` - -and use that one. - -//// - -An important detail is that it will put the virtual environment path at the **beginning** of the `PATH` variable. The system will find it **before** finding any other Python available. This way, when you run `python`, it will use the Python **from the virtual environment** instead of any other `python` (for example, a `python` from a global environment). - -Activating a virtual environment also changes a couple of other things, but this is one of the most important things it does. - -## Checking a Virtual Environment - -When you check if a virtual environment is active, for example with: - -//// tab | Linux, macOS, Windows Bash - -
- -```console -$ which python - -/home/user/code/awesome-project/.venv/bin/python -``` - -
- -//// - -//// tab | Windows PowerShell - -
- -```console -$ Get-Command python - -C:\Users\user\code\awesome-project\.venv\Scripts\python -``` - -
- -//// - -That means that the `python` program that will be used is the one **in the virtual environment**. - -you use `which` in Linux and macOS and `Get-Command` in Windows PowerShell. - -The way that command works is that it will go and check in the `PATH` environment variable, going through **each path in order**, looking for the program called `python`. Once it finds it, it will **show you the path** to that program. - -The most important part is that when you call `python`, that is the exact "`python`" that will be executed. - -So, you can confirm if you are in the correct virtual environment. - -/// tip - -It's easy to activate one virtual environment, get one Python, and then **go to another project**. - -And the second project **wouldn't work** because you are using the **incorrect Python**, from a virtual environment for another project. - -It's useful being able to check what `python` is being used. πŸ€“ - -/// - -## Why Deactivate a Virtual Environment - -For example, you could be working on a project `philosophers-stone`, **activate that virtual environment**, install packages and work with that environment. - -And then you want to work on **another project** `prisoner-of-azkaban`. - -You go to that project: - -
- -```console -$ cd ~/code/prisoner-of-azkaban -``` - -
- -If you don't deactivate the virtual environment for `philosophers-stone`, when you run `python` in the terminal, it will try to use the Python from `philosophers-stone`. - -
- -```console -$ cd ~/code/prisoner-of-azkaban - -$ python main.py - -// Error importing sirius, it's not installed 😱 -Traceback (most recent call last): - File "main.py", line 1, in - import sirius -``` - -
- -But if you deactivate the virtual environment and activate the new one for `prisoner-of-askaban` then when you run `python` it will use the Python from the virtual environment in `prisoner-of-azkaban`. - -
- -```console -$ cd ~/code/prisoner-of-azkaban - -// You don't need to be in the old directory to deactivate, you can do it wherever you are, even after going to the other project 😎 -$ deactivate - -// Activate the virtual environment in prisoner-of-azkaban/.venv πŸš€ -$ source .venv/bin/activate - -// Now when you run python, it will find the package sirius installed in this virtual environment ✨ -$ python main.py - -I solemnly swear 🐺 -``` - -
- -## Alternatives - -This is a simple guide to get you started and teach you how everything works **underneath**. - -There are many **alternatives** to managing virtual environments, package dependencies (requirements), projects. - -Once you are ready and want to use a tool to **manage the entire project**, packages dependencies, virtual environments, etc. I would suggest you try uv. - -`uv` can do a lot of things, it can: - -* **Install Python** for you, including different versions -* Manage the **virtual environment** for your projects -* Install **packages** -* Manage package **dependencies and versions** for your project -* Make sure you have an **exact** set of packages and versions to install, including their dependencies, so that you can be sure that you can run your project in production exactly the same as in your computer while developing, this is called **locking** -* And many other things - -## Conclusion - -If you read and understood all this, now **you know much more** about virtual environments than many developers out there. πŸ€“ - -Knowing these details will most probably be useful in a future time when you are debugging something that seems complex, but you will know **how it all works underneath**. 😎 diff --git a/docs_src/__init__.py b/docs_src/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/advanced/__init__.py b/docs_src/advanced/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/advanced/decimal/__init__.py b/docs_src/advanced/decimal/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/advanced/decimal/tutorial001.py b/docs_src/advanced/decimal/tutorial001.py deleted file mode 100644 index a0a9804ade..0000000000 --- a/docs_src/advanced/decimal/tutorial001.py +++ /dev/null @@ -1,61 +0,0 @@ -from decimal import Decimal -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - money: Decimal = Field(default=0, max_digits=5, decimal_places=3) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson", money=1.1) - hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador", money=0.001) - hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48, money=2.2) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Deadpond") - results = session.exec(statement) - hero_1 = results.one() - print("Hero 1:", hero_1) - - statement = select(Hero).where(Hero.name == "Rusty-Man") - results = session.exec(statement) - hero_2 = results.one() - print("Hero 2:", hero_2) - - total_money = hero_1.money + hero_2.money - print(f"Total money: {total_money}") - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/advanced/decimal/tutorial001_py310.py b/docs_src/advanced/decimal/tutorial001_py310.py deleted file mode 100644 index 267338912e..0000000000 --- a/docs_src/advanced/decimal/tutorial001_py310.py +++ /dev/null @@ -1,60 +0,0 @@ -from decimal import Decimal - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - money: Decimal = Field(default=0, max_digits=5, decimal_places=3) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson", money=1.1) - hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador", money=0.001) - hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48, money=2.2) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Deadpond") - results = session.exec(statement) - hero_1 = results.one() - print("Hero 1:", hero_1) - - statement = select(Hero).where(Hero.name == "Rusty-Man") - results = session.exec(statement) - hero_2 = results.one() - print("Hero 2:", hero_2) - - total_money = hero_1.money + hero_2.money - print(f"Total money: {total_money}") - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/advanced/uuid/__init__.py b/docs_src/advanced/uuid/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/advanced/uuid/tutorial001.py b/docs_src/advanced/uuid/tutorial001.py deleted file mode 100644 index cfd3146b41..0000000000 --- a/docs_src/advanced/uuid/tutorial001.py +++ /dev/null @@ -1,65 +0,0 @@ -import uuid -from typing import Union - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Union[int, None] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_hero(): - with Session(engine) as session: - hero = Hero(name="Deadpond", secret_name="Dive Wilson") - print("The hero before saving in the DB") - print(hero) - print("The hero ID was already set") - print(hero.id) - session.add(hero) - session.commit() - session.refresh(hero) - print("After saving in the DB") - print(hero) - - -def select_hero(): - with Session(engine) as session: - hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") - session.add(hero_2) - session.commit() - session.refresh(hero_2) - hero_id = hero_2.id - print("Created hero:") - print(hero_2) - print("Created hero ID:") - print(hero_id) - - statement = select(Hero).where(Hero.id == hero_id) - selected_hero = session.exec(statement).one() - print("Selected hero:") - print(selected_hero) - print("Selected hero ID:") - print(selected_hero.id) - - -def main() -> None: - create_db_and_tables() - create_hero() - select_hero() - - -if __name__ == "__main__": - main() diff --git a/docs_src/advanced/uuid/tutorial001_py310.py b/docs_src/advanced/uuid/tutorial001_py310.py deleted file mode 100644 index 610ec6b0d4..0000000000 --- a/docs_src/advanced/uuid/tutorial001_py310.py +++ /dev/null @@ -1,64 +0,0 @@ -import uuid - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_hero(): - with Session(engine) as session: - hero = Hero(name="Deadpond", secret_name="Dive Wilson") - print("The hero before saving in the DB") - print(hero) - print("The hero ID was already set") - print(hero.id) - session.add(hero) - session.commit() - session.refresh(hero) - print("After saving in the DB") - print(hero) - - -def select_hero(): - with Session(engine) as session: - hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") - session.add(hero_2) - session.commit() - session.refresh(hero_2) - hero_id = hero_2.id - print("Created hero:") - print(hero_2) - print("Created hero ID:") - print(hero_id) - - statement = select(Hero).where(Hero.id == hero_id) - selected_hero = session.exec(statement).one() - print("Selected hero:") - print(selected_hero) - print("Selected hero ID:") - print(selected_hero.id) - - -def main() -> None: - create_db_and_tables() - create_hero() - select_hero() - - -if __name__ == "__main__": - main() diff --git a/docs_src/advanced/uuid/tutorial002.py b/docs_src/advanced/uuid/tutorial002.py deleted file mode 100644 index 831725581b..0000000000 --- a/docs_src/advanced/uuid/tutorial002.py +++ /dev/null @@ -1,64 +0,0 @@ -import uuid -from typing import Union - -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Union[int, None] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_hero(): - with Session(engine) as session: - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") - print("The hero before saving in the DB") - print(hero_1) - print("The hero ID was already set") - print(hero_1.id) - session.add(hero_1) - session.commit() - session.refresh(hero_1) - print("After saving in the DB") - print(hero_1) - - -def select_hero(): - with Session(engine) as session: - hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") - session.add(hero_2) - session.commit() - session.refresh(hero_2) - hero_id = hero_2.id - print("Created hero:") - print(hero_2) - print("Created hero ID:") - print(hero_id) - - selected_hero = session.get(Hero, hero_id) - print("Selected hero:") - print(selected_hero) - print("Selected hero ID:") - print(selected_hero.id) - - -def main() -> None: - create_db_and_tables() - create_hero() - select_hero() - - -if __name__ == "__main__": - main() diff --git a/docs_src/advanced/uuid/tutorial002_py310.py b/docs_src/advanced/uuid/tutorial002_py310.py deleted file mode 100644 index 3ec8c80fa0..0000000000 --- a/docs_src/advanced/uuid/tutorial002_py310.py +++ /dev/null @@ -1,63 +0,0 @@ -import uuid - -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_hero(): - with Session(engine) as session: - hero = Hero(name="Deadpond", secret_name="Dive Wilson") - print("The hero before saving in the DB") - print(hero) - print("The hero ID was already set") - print(hero.id) - session.add(hero) - session.commit() - session.refresh(hero) - print("After saving in the DB") - print(hero) - - -def select_hero(): - with Session(engine) as session: - hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") - session.add(hero_2) - session.commit() - session.refresh(hero_2) - hero_id = hero_2.id - print("Created hero:") - print(hero_2) - print("Created hero ID:") - print(hero_id) - - selected_hero = session.get(Hero, hero_id) - print("Selected hero:") - print(selected_hero) - print("Selected hero ID:") - print(selected_hero.id) - - -def main() -> None: - create_db_and_tables() - create_hero() - select_hero() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/__init__.py b/docs_src/tutorial/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/automatic_id_none_refresh/__init__.py b/docs_src/tutorial/automatic_id_none_refresh/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/automatic_id_none_refresh/annotations/en/tutorial002.md b/docs_src/tutorial/automatic_id_none_refresh/annotations/en/tutorial002.md deleted file mode 100644 index fee38368dc..0000000000 --- a/docs_src/tutorial/automatic_id_none_refresh/annotations/en/tutorial002.md +++ /dev/null @@ -1,396 +0,0 @@ -1. Create the `hero_1`. - - **Doesn't generate any output**. - -2. Create the `hero_2`. - - **Doesn't generate any output**. - -3. Create the `hero_3`. - - **Doesn't generate any output**. - -4. Print the line `"Before interacting with the database"`. - - Generates the output: - - ``` - Before interacting with the database - ``` - -5. Print the `hero_1` before interacting with the database. - - Generates the output: - - ``` - Hero 1: id=None name='Deadpond' secret_name='Dive Wilson' age=None - ``` - -6. Print the `hero_2` before interacting with the database. - - Generates the output: - - ``` - Hero 2: id=None name='Spider-Boy' secret_name='Pedro Parqueador' age=None - ``` - -7. Print the `hero_3` before interacting with the database. - - Generates the output: - - ``` - Hero 3: id=None name='Rusty-Man' secret_name='Tommy Sharp' age=48 - ``` - -8. Create the `Session` in a `with` block. - - **Doesn't generate any output**. - -9. Add the `hero_1` to the session. - - This still doesn't save it to the database. - - **Doesn't generate any output**. - -10. Add the `hero_2` to the session. - - This still doesn't save it to the database. - - **Doesn't generate any output**. - -11. Add the `hero_3` to the session. - - This still doesn't save it to the database. - - **Doesn't generate any output**. - -12. Print the line `"After adding to the session"`. - - Generates the output: - - ``` - After adding to the session - ``` - -13. Print the `hero_1` after adding it to the session. - - It still has the same data as there hasn't been any interaction with the database yet. Notice that the `id` is still `None`. - - Generates the output: - - ``` - Hero 1: id=None name='Deadpond' secret_name='Dive Wilson' age=None - ``` - -14. Print the `hero_2` after adding it to the session. - - It still has the same data as there hasn't been any interaction with the database yet. Notice that the `id` is still `None`. - - Generates the output: - - ``` - Hero 2: id=None name='Spider-Boy' secret_name='Pedro Parqueador' age=None - ``` - -15. Print the `hero_3` after adding it to the session. - - It still has the same data as there hasn't been any interaction with the database yet. Notice that the `id` is still `None`. - - Generates the output: - - ``` - Hero 3: id=None name='Rusty-Man' secret_name='Tommy Sharp' age=48 - ``` - -16. `commit` the **session**. - - This will **save** all the data to the database. The **session** will use the **engine** to run a lot of SQL. - - Generates the output: - - ``` - INFO Engine BEGIN (implicit) - INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) - INFO Engine [generated in 0.00018s] ('Deadpond', 'Dive Wilson', None) - INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) - INFO Engine [cached since 0.0008968s ago] ('Spider-Boy', 'Pedro Parqueador', None) - INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?) - INFO Engine [cached since 0.001143s ago] ('Rusty-Man', 'Tommy Sharp', 48) - INFO Engine COMMIT - ``` - -17. Print the line `"After committing the session"`. - - Generates the output: - - ``` - After committing the session - ``` - -18. Print the `hero_1` after committing the session. - - The `hero_1` is now internally marked as expired, and until it is refreshed, it looks like if it didn't contain any data. - - Generates the output: - - ``` - Hero 1: - ``` - -19. Print the `hero_2` after committing the session. - - The `hero_2` is now internally marked as expired, and until it is refreshed, it looks like if it didn't contain any data. - - Generates the output: - - ``` - Hero 2: - ``` - -20. Print the `hero_3` after committing the session. - - The `hero_3` is now internally marked as expired, and until it is refreshed, it looks like if it didn't contain any data. - - Generates the output: - - ``` - Hero 3: - ``` - -21. Print the line `"After committing the session, show IDs"`. - - Generates the output: - - ``` - After committing the session, show IDs - ``` - -22. Print the `hero_1.id`. A lot happens here. - - Because we are accessing the attribute `id` of `hero_1`, **SQLModel** (actually SQLAlchemy) can detect that we are trying to access data from the `hero_1`. - - It then detects that `hero_1` is currently associated with a **session** (because we added it to the session and committed it), and it is marked as expired. - - Then with the **session**, it uses the **engine** to execute all the SQL to fetch the data for this object from the database. - - Next it updates the object with the new data and marks it internally as "fresh" or "not expired". - - Finally, it makes the ID value available for the rest of the Python expression. In this case, the Python expression just prints the ID. - - Generates the output: - - ``` - INFO Engine BEGIN (implicit) - INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age - FROM hero - WHERE hero.id = ? - INFO Engine [generated in 0.00017s] (1,) - - Hero 1 ID: 1 - ``` - -23. Print the `hero_2.id`. - - A lot happens here, all the same stuff that happened at point 22, but for this `hero_2` object. - - Generates the output: - - ``` - INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age - FROM hero - WHERE hero.id = ? - INFO Engine [cached since 0.001245s ago] (2,) - - Hero 2 ID: 2 - ``` - -24. Print the `hero_3.id`. - - A lot happens here, all the same stuff that happened at point 22, but for this `hero_3` object. - - Generates the output: - - ``` - INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age - FROM hero - WHERE hero.id = ? - INFO Engine [cached since 0.002215s ago] (3,) - - - Hero 3 ID: 3 - ``` - -25. Print the line `"After committing the session, show names"`. - - Generates the output: - - ``` - After committing the session, show names - ``` - -26. Print the `hero_1.name`. - - Because `hero_1` is still fresh, no additional data is fetched, no additional SQL is executed, and the name is available. - - Generates the output: - - ``` - Hero 1 name: Deadpond - ``` - -27. Print the `hero_2.name`. - - Because `hero_2` is still fresh, no additional data is fetched, no additional SQL is executed, and the name is available. - - Generates the output: - - ``` - Hero 2 name: Spider-Boy - ``` - -28. Print the `hero_3.name`. - - Because `hero_3` is still fresh, no additional data is fetched, no additional SQL is executed, and the name is available. - - Generates the output: - - ``` - Hero 3 name: Rusty-Man - ``` - -29. Explicitly refresh the `hero_1` object. - - The **session** will use the **engine** to execute the SQL necessary to fetch fresh data from the database for the `hero_1` object. - - Generates the output: - - ``` - INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age - FROM hero - WHERE hero.id = ? - INFO Engine [generated in 0.00024s] (1,) - ``` - -30. Explicitly refresh the `hero_2` object. - - The **session** will use the **engine** to execute the SQL necessary to fetch fresh data from the database for the `hero_2` object. - - Generates the output: - - ``` - INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age - FROM hero - WHERE hero.id = ? - INFO Engine [cached since 0.001487s ago] (2,) - ``` - -31. Explicitly refresh the `hero_3` object. - - The **session** will use the **engine** to execute the SQL necessary to fetch fresh data from the database for the `hero_3` object. - - Generates the output: - - ``` - INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age - FROM hero - WHERE hero.id = ? - INFO Engine [cached since 0.002377s ago] (3,) - ``` - -32. Print the line `"After refreshing the heroes"`. - - Generates the output: - - ``` - After refreshing the heroes - ``` - -33. Print the `hero_1`. - - /// info - - Even if the `hero_1` wasn't fresh, this would **not** trigger a `refresh` making the **session** use the **engine** to fetch data from the database because it is not accessing an attribute. - - /// - - Because the `hero_1` is fresh it has all it's data available. - - Generates the output: - - ``` - Hero 1: age=None id=1 name='Deadpond' secret_name='Dive Wilson' - ``` - -34. Print the `hero_2`. - - /// info - - Even if the `hero_2` wasn't fresh, this would **not** trigger a `refresh` making the **session** use the **engine** to fetch data from the database because it is not accessing an attribute. - - /// - - Because the `hero_2` is fresh it has all it's data available. - - Generates the output: - - ``` - Hero 2: age=None id=2 name='Spider-Boy' secret_name='Pedro Parqueador' - ``` - -35. Print the `hero_3`. - - /// info - - Even if the `hero_3` wasn't fresh, this would **not** trigger a `refresh` making the **session** use the **engine** to fetch data from the database because it is not accessing an attribute. - - /// - - Because the `hero_3` is fresh it has all it's data available. - - Generates the output: - - ``` - Hero 3: age=48 id=3 name='Rusty-Man' secret_name='Tommy Sharp' - ``` - -36. The `with` block ends here (there's no more indented code), so the **session** is closed, running all it's closing code. - - This includes doing a `ROLLBACK` of any possible transaction that could have been started. - - Generates the output: - - ``` - INFO Engine ROLLBACK - ``` - -37. Print the line `"After the session closes"`. - - Generates the output: - - ``` - After the session closes - ``` - -38. Print the `hero_1` after closing the session. - - Generates the output: - - ``` - Hero 1: age=None id=1 name='Deadpond' secret_name='Dive Wilson' - ``` - -39. Print the `hero_2` after closing the session. - - Generates the output: - - ``` - Hero 2: age=None id=2 name='Spider-Boy' secret_name='Pedro Parqueador' - ``` - -40. Print the `hero_3` after closing the session. - - Generates the output: - - ``` - Hero 3: age=48 id=3 name='Rusty-Man' secret_name='Tommy Sharp' - ``` diff --git a/docs_src/tutorial/automatic_id_none_refresh/tutorial001.py b/docs_src/tutorial/automatic_id_none_refresh/tutorial001.py deleted file mode 100644 index e15742ffb8..0000000000 --- a/docs_src/tutorial/automatic_id_none_refresh/tutorial001.py +++ /dev/null @@ -1,81 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine - - -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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - print("Before interacting with the database") - print("Hero 1:", hero_1) - print("Hero 2:", hero_2) - print("Hero 3:", hero_3) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - print("After adding to the session") - print("Hero 1:", hero_1) - print("Hero 2:", hero_2) - print("Hero 3:", hero_3) - - session.commit() - - print("After committing the session") - print("Hero 1:", hero_1) - print("Hero 2:", hero_2) - print("Hero 3:", hero_3) - - print("After committing the session, show IDs") - print("Hero 1 ID:", hero_1.id) - print("Hero 2 ID:", hero_2.id) - print("Hero 3 ID:", hero_3.id) - - print("After committing the session, show names") - print("Hero 1 name:", hero_1.name) - print("Hero 2 name:", hero_2.name) - print("Hero 3 name:", hero_3.name) - - session.refresh(hero_1) - session.refresh(hero_2) - session.refresh(hero_3) - - print("After refreshing the heroes") - print("Hero 1:", hero_1) - print("Hero 2:", hero_2) - print("Hero 3:", hero_3) - - print("After the session closes") - print("Hero 1:", hero_1) - print("Hero 2:", hero_2) - print("Hero 3:", hero_3) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py b/docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py deleted file mode 100644 index 6b76da58ff..0000000000 --- a/docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py +++ /dev/null @@ -1,79 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - print("Before interacting with the database") - print("Hero 1:", hero_1) - print("Hero 2:", hero_2) - print("Hero 3:", hero_3) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - print("After adding to the session") - print("Hero 1:", hero_1) - print("Hero 2:", hero_2) - print("Hero 3:", hero_3) - - session.commit() - - print("After committing the session") - print("Hero 1:", hero_1) - print("Hero 2:", hero_2) - print("Hero 3:", hero_3) - - print("After committing the session, show IDs") - print("Hero 1 ID:", hero_1.id) - print("Hero 2 ID:", hero_2.id) - print("Hero 3 ID:", hero_3.id) - - print("After committing the session, show names") - print("Hero 1 name:", hero_1.name) - print("Hero 2 name:", hero_2.name) - print("Hero 3 name:", hero_3.name) - - session.refresh(hero_1) - session.refresh(hero_2) - session.refresh(hero_3) - - print("After refreshing the heroes") - print("Hero 1:", hero_1) - print("Hero 2:", hero_2) - print("Hero 3:", hero_3) - - print("After the session closes") - print("Hero 1:", hero_1) - print("Hero 2:", hero_2) - print("Hero 3:", hero_3) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/automatic_id_none_refresh/tutorial002.py b/docs_src/tutorial/automatic_id_none_refresh/tutorial002.py deleted file mode 100644 index c597506a62..0000000000 --- a/docs_src/tutorial/automatic_id_none_refresh/tutorial002.py +++ /dev/null @@ -1,82 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine - - -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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") # (1)! - hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") # (2)! - hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) # (3)! - - print("Before interacting with the database") # (4)! - print("Hero 1:", hero_1) # (5)! - print("Hero 2:", hero_2) # (6)! - print("Hero 3:", hero_3) # (7)! - - with Session(engine) as session: # (8)! - session.add(hero_1) # (9)! - session.add(hero_2) # (10)! - session.add(hero_3) # (11)! - - print("After adding to the session") # (12)! - print("Hero 1:", hero_1) # (13)! - print("Hero 2:", hero_2) # (14)! - print("Hero 3:", hero_3) # (15)! - - session.commit() # (16)! - - print("After committing the session") # (17)! - print("Hero 1:", hero_1) # (18)! - print("Hero 2:", hero_2) # (19)! - print("Hero 3:", hero_3) # (20)! - - print("After committing the session, show IDs") # (21)! - print("Hero 1 ID:", hero_1.id) # (22)! - print("Hero 2 ID:", hero_2.id) # (23)! - print("Hero 3 ID:", hero_3.id) # (24)! - - print("After committing the session, show names") # (25)! - print("Hero 1 name:", hero_1.name) # (26)! - print("Hero 2 name:", hero_2.name) # (27)! - print("Hero 3 name:", hero_3.name) # (28)! - - session.refresh(hero_1) # (29)! - session.refresh(hero_2) # (30)! - session.refresh(hero_3) # (31)! - - print("After refreshing the heroes") # (32)! - print("Hero 1:", hero_1) # (33)! - print("Hero 2:", hero_2) # (34)! - print("Hero 3:", hero_3) # (35)! - # (36)! - - print("After the session closes") # (37)! - print("Hero 1:", hero_1) # (38)! - print("Hero 2:", hero_2) # (39)! - print("Hero 3:", hero_3) # (40)! - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/automatic_id_none_refresh/tutorial002_py310.py b/docs_src/tutorial/automatic_id_none_refresh/tutorial002_py310.py deleted file mode 100644 index 0f3ad44cf1..0000000000 --- a/docs_src/tutorial/automatic_id_none_refresh/tutorial002_py310.py +++ /dev/null @@ -1,80 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") # (1)! - hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") # (2)! - hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) # (3)! - - print("Before interacting with the database") # (4)! - print("Hero 1:", hero_1) # (5)! - print("Hero 2:", hero_2) # (6)! - print("Hero 3:", hero_3) # (7)! - - with Session(engine) as session: # (8)! - session.add(hero_1) # (9)! - session.add(hero_2) # (10)! - session.add(hero_3) # (11)! - - print("After adding to the session") # (12)! - print("Hero 1:", hero_1) # (13)! - print("Hero 2:", hero_2) # (14)! - print("Hero 3:", hero_3) # (15)! - - session.commit() # (16)! - - print("After committing the session") # (17)! - print("Hero 1:", hero_1) # (18)! - print("Hero 2:", hero_2) # (19)! - print("Hero 3:", hero_3) # (20)! - - print("After committing the session, show IDs") # (21)! - print("Hero 1 ID:", hero_1.id) # (22)! - print("Hero 2 ID:", hero_2.id) # (23)! - print("Hero 3 ID:", hero_3.id) # (24)! - - print("After committing the session, show names") # (25)! - print("Hero 1 name:", hero_1.name) # (26)! - print("Hero 2 name:", hero_2.name) # (27)! - print("Hero 3 name:", hero_3.name) # (28)! - - session.refresh(hero_1) # (29)! - session.refresh(hero_2) # (30)! - session.refresh(hero_3) # (31)! - - print("After refreshing the heroes") # (32)! - print("Hero 1:", hero_1) # (33)! - print("Hero 2:", hero_2) # (34)! - print("Hero 3:", hero_3) # (35)! - # (36)! - - print("After the session closes") # (37)! - print("Hero 1:", hero_1) # (38)! - print("Hero 2:", hero_2) # (39)! - print("Hero 3:", hero_3) # (40)! - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/code_structure/__init__.py b/docs_src/tutorial/code_structure/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/code_structure/tutorial001/__init__.py b/docs_src/tutorial/code_structure/tutorial001/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/code_structure/tutorial001/app.py b/docs_src/tutorial/code_structure/tutorial001/app.py deleted file mode 100644 index 3d1bfc69a0..0000000000 --- a/docs_src/tutorial/code_structure/tutorial001/app.py +++ /dev/null @@ -1,29 +0,0 @@ -from sqlmodel import Session - -from .database import create_db_and_tables, engine -from .models import Hero, Team - - -def create_heroes(): - with Session(engine) as session: - 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 - ) - session.add(hero_deadpond) - session.commit() - - session.refresh(hero_deadpond) - - print("Created hero:", hero_deadpond) - print("Hero's team:", hero_deadpond.team) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/code_structure/tutorial001/database.py b/docs_src/tutorial/code_structure/tutorial001/database.py deleted file mode 100644 index d6de16c11f..0000000000 --- a/docs_src/tutorial/code_structure/tutorial001/database.py +++ /dev/null @@ -1,10 +0,0 @@ -from sqlmodel import SQLModel, create_engine - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) diff --git a/docs_src/tutorial/code_structure/tutorial001/models.py b/docs_src/tutorial/code_structure/tutorial001/models.py deleted file mode 100644 index 8e2647b3c4..0000000000 --- a/docs_src/tutorial/code_structure/tutorial001/models.py +++ /dev/null @@ -1,21 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, SQLModel - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, 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/tutorial001_py310/__init__.py b/docs_src/tutorial/code_structure/tutorial001_py310/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/code_structure/tutorial001_py310/app.py b/docs_src/tutorial/code_structure/tutorial001_py310/app.py deleted file mode 100644 index 3d1bfc69a0..0000000000 --- a/docs_src/tutorial/code_structure/tutorial001_py310/app.py +++ /dev/null @@ -1,29 +0,0 @@ -from sqlmodel import Session - -from .database import create_db_and_tables, engine -from .models import Hero, Team - - -def create_heroes(): - with Session(engine) as session: - 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 - ) - session.add(hero_deadpond) - session.commit() - - session.refresh(hero_deadpond) - - print("Created hero:", hero_deadpond) - print("Hero's team:", hero_deadpond.team) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/code_structure/tutorial001_py310/database.py b/docs_src/tutorial/code_structure/tutorial001_py310/database.py deleted file mode 100644 index d6de16c11f..0000000000 --- a/docs_src/tutorial/code_structure/tutorial001_py310/database.py +++ /dev/null @@ -1,10 +0,0 @@ -from sqlmodel import SQLModel, create_engine - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) diff --git a/docs_src/tutorial/code_structure/tutorial001_py310/models.py b/docs_src/tutorial/code_structure/tutorial001_py310/models.py deleted file mode 100644 index 1f485ef0b1..0000000000 --- a/docs_src/tutorial/code_structure/tutorial001_py310/models.py +++ /dev/null @@ -1,19 +0,0 @@ -from sqlmodel import Field, Relationship, SQLModel - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - team: Team | None = Relationship(back_populates="heroes") diff --git a/docs_src/tutorial/code_structure/tutorial001_py39/__init__.py b/docs_src/tutorial/code_structure/tutorial001_py39/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/code_structure/tutorial001_py39/app.py b/docs_src/tutorial/code_structure/tutorial001_py39/app.py deleted file mode 100644 index 3d1bfc69a0..0000000000 --- a/docs_src/tutorial/code_structure/tutorial001_py39/app.py +++ /dev/null @@ -1,29 +0,0 @@ -from sqlmodel import Session - -from .database import create_db_and_tables, engine -from .models import Hero, Team - - -def create_heroes(): - with Session(engine) as session: - 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 - ) - session.add(hero_deadpond) - session.commit() - - session.refresh(hero_deadpond) - - print("Created hero:", hero_deadpond) - print("Hero's team:", hero_deadpond.team) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/code_structure/tutorial001_py39/database.py b/docs_src/tutorial/code_structure/tutorial001_py39/database.py deleted file mode 100644 index d6de16c11f..0000000000 --- a/docs_src/tutorial/code_structure/tutorial001_py39/database.py +++ /dev/null @@ -1,10 +0,0 @@ -from sqlmodel import SQLModel, create_engine - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) diff --git a/docs_src/tutorial/code_structure/tutorial001_py39/models.py b/docs_src/tutorial/code_structure/tutorial001_py39/models.py deleted file mode 100644 index ff6b6c2d66..0000000000 --- a/docs_src/tutorial/code_structure/tutorial001_py39/models.py +++ /dev/null @@ -1,21 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, SQLModel - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, 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/__init__.py b/docs_src/tutorial/code_structure/tutorial002/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/code_structure/tutorial002/app.py b/docs_src/tutorial/code_structure/tutorial002/app.py deleted file mode 100644 index 2ecaec0c3c..0000000000 --- a/docs_src/tutorial/code_structure/tutorial002/app.py +++ /dev/null @@ -1,30 +0,0 @@ -from sqlmodel import Session - -from .database import create_db_and_tables, engine -from .hero_model import Hero -from .team_model import Team - - -def create_heroes(): - with Session(engine) as session: - 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 - ) - session.add(hero_deadpond) - session.commit() - - session.refresh(hero_deadpond) - - print("Created hero:", hero_deadpond) - print("Hero's team:", hero_deadpond.team) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/code_structure/tutorial002/database.py b/docs_src/tutorial/code_structure/tutorial002/database.py deleted file mode 100644 index d6de16c11f..0000000000 --- a/docs_src/tutorial/code_structure/tutorial002/database.py +++ /dev/null @@ -1,10 +0,0 @@ -from sqlmodel import SQLModel, create_engine - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) diff --git a/docs_src/tutorial/code_structure/tutorial002/hero_model.py b/docs_src/tutorial/code_structure/tutorial002/hero_model.py deleted file mode 100644 index 06dd9c6dfd..0000000000 --- a/docs_src/tutorial/code_structure/tutorial002/hero_model.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import TYPE_CHECKING, Optional - -from sqlmodel import Field, Relationship, SQLModel - -if TYPE_CHECKING: - from .team_model import Team - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, 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/team_model.py b/docs_src/tutorial/code_structure/tutorial002/team_model.py deleted file mode 100644 index c8a008bf4c..0000000000 --- a/docs_src/tutorial/code_structure/tutorial002/team_model.py +++ /dev/null @@ -1,14 +0,0 @@ -from typing import TYPE_CHECKING, List, Optional - -from sqlmodel import Field, Relationship, SQLModel - -if TYPE_CHECKING: - from .hero_model import Hero - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team") diff --git a/docs_src/tutorial/code_structure/tutorial002_py310/__init__.py b/docs_src/tutorial/code_structure/tutorial002_py310/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/code_structure/tutorial002_py310/app.py b/docs_src/tutorial/code_structure/tutorial002_py310/app.py deleted file mode 100644 index 2ecaec0c3c..0000000000 --- a/docs_src/tutorial/code_structure/tutorial002_py310/app.py +++ /dev/null @@ -1,30 +0,0 @@ -from sqlmodel import Session - -from .database import create_db_and_tables, engine -from .hero_model import Hero -from .team_model import Team - - -def create_heroes(): - with Session(engine) as session: - 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 - ) - session.add(hero_deadpond) - session.commit() - - session.refresh(hero_deadpond) - - print("Created hero:", hero_deadpond) - print("Hero's team:", hero_deadpond.team) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/code_structure/tutorial002_py310/database.py b/docs_src/tutorial/code_structure/tutorial002_py310/database.py deleted file mode 100644 index d6de16c11f..0000000000 --- a/docs_src/tutorial/code_structure/tutorial002_py310/database.py +++ /dev/null @@ -1,10 +0,0 @@ -from sqlmodel import SQLModel, create_engine - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) diff --git a/docs_src/tutorial/code_structure/tutorial002_py310/hero_model.py b/docs_src/tutorial/code_structure/tutorial002_py310/hero_model.py deleted file mode 100644 index 52fe68be16..0000000000 --- a/docs_src/tutorial/code_structure/tutorial002_py310/hero_model.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import TYPE_CHECKING, Optional - -from sqlmodel import Field, Relationship, SQLModel - -if TYPE_CHECKING: - from .team_model import Team - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - team: Optional["Team"] = Relationship(back_populates="heroes") diff --git a/docs_src/tutorial/code_structure/tutorial002_py310/team_model.py b/docs_src/tutorial/code_structure/tutorial002_py310/team_model.py deleted file mode 100644 index 10af5b9c2f..0000000000 --- a/docs_src/tutorial/code_structure/tutorial002_py310/team_model.py +++ /dev/null @@ -1,14 +0,0 @@ -from typing import TYPE_CHECKING - -from sqlmodel import Field, Relationship, SQLModel - -if TYPE_CHECKING: - from .hero_model import Hero - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") diff --git a/docs_src/tutorial/code_structure/tutorial002_py39/__init__.py b/docs_src/tutorial/code_structure/tutorial002_py39/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/code_structure/tutorial002_py39/app.py b/docs_src/tutorial/code_structure/tutorial002_py39/app.py deleted file mode 100644 index 2ecaec0c3c..0000000000 --- a/docs_src/tutorial/code_structure/tutorial002_py39/app.py +++ /dev/null @@ -1,30 +0,0 @@ -from sqlmodel import Session - -from .database import create_db_and_tables, engine -from .hero_model import Hero -from .team_model import Team - - -def create_heroes(): - with Session(engine) as session: - 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 - ) - session.add(hero_deadpond) - session.commit() - - session.refresh(hero_deadpond) - - print("Created hero:", hero_deadpond) - print("Hero's team:", hero_deadpond.team) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/code_structure/tutorial002_py39/database.py b/docs_src/tutorial/code_structure/tutorial002_py39/database.py deleted file mode 100644 index d6de16c11f..0000000000 --- a/docs_src/tutorial/code_structure/tutorial002_py39/database.py +++ /dev/null @@ -1,10 +0,0 @@ -from sqlmodel import SQLModel, create_engine - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) diff --git a/docs_src/tutorial/code_structure/tutorial002_py39/hero_model.py b/docs_src/tutorial/code_structure/tutorial002_py39/hero_model.py deleted file mode 100644 index 06dd9c6dfd..0000000000 --- a/docs_src/tutorial/code_structure/tutorial002_py39/hero_model.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import TYPE_CHECKING, Optional - -from sqlmodel import Field, Relationship, SQLModel - -if TYPE_CHECKING: - from .team_model import Team - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, 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_py39/team_model.py b/docs_src/tutorial/code_structure/tutorial002_py39/team_model.py deleted file mode 100644 index b51c070cf1..0000000000 --- a/docs_src/tutorial/code_structure/tutorial002_py39/team_model.py +++ /dev/null @@ -1,14 +0,0 @@ -from typing import TYPE_CHECKING, Optional - -from sqlmodel import Field, Relationship, SQLModel - -if TYPE_CHECKING: - from .hero_model import Hero - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") diff --git a/docs_src/tutorial/connect/__init__.py b/docs_src/tutorial/connect/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/connect/create_tables/__init__.py b/docs_src/tutorial/connect/create_tables/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/connect/create_tables/tutorial001.py b/docs_src/tutorial/connect/create_tables/tutorial001.py deleted file mode 100644 index ef24ec77d0..0000000000 --- a/docs_src/tutorial/connect/create_tables/tutorial001.py +++ /dev/null @@ -1,36 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def main(): - create_db_and_tables() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/create_tables/tutorial001_py310.py b/docs_src/tutorial/connect/create_tables/tutorial001_py310.py deleted file mode 100644 index 460b9768d7..0000000000 --- a/docs_src/tutorial/connect/create_tables/tutorial001_py310.py +++ /dev/null @@ -1,34 +0,0 @@ -from sqlmodel import Field, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def main(): - create_db_and_tables() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/delete/__init__.py b/docs_src/tutorial/connect/delete/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/connect/delete/tutorial001.py b/docs_src/tutorial/connect/delete/tutorial001.py deleted file mode 100644 index aa7d0db287..0000000000 --- a/docs_src/tutorial/connect/delete/tutorial001.py +++ /dev/null @@ -1,81 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_spider_boy.team_id = None - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("No longer Preventer:", hero_spider_boy) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/delete/tutorial001_py310.py b/docs_src/tutorial/connect/delete/tutorial001_py310.py deleted file mode 100644 index de0dd8d7e7..0000000000 --- a/docs_src/tutorial/connect/delete/tutorial001_py310.py +++ /dev/null @@ -1,79 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_spider_boy.team_id = None - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("No longer Preventer:", hero_spider_boy) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/insert/__init__.py b/docs_src/tutorial/connect/insert/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/connect/insert/tutorial001.py b/docs_src/tutorial/connect/insert/tutorial001.py deleted file mode 100644 index d2e3b2f0e3..0000000000 --- a/docs_src/tutorial/connect/insert/tutorial001.py +++ /dev/null @@ -1,69 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Created hero:", hero_deadpond) - print("Created hero:", hero_rusty_man) - print("Created hero:", hero_spider_boy) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/insert/tutorial001_py310.py b/docs_src/tutorial/connect/insert/tutorial001_py310.py deleted file mode 100644 index 16c2c7fe09..0000000000 --- a/docs_src/tutorial/connect/insert/tutorial001_py310.py +++ /dev/null @@ -1,67 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Created hero:", hero_deadpond) - print("Created hero:", hero_rusty_man) - print("Created hero:", hero_spider_boy) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/select/__init__.py b/docs_src/tutorial/connect/select/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/connect/select/tutorial001.py b/docs_src/tutorial/connect/select/tutorial001.py deleted file mode 100644 index d98e635779..0000000000 --- a/docs_src/tutorial/connect/select/tutorial001.py +++ /dev/null @@ -1,78 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Created hero:", hero_deadpond) - print("Created hero:", hero_rusty_man) - print("Created hero:", hero_spider_boy) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero, Team).where(Hero.team_id == Team.id) - results = session.exec(statement) - for hero, team in results: - print("Hero:", hero, "Team:", team) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/select/tutorial001_py310.py b/docs_src/tutorial/connect/select/tutorial001_py310.py deleted file mode 100644 index faa25b3891..0000000000 --- a/docs_src/tutorial/connect/select/tutorial001_py310.py +++ /dev/null @@ -1,76 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Created hero:", hero_deadpond) - print("Created hero:", hero_rusty_man) - print("Created hero:", hero_spider_boy) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero, Team).where(Hero.team_id == Team.id) - results = session.exec(statement) - for hero, team in results: - print("Hero:", hero, "Team:", team) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/select/tutorial002.py b/docs_src/tutorial/connect/select/tutorial002.py deleted file mode 100644 index 270f95003d..0000000000 --- a/docs_src/tutorial/connect/select/tutorial002.py +++ /dev/null @@ -1,78 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Created hero:", hero_deadpond) - print("Created hero:", hero_rusty_man) - print("Created hero:", hero_spider_boy) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero, Team).join(Team) - results = session.exec(statement) - for hero, team in results: - print("Hero:", hero, "Team:", team) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/select/tutorial002_py310.py b/docs_src/tutorial/connect/select/tutorial002_py310.py deleted file mode 100644 index 08adc69d32..0000000000 --- a/docs_src/tutorial/connect/select/tutorial002_py310.py +++ /dev/null @@ -1,76 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Created hero:", hero_deadpond) - print("Created hero:", hero_rusty_man) - print("Created hero:", hero_spider_boy) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero, Team).join(Team) - results = session.exec(statement) - for hero, team in results: - print("Hero:", hero, "Team:", team) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/select/tutorial003.py b/docs_src/tutorial/connect/select/tutorial003.py deleted file mode 100644 index ee427e309c..0000000000 --- a/docs_src/tutorial/connect/select/tutorial003.py +++ /dev/null @@ -1,78 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Created hero:", hero_deadpond) - print("Created hero:", hero_rusty_man) - print("Created hero:", hero_spider_boy) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero, Team).join(Team, isouter=True) - results = session.exec(statement) - for hero, team in results: - print("Hero:", hero, "Team:", team) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/select/tutorial003_py310.py b/docs_src/tutorial/connect/select/tutorial003_py310.py deleted file mode 100644 index 07127c6901..0000000000 --- a/docs_src/tutorial/connect/select/tutorial003_py310.py +++ /dev/null @@ -1,76 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Created hero:", hero_deadpond) - print("Created hero:", hero_rusty_man) - print("Created hero:", hero_spider_boy) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero, Team).join(Team, isouter=True) - results = session.exec(statement) - for hero, team in results: - print("Hero:", hero, "Team:", team) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/select/tutorial004.py b/docs_src/tutorial/connect/select/tutorial004.py deleted file mode 100644 index 29a7c205bd..0000000000 --- a/docs_src/tutorial/connect/select/tutorial004.py +++ /dev/null @@ -1,78 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Created hero:", hero_deadpond) - print("Created hero:", hero_rusty_man) - print("Created hero:", hero_spider_boy) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).join(Team).where(Team.name == "Preventers") - results = session.exec(statement) - for hero in results: - print("Preventer Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/select/tutorial004_py310.py b/docs_src/tutorial/connect/select/tutorial004_py310.py deleted file mode 100644 index 6bcdee96be..0000000000 --- a/docs_src/tutorial/connect/select/tutorial004_py310.py +++ /dev/null @@ -1,76 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Created hero:", hero_deadpond) - print("Created hero:", hero_rusty_man) - print("Created hero:", hero_spider_boy) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).join(Team).where(Team.name == "Preventers") - results = session.exec(statement) - for hero in results: - print("Preventer Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/select/tutorial005.py b/docs_src/tutorial/connect/select/tutorial005.py deleted file mode 100644 index 96a12ab537..0000000000 --- a/docs_src/tutorial/connect/select/tutorial005.py +++ /dev/null @@ -1,78 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Created hero:", hero_deadpond) - print("Created hero:", hero_rusty_man) - print("Created hero:", hero_spider_boy) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero, Team).join(Team).where(Team.name == "Preventers") - results = session.exec(statement) - for hero, team in results: - print("Preventer Hero:", hero, "Team:", team) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/select/tutorial005_py310.py b/docs_src/tutorial/connect/select/tutorial005_py310.py deleted file mode 100644 index 445572a147..0000000000 --- a/docs_src/tutorial/connect/select/tutorial005_py310.py +++ /dev/null @@ -1,76 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Created hero:", hero_deadpond) - print("Created hero:", hero_rusty_man) - print("Created hero:", hero_spider_boy) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero, Team).join(Team).where(Team.name == "Preventers") - results = session.exec(statement) - for hero, team in results: - print("Preventer Hero:", hero, "Team:", team) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/update/__init__.py b/docs_src/tutorial/connect/update/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/connect/update/tutorial001.py b/docs_src/tutorial/connect/update/tutorial001.py deleted file mode 100644 index b32599fc0c..0000000000 --- a/docs_src/tutorial/connect/update/tutorial001.py +++ /dev/null @@ -1,75 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/connect/update/tutorial001_py310.py b/docs_src/tutorial/connect/update/tutorial001_py310.py deleted file mode 100644 index a6ebfa6ee0..0000000000 --- a/docs_src/tutorial/connect/update/tutorial001_py310.py +++ /dev/null @@ -1,73 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) 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) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/create_db_and_table/__init__.py b/docs_src/tutorial/create_db_and_table/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/create_db_and_table/annotations/en/tutorial003.md b/docs_src/tutorial/create_db_and_table/annotations/en/tutorial003.md deleted file mode 100644 index f20779a57c..0000000000 --- a/docs_src/tutorial/create_db_and_table/annotations/en/tutorial003.md +++ /dev/null @@ -1,75 +0,0 @@ -1. Import `Optional` from `typing` to declare fields that could be `None`. -2. Import the things we will need from `sqlmodel`: `Field`, `SQLModel`, `create_engine`. -3. Create the `Hero` model class, representing the `hero` table in the database. - - And also mark this class as a **table model** with `table=True`. - -4. Create the `id` field: - - It could be `None` until the database assigns a value to it, so we annotate it with `Optional`. - - It is a **primary key**, so we use `Field()` and the argument `primary_key=True`. - -5. Create the `name` field. - - It is required, so there's no default value, and it's not `Optional`. - -6. Create the `secret_name` field. - - Also required. - -7. Create the `age` field. - - It is not required, the default value is `None`. - - In the database, the default value will be `NULL`, the SQL equivalent of `None`. - - As this field could be `None` (and `NULL` in the database), we annotate it with `Optional`. - -8. Write the name of the database file. -9. Use the name of the database file to create the database URL. -10. Create the engine using the URL. - - This doesn't create the database yet, no file or table is created at this point, only the **engine** object that will handle the connections with this specific database, and with specific support for SQLite (based on the URL). - -11. Put the code that creates side effects in a function. - - In this case, only one line that creates the database file with the table. - -12. Create all the tables that were automatically registered in `SQLModel.metadata`. - -13. Add a main block, or "Top-level script environment". - - And put some logic to be executed when this is called directly with Python, as in: - -
- - ```console - $ python app.py - - // Execute all the stuff and show the output - ``` - -
- - ...but that is not executed when importing something from this module, like: - - ```Python - from app import Hero - ``` - -14. In this main block, call the function that creates the database file and the table. - - This way when we call it with: - -
- - ```console - $ python app.py - - // Doing stuff ✨ - ``` - -
- - ...it will create the database file and the table. diff --git a/docs_src/tutorial/create_db_and_table/tutorial001.py b/docs_src/tutorial/create_db_and_table/tutorial001.py deleted file mode 100644 index cab7d9e700..0000000000 --- a/docs_src/tutorial/create_db_and_table/tutorial001.py +++ /dev/null @@ -1,18 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, SQLModel, create_engine - - -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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - -SQLModel.metadata.create_all(engine) diff --git a/docs_src/tutorial/create_db_and_table/tutorial001_py310.py b/docs_src/tutorial/create_db_and_table/tutorial001_py310.py deleted file mode 100644 index 13ae331bf1..0000000000 --- a/docs_src/tutorial/create_db_and_table/tutorial001_py310.py +++ /dev/null @@ -1,16 +0,0 @@ -from sqlmodel import Field, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - -SQLModel.metadata.create_all(engine) diff --git a/docs_src/tutorial/create_db_and_table/tutorial002.py b/docs_src/tutorial/create_db_and_table/tutorial002.py deleted file mode 100644 index 3297aef598..0000000000 --- a/docs_src/tutorial/create_db_and_table/tutorial002.py +++ /dev/null @@ -1,24 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, SQLModel, create_engine - - -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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -if __name__ == "__main__": - create_db_and_tables() diff --git a/docs_src/tutorial/create_db_and_table/tutorial002_py310.py b/docs_src/tutorial/create_db_and_table/tutorial002_py310.py deleted file mode 100644 index 50c7241ef4..0000000000 --- a/docs_src/tutorial/create_db_and_table/tutorial002_py310.py +++ /dev/null @@ -1,22 +0,0 @@ -from sqlmodel import Field, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -if __name__ == "__main__": - create_db_and_tables() diff --git a/docs_src/tutorial/create_db_and_table/tutorial003.py b/docs_src/tutorial/create_db_and_table/tutorial003.py deleted file mode 100644 index 9406300400..0000000000 --- a/docs_src/tutorial/create_db_and_table/tutorial003.py +++ /dev/null @@ -1,24 +0,0 @@ -from typing import Optional # (1)! - -from sqlmodel import Field, SQLModel, create_engine # (2)! - - -class Hero(SQLModel, table=True): # (3)! - id: Optional[int] = Field(default=None, primary_key=True) # (4)! - name: str # (5)! - secret_name: str # (6)! - age: Optional[int] = None # (7)! - - -sqlite_file_name = "database.db" # (8)! -sqlite_url = f"sqlite:///{sqlite_file_name}" # (9)! - -engine = create_engine(sqlite_url, echo=True) # (10)! - - -def create_db_and_tables(): # (11)! - SQLModel.metadata.create_all(engine) # (12)! - - -if __name__ == "__main__": # (13)! - create_db_and_tables() # (14)! diff --git a/docs_src/tutorial/create_db_and_table/tutorial003_py310.py b/docs_src/tutorial/create_db_and_table/tutorial003_py310.py deleted file mode 100644 index 01fcb14ac2..0000000000 --- a/docs_src/tutorial/create_db_and_table/tutorial003_py310.py +++ /dev/null @@ -1,22 +0,0 @@ -from sqlmodel import Field, SQLModel, create_engine # (2)! - - -class Hero(SQLModel, table=True): # (3)! - id: int | None = Field(default=None, primary_key=True) # (4)! - name: str # (5)! - secret_name: str # (6)! - age: int | None = None # (7)! - - -sqlite_file_name = "database.db" # (8)! -sqlite_url = f"sqlite:///{sqlite_file_name}" # (9)! - -engine = create_engine(sqlite_url, echo=True) # (10)! - - -def create_db_and_tables(): # (11)! - SQLModel.metadata.create_all(engine) # (12)! - - -if __name__ == "__main__": # (13)! - create_db_and_tables() # (14)! diff --git a/docs_src/tutorial/delete/__init__.py b/docs_src/tutorial/delete/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/delete/annotations/en/tutorial002.md b/docs_src/tutorial/delete/annotations/en/tutorial002.md deleted file mode 100644 index a6971f67d5..0000000000 --- a/docs_src/tutorial/delete/annotations/en/tutorial002.md +++ /dev/null @@ -1,99 +0,0 @@ -1. Select the hero we will delete. - -2. Execute the query with the select statement object. - - This generates the output: - - ``` - INFO Engine BEGIN (implicit) - INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age - FROM hero - WHERE hero.name = ? - INFO Engine [no key 0.00011s] ('Spider-Youngster',) - ``` - -3. Get one hero object, expecting exactly one. - - /// tip - - This ensures there's no more than one, and that there's exactly one, not `None`. - - This would never return `None`, instead it would raise an exception. - - /// - -4. Print the hero object. - - This generates the output: - - ``` - Hero: name='Spider-Youngster' secret_name='Pedro Parqueador' age=16 id=2 - ``` - -5. Delete the hero from the **session**. - - This marks the hero as deleted from the session, but it will not be removed from the database until we **commit** the changes. - -6. Commit the session. - - This saves the changes in the session, including deleting this row. - - It generates the output: - - ``` - INFO Engine DELETE FROM hero WHERE hero.id = ? - INFO Engine [generated in 0.00020s] (2,) - INFO Engine COMMIT - ``` - -7. Print the deleted hero object. - - The hero is deleted in the database. And is marked as deleted in the session. - - But we still have the object in memory with its data, so we can use it to print it. - - This generates the output: - - ``` - Deleted hero: name='Spider-Youngster' secret_name='Pedro Parqueador' age=16 id=2 - ``` - -8. Select the same hero again. - - We'll do this to confirm if the hero is really deleted. - -9. Execute the select statement. - - This generates the output: - - ``` - INFO Engine BEGIN (implicit) - INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age - FROM hero - WHERE hero.name = ? - INFO Engine [no key 0.00013s] ('Spider-Youngster',) - ``` - -10. Get the "first" item from the `results`. - - If no items were found, this will return `None`, which is what we expect. - -11. Check if the first item from the results is `None`. - -12. If this first item is indeed `None`, it means that it was correctly deleted from the database. - - Now we can print a message to confirm. - - This generates the output: - - ``` - There's no hero named Spider-Youngster - ``` - -13. This is the end of the `with` block, here the **session** executes its closing code. - - This generates the output: - - ``` - INFO Engine ROLLBACK - ``` diff --git a/docs_src/tutorial/delete/tutorial001.py b/docs_src/tutorial/delete/tutorial001.py deleted file mode 100644 index 7c911df50e..0000000000 --- a/docs_src/tutorial/delete/tutorial001.py +++ /dev/null @@ -1,100 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - results = session.exec(statement) - hero_1 = results.one() - print("Hero 1:", hero_1) - - statement = select(Hero).where(Hero.name == "Captain North America") - results = session.exec(statement) - hero_2 = results.one() - print("Hero 2:", hero_2) - - hero_1.age = 16 - hero_1.name = "Spider-Youngster" - session.add(hero_1) - - hero_2.name = "Captain North America Except Canada" - hero_2.age = 110 - session.add(hero_2) - - session.commit() - session.refresh(hero_1) - session.refresh(hero_2) - - print("Updated hero 1:", hero_1) - print("Updated hero 2:", hero_2) - - -def delete_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Youngster") - results = session.exec(statement) - hero = results.one() - print("Hero: ", hero) - - session.delete(hero) - session.commit() - - print("Deleted hero:", hero) - - statement = select(Hero).where(Hero.name == "Spider-Youngster") - results = session.exec(statement) - hero = results.first() - - if hero is None: - print("There's no hero named Spider-Youngster") - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - delete_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/delete/tutorial001_py310.py b/docs_src/tutorial/delete/tutorial001_py310.py deleted file mode 100644 index 7c29efa3c3..0000000000 --- a/docs_src/tutorial/delete/tutorial001_py310.py +++ /dev/null @@ -1,98 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - results = session.exec(statement) - hero_1 = results.one() - print("Hero 1:", hero_1) - - statement = select(Hero).where(Hero.name == "Captain North America") - results = session.exec(statement) - hero_2 = results.one() - print("Hero 2:", hero_2) - - hero_1.age = 16 - hero_1.name = "Spider-Youngster" - session.add(hero_1) - - hero_2.name = "Captain North America Except Canada" - hero_2.age = 110 - session.add(hero_2) - - session.commit() - session.refresh(hero_1) - session.refresh(hero_2) - - print("Updated hero 1:", hero_1) - print("Updated hero 2:", hero_2) - - -def delete_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Youngster") - results = session.exec(statement) - hero = results.one() - print("Hero: ", hero) - - session.delete(hero) - session.commit() - - print("Deleted hero:", hero) - - statement = select(Hero).where(Hero.name == "Spider-Youngster") - results = session.exec(statement) - hero = results.first() - - if hero is None: - print("There's no hero named Spider-Youngster") - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - delete_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/delete/tutorial002.py b/docs_src/tutorial/delete/tutorial002.py deleted file mode 100644 index 4d8c368d3c..0000000000 --- a/docs_src/tutorial/delete/tutorial002.py +++ /dev/null @@ -1,101 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - results = session.exec(statement) - hero_1 = results.one() - print("Hero 1:", hero_1) - - statement = select(Hero).where(Hero.name == "Captain North America") - results = session.exec(statement) - hero_2 = results.one() - print("Hero 2:", hero_2) - - hero_1.age = 16 - hero_1.name = "Spider-Youngster" - session.add(hero_1) - - hero_2.name = "Captain North America Except Canada" - hero_2.age = 110 - session.add(hero_2) - - session.commit() - session.refresh(hero_1) - session.refresh(hero_2) - - print("Updated hero 1:", hero_1) - print("Updated hero 2:", hero_2) - - -def delete_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Youngster") # (1)! - results = session.exec(statement) # (2)! - hero = results.one() # (3)! - print("Hero: ", hero) # (4)! - - session.delete(hero) # (5)! - session.commit() # (6)! - - print("Deleted hero:", hero) # (7)! - - statement = select(Hero).where(Hero.name == "Spider-Youngster") # (8)! - results = session.exec(statement) # (9)! - hero = results.first() # (10)! - - if hero is None: # (11)! - print("There's no hero named Spider-Youngster") # (12)! - # (13)! - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - delete_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/delete/tutorial002_py310.py b/docs_src/tutorial/delete/tutorial002_py310.py deleted file mode 100644 index afe9a4764d..0000000000 --- a/docs_src/tutorial/delete/tutorial002_py310.py +++ /dev/null @@ -1,99 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - results = session.exec(statement) - hero_1 = results.one() - print("Hero 1:", hero_1) - - statement = select(Hero).where(Hero.name == "Captain North America") - results = session.exec(statement) - hero_2 = results.one() - print("Hero 2:", hero_2) - - hero_1.age = 16 - hero_1.name = "Spider-Youngster" - session.add(hero_1) - - hero_2.name = "Captain North America Except Canada" - hero_2.age = 110 - session.add(hero_2) - - session.commit() - session.refresh(hero_1) - session.refresh(hero_2) - - print("Updated hero 1:", hero_1) - print("Updated hero 2:", hero_2) - - -def delete_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Youngster") # (1)! - results = session.exec(statement) # (2)! - hero = results.one() # (3)! - print("Hero: ", hero) # (4)! - - session.delete(hero) # (5)! - session.commit() # (6)! - - print("Deleted hero:", hero) # (7)! - - statement = select(Hero).where(Hero.name == "Spider-Youngster") # (8)! - results = session.exec(statement) # (9)! - hero = results.first() # (10)! - - if hero is None: # (11)! - print("There's no hero named Spider-Youngster") # (12)! - # (13)! - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - delete_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/fastapi/__init__.py b/docs_src/tutorial/fastapi/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/app_testing/__init__.py b/docs_src/tutorial/fastapi/app_testing/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/__init__.py b/docs_src/tutorial/fastapi/app_testing/tutorial001/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_001.md b/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_001.md deleted file mode 100644 index 936b84b92d..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_001.md +++ /dev/null @@ -1,17 +0,0 @@ -1. Import the `app` from the the `main` module. - -2. We create a `TestClient` for the FastAPI `app` and put it in the variable `client`. - -3. Then we use use this `client` to **talk to the API** and send a `POST` HTTP operation, creating a new hero. - -4. Then we get the **JSON data** from the response and put it in the variable `data`. - -5. Next we start testing the results with `assert` statements, we check that the status code of the response is `200`. - -6. We check that the `name` of the hero created is `"Deadpond"`. - -7. We check that the `secret_name` of the hero created is `"Dive Wilson"`. - -8. We check that the `age` of the hero created is `None`, because we didn't send an age. - -9. We check that the hero created has an `id` created by the database, so it's not `None`. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_002.md b/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_002.md deleted file mode 100644 index 0f8555a8dd..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_002.md +++ /dev/null @@ -1,25 +0,0 @@ -1. Import the `get_session` dependency from the the `main` module. - -2. Define the new function that will be the new **dependency override**. - -3. This function will return a different **session** than the one that would be returned by the original `get_session` function. - - We haven't seen how this new **session** object is created yet, but the point is that this is a different session than the original one from the app. - - This session is attached to a different **engine**, and that different **engine** uses a different URL, for a database just for testing. - - We haven't defined that new **URL** nor the new **engine** yet, but here we already see the that this object `session` will override the one returned by the original dependency `get_session()`. - -4. Then, the FastAPI `app` object has an attribute `app.dependency_overrides`. - - This attribute is a dictionary, and we can put dependency overrides in it by passing, as the **key**, the **original dependency function**, and as the **value**, the **new overriding dependency function**. - - So, here we are telling the FastAPI app to use `get_session_override` instead of `get_session` in all the places in the code that depend on `get_session`, that is, all the parameters with something like: - - ```Python - session: Session = Depends(get_session) - ``` - -5. After we are done with the dependency override, we can restore the application back to normal, by removing all the values in this dictionary `app.dependency_overrides`. - - This way whenever a *path operation function* needs the dependency FastAPI will use the original one instead of the override. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_003.md b/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_003.md deleted file mode 100644 index 2b48ebdacf..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_003.md +++ /dev/null @@ -1,37 +0,0 @@ -1. Here's a subtle thing to notice. - - Remember that [Order Matters](../create-db-and-table.md#sqlmodel-metadata-order-matters){.internal-link target=_blank} and we need to make sure all the **SQLModel** models are already defined and **imported** before calling `.create_all()`. - - IN this line, by importing something, *anything*, from `.main`, the code in `.main` will be executed, including the definition of the **table models**, and that will automatically register them in `SQLModel.metadata`. - -2. Here we create a new **engine**, completely different from the one in `main.py`. - - This is the engine we will use for the tests. - - We use the new URL of the database for tests: - - ``` - sqlite:///testing.db - ``` - - And again, we use the connection argument `check_same_thread=False`. - -3. Then we call: - - ```Python - SQLModel.metadata.create_all(engine) - ``` - - ...to make sure we create all the tables in the new testing database. - - The **table models** are registered in `SQLModel.metadata` just because we imported *something* from `.main`, and the code in `.main` was executed, creating the classes for the **table models** and automatically registering them in `SQLModel.metadata`. - - So, by the point we call this method, the **table models** are already registered there. πŸ’― - -4. Here's where we create the custom **session** object for this test in a `with` block. - - It uses the new custom **engine** we created, so anything that uses this session will be using the testing database. - -5. Now, back to the dependency override, it is just returning the same **session** object from outside, that's it, that's the whole trick. - -6. By this point, the testing **session** `with` block finishes, and the session is closed, the file is closed, etc. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_004.md b/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_004.md deleted file mode 100644 index de754c5e76..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_004.md +++ /dev/null @@ -1,29 +0,0 @@ -1. Import `StaticPool` from `sqlmodel`, we will use it in a bit. - -2. For the **SQLite URL**, don't write any file name, leave it empty. - - So, instead of: - - ``` - sqlite:///testing.db - ``` - - ...just write: - - ``` - sqlite:// - ``` - - This is enough to tell **SQLModel** (actually SQLAlchemy) that we want to use an **in-memory SQLite database**. - -3. Remember that we told the **low-level** library in charge of communicating with SQLite that we want to be able to **access the database from different threads** with `check_same_thread=False`? - - Now that we use an **in-memory database**, we need to also tell SQLAlchemy that we want to be able to use the **same in-memory database** object from different threads. - - We tell it that with the `poolclass=StaticPool` parameter. - - /// info - - You can read more details in the SQLAlchemy documentation about Using a Memory Database in Multiple Threads - - /// diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_005.md b/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_005.md deleted file mode 100644 index 126e1f1790..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_005.md +++ /dev/null @@ -1,41 +0,0 @@ -1. Import `pytest`. - -2. Use the `@pytest.fixture()` decorator on top of the function to tell pytest that this is a **fixture** function (equivalent to a FastAPI dependency). - - We also give it a name of `"session"`, this will be important in the testing function. - -3. Create the fixture function. This is equivalent to a FastAPI dependency function. - - In this fixture we create the custom **engine**, with the in-memory database, we create the tables, and we create the **session**. - - Then we `yield` the `session` object. - -4. The thing that we `return` or `yield` is what will be available to the test function, in this case, the `session` object. - - Here we use `yield` so that **pytest** comes back to execute "the rest of the code" in this function once the testing function is done. - - We don't have any more visible "rest of the code" after the `yield`, but we have the end of the `with` block that will close the **session**. - - By using `yield`, pytest will: - - * run the first part - * create the **session** object - * give it to the test function - * run the test function - * once the test function is done, it will continue here, right after the `yield`, and will correctly close the **session** object in the end of the `with` block. - -5. Now, in the test function, to tell **pytest** that this test wants to get the fixture, instead of declaring something like in FastAPI with: - - ```Python - session: Session = Depends(session_fixture) - ``` - - ...the way we tell pytest what is the fixture that we want is by using the **exact same name** of the fixture. - - In this case, we named it `session`, so the parameter has to be exactly named `session` for it to work. - - We also add the type annotation `session: Session` so that we can get autocompletion and inline error checks in our editor. - -6. Now in the dependency override function, we just return the same `session` object that came from outside it. - - The `session` object comes from the parameter passed to the test function, and we just re-use it and return it here in the dependency override. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_006.md b/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_006.md deleted file mode 100644 index d44a3b67da..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/annotations/en/test_main_006.md +++ /dev/null @@ -1,23 +0,0 @@ -1. Create the new fixture named `"client"`. - -2. This **client fixture**, in turn, also requires the **session fixture**. - -3. Now we create the **dependency override** inside the client fixture. - -4. Set the **dependency override** in the `app.dependency_overrides` dictionary. - -5. Create the `TestClient` with the **FastAPI** `app`. - -6. `yield` the `TestClient` instance. - - By using `yield`, after the test function is done, pytest will come back to execute the rest of the code after `yield`. - -7. This is the cleanup code, after `yield`, and after the test function is done. - - Here we clear the dependency overrides (here it's only one) in the FastAPI `app`. - -8. Now the test function requires the **client fixture**. - - And inside the test function, the code is quite **simple**, we just use the `TestClient` to make requests to the API, check the data, and that's it. - - The fixtures take care of all the **setup** and **cleanup** code. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/main.py b/docs_src/tutorial/fastapi/app_testing/tutorial001/main.py deleted file mode 100644 index f0a2559467..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/main.py +++ /dev/null @@ -1,105 +0,0 @@ -from typing import List, Optional - -from fastapi import Depends, FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def get_session(): - with Session(engine) as session: - yield session - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=List[HeroPublic]) -def read_heroes( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero( - *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate -): - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_extra_coverage.py b/docs_src/tutorial/fastapi/app_testing/tutorial001/test_extra_coverage.py deleted file mode 100644 index 1d8153ab9f..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_extra_coverage.py +++ /dev/null @@ -1,38 +0,0 @@ -from fastapi.testclient import TestClient -from sqlalchemy import Inspector, inspect -from sqlmodel import Session, create_engine - -from . import main as app_mod -from .test_main import client_fixture, session_fixture - -assert client_fixture, "This keeps the client fixture used below" -assert session_fixture, "This keeps the session fixture used by client_fixture" - - -def test_startup(): - app_mod.engine = create_engine("sqlite://") - app_mod.on_startup() - insp: Inspector = inspect(app_mod.engine) - assert insp.has_table(str(app_mod.Hero.__tablename__)) - - -def test_get_session(): - app_mod.engine = create_engine("sqlite://") - for session in app_mod.get_session(): - assert isinstance(session, Session) - assert session.bind == app_mod.engine - - -def test_read_hero_not_found(client: TestClient): - response = client.get("/heroes/9000") - assert response.status_code == 404 - - -def test_update_hero_not_found(client: TestClient): - response = client.patch("/heroes/9000", json={"name": "Very-Rusty-Man"}) - assert response.status_code == 404 - - -def test_delete_hero_not_found(client: TestClient): - response = client.delete("/heroes/9000") - assert response.status_code == 404 diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py b/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py deleted file mode 100644 index 435787c79b..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main.py +++ /dev/null @@ -1,125 +0,0 @@ -import pytest -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine -from sqlmodel.pool import StaticPool - -from .main import Hero, app, get_session - - -@pytest.fixture(name="session") -def session_fixture(): - engine = create_engine( - "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool - ) - SQLModel.metadata.create_all(engine) - with Session(engine) as session: - yield session - - -@pytest.fixture(name="client") -def client_fixture(session: Session): - def get_session_override(): - return session - - app.dependency_overrides[get_session] = get_session_override - client = TestClient(app) - yield client - app.dependency_overrides.clear() - - -def test_create_hero(client: TestClient): - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None - - -def test_create_hero_incomplete(client: TestClient): - # No secret_name - response = client.post("/heroes/", json={"name": "Deadpond"}) - assert response.status_code == 422 - - -def test_create_hero_invalid(client: TestClient): - # secret_name has an invalid type - response = client.post( - "/heroes/", - json={ - "name": "Deadpond", - "secret_name": {"message": "Do you wanna know my secret identity?"}, - }, - ) - assert response.status_code == 422 - - -def test_read_heroes(session: Session, client: TestClient): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") - hero_2 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) - session.add(hero_1) - session.add(hero_2) - session.commit() - - response = client.get("/heroes/") - data = response.json() - - assert response.status_code == 200 - - assert len(data) == 2 - assert data[0]["name"] == hero_1.name - assert data[0]["secret_name"] == hero_1.secret_name - assert data[0]["age"] == hero_1.age - assert data[0]["id"] == hero_1.id - assert data[1]["name"] == hero_2.name - assert data[1]["secret_name"] == hero_2.secret_name - assert data[1]["age"] == hero_2.age - assert data[1]["id"] == hero_2.id - - -def test_read_hero(session: Session, client: TestClient): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") - session.add(hero_1) - session.commit() - - response = client.get(f"/heroes/{hero_1.id}") - data = response.json() - - assert response.status_code == 200 - assert data["name"] == hero_1.name - assert data["secret_name"] == hero_1.secret_name - assert data["age"] == hero_1.age - assert data["id"] == hero_1.id - - -def test_update_hero(session: Session, client: TestClient): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") - session.add(hero_1) - session.commit() - - response = client.patch(f"/heroes/{hero_1.id}", json={"name": "Deadpuddle"}) - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpuddle" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] == hero_1.id - - -def test_delete_hero(session: Session, client: TestClient): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") - session.add(hero_1) - session.commit() - - response = client.delete(f"/heroes/{hero_1.id}") - - hero_in_db = session.get(Hero, hero_1.id) - - assert response.status_code == 200 - - assert hero_in_db is None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_001.py b/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_001.py deleted file mode 100644 index 3ae40773f9..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_001.py +++ /dev/null @@ -1,32 +0,0 @@ -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine - -from .main import app, get_session # (1)! - - -def test_create_hero(): - engine = create_engine( - "sqlite:///testing.db", connect_args={"check_same_thread": False} - ) - SQLModel.metadata.create_all(engine) - - with Session(engine) as session: - - def get_session_override(): - return session - - app.dependency_overrides[get_session] = get_session_override - - client = TestClient(app) # (2)! - - response = client.post( # (3)! - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() - data = response.json() # (4)! - - assert response.status_code == 200 # (5)! - assert data["name"] == "Deadpond" # (6)! - assert data["secret_name"] == "Dive Wilson" # (7)! - assert data["age"] is None # (8)! - assert data["id"] is not None # (9)! diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_002.py b/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_002.py deleted file mode 100644 index 727580b68f..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_002.py +++ /dev/null @@ -1,32 +0,0 @@ -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine - -from .main import app, get_session # (1)! - - -def test_create_hero(): - engine = create_engine( - "sqlite:///testing.db", connect_args={"check_same_thread": False} - ) - SQLModel.metadata.create_all(engine) - - with Session(engine) as session: - - def get_session_override(): # (2)! - return session # (3)! - - app.dependency_overrides[get_session] = get_session_override # (4)! - - client = TestClient(app) - - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() # (5)! - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_003.py b/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_003.py deleted file mode 100644 index 465c525108..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_003.py +++ /dev/null @@ -1,33 +0,0 @@ -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine - -from .main import app, get_session # (1)! - - -def test_create_hero(): - engine = create_engine( # (2)! - "sqlite:///testing.db", connect_args={"check_same_thread": False} - ) - SQLModel.metadata.create_all(engine) # (3)! - - with Session(engine) as session: # (4)! - - def get_session_override(): - return session # (5)! - - app.dependency_overrides[get_session] = get_session_override # (4)! - - client = TestClient(app) - - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None - # (6)! diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_004.py b/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_004.py deleted file mode 100644 index b770a9aa59..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_004.py +++ /dev/null @@ -1,35 +0,0 @@ -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine -from sqlmodel.pool import StaticPool # (1)! - -from .main import app, get_session - - -def test_create_hero(): - engine = create_engine( - "sqlite://", # (2)! - connect_args={"check_same_thread": False}, - poolclass=StaticPool, # (3)! - ) - SQLModel.metadata.create_all(engine) - - with Session(engine) as session: - - def get_session_override(): - return session - - app.dependency_overrides[get_session] = get_session_override - - client = TestClient(app) - - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_005.py b/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_005.py deleted file mode 100644 index f653eef7ec..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_005.py +++ /dev/null @@ -1,37 +0,0 @@ -import pytest # (1)! -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine -from sqlmodel.pool import StaticPool - -from .main import app, get_session - - -@pytest.fixture(name="session") # (2)! -def session_fixture(): # (3)! - engine = create_engine( - "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool - ) - SQLModel.metadata.create_all(engine) - with Session(engine) as session: - yield session # (4)! - - -def test_create_hero(session: Session): # (5)! - def get_session_override(): - return session # (6)! - - app.dependency_overrides[get_session] = get_session_override - - client = TestClient(app) - - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_006.py b/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_006.py deleted file mode 100644 index 8dbfd45caf..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001/test_main_006.py +++ /dev/null @@ -1,41 +0,0 @@ -import pytest -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine -from sqlmodel.pool import StaticPool - -from .main import app, get_session - - -@pytest.fixture(name="session") -def session_fixture(): - engine = create_engine( - "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool - ) - SQLModel.metadata.create_all(engine) - with Session(engine) as session: - yield session - - -@pytest.fixture(name="client") # (1)! -def client_fixture(session: Session): # (2)! - def get_session_override(): # (3)! - return session - - app.dependency_overrides[get_session] = get_session_override # (4)! - - client = TestClient(app) # (5)! - yield client # (6)! - app.dependency_overrides.clear() # (7)! - - -def test_create_hero(client: TestClient): # (8)! - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/__init__.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_001.md b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_001.md deleted file mode 100644 index 936b84b92d..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_001.md +++ /dev/null @@ -1,17 +0,0 @@ -1. Import the `app` from the the `main` module. - -2. We create a `TestClient` for the FastAPI `app` and put it in the variable `client`. - -3. Then we use use this `client` to **talk to the API** and send a `POST` HTTP operation, creating a new hero. - -4. Then we get the **JSON data** from the response and put it in the variable `data`. - -5. Next we start testing the results with `assert` statements, we check that the status code of the response is `200`. - -6. We check that the `name` of the hero created is `"Deadpond"`. - -7. We check that the `secret_name` of the hero created is `"Dive Wilson"`. - -8. We check that the `age` of the hero created is `None`, because we didn't send an age. - -9. We check that the hero created has an `id` created by the database, so it's not `None`. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_002.md b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_002.md deleted file mode 100644 index 0f8555a8dd..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_002.md +++ /dev/null @@ -1,25 +0,0 @@ -1. Import the `get_session` dependency from the the `main` module. - -2. Define the new function that will be the new **dependency override**. - -3. This function will return a different **session** than the one that would be returned by the original `get_session` function. - - We haven't seen how this new **session** object is created yet, but the point is that this is a different session than the original one from the app. - - This session is attached to a different **engine**, and that different **engine** uses a different URL, for a database just for testing. - - We haven't defined that new **URL** nor the new **engine** yet, but here we already see the that this object `session` will override the one returned by the original dependency `get_session()`. - -4. Then, the FastAPI `app` object has an attribute `app.dependency_overrides`. - - This attribute is a dictionary, and we can put dependency overrides in it by passing, as the **key**, the **original dependency function**, and as the **value**, the **new overriding dependency function**. - - So, here we are telling the FastAPI app to use `get_session_override` instead of `get_session` in all the places in the code that depend on `get_session`, that is, all the parameters with something like: - - ```Python - session: Session = Depends(get_session) - ``` - -5. After we are done with the dependency override, we can restore the application back to normal, by removing all the values in this dictionary `app.dependency_overrides`. - - This way whenever a *path operation function* needs the dependency FastAPI will use the original one instead of the override. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_003.md b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_003.md deleted file mode 100644 index 2b48ebdacf..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_003.md +++ /dev/null @@ -1,37 +0,0 @@ -1. Here's a subtle thing to notice. - - Remember that [Order Matters](../create-db-and-table.md#sqlmodel-metadata-order-matters){.internal-link target=_blank} and we need to make sure all the **SQLModel** models are already defined and **imported** before calling `.create_all()`. - - IN this line, by importing something, *anything*, from `.main`, the code in `.main` will be executed, including the definition of the **table models**, and that will automatically register them in `SQLModel.metadata`. - -2. Here we create a new **engine**, completely different from the one in `main.py`. - - This is the engine we will use for the tests. - - We use the new URL of the database for tests: - - ``` - sqlite:///testing.db - ``` - - And again, we use the connection argument `check_same_thread=False`. - -3. Then we call: - - ```Python - SQLModel.metadata.create_all(engine) - ``` - - ...to make sure we create all the tables in the new testing database. - - The **table models** are registered in `SQLModel.metadata` just because we imported *something* from `.main`, and the code in `.main` was executed, creating the classes for the **table models** and automatically registering them in `SQLModel.metadata`. - - So, by the point we call this method, the **table models** are already registered there. πŸ’― - -4. Here's where we create the custom **session** object for this test in a `with` block. - - It uses the new custom **engine** we created, so anything that uses this session will be using the testing database. - -5. Now, back to the dependency override, it is just returning the same **session** object from outside, that's it, that's the whole trick. - -6. By this point, the testing **session** `with` block finishes, and the session is closed, the file is closed, etc. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_004.md b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_004.md deleted file mode 100644 index de754c5e76..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_004.md +++ /dev/null @@ -1,29 +0,0 @@ -1. Import `StaticPool` from `sqlmodel`, we will use it in a bit. - -2. For the **SQLite URL**, don't write any file name, leave it empty. - - So, instead of: - - ``` - sqlite:///testing.db - ``` - - ...just write: - - ``` - sqlite:// - ``` - - This is enough to tell **SQLModel** (actually SQLAlchemy) that we want to use an **in-memory SQLite database**. - -3. Remember that we told the **low-level** library in charge of communicating with SQLite that we want to be able to **access the database from different threads** with `check_same_thread=False`? - - Now that we use an **in-memory database**, we need to also tell SQLAlchemy that we want to be able to use the **same in-memory database** object from different threads. - - We tell it that with the `poolclass=StaticPool` parameter. - - /// info - - You can read more details in the SQLAlchemy documentation about Using a Memory Database in Multiple Threads - - /// diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_005.md b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_005.md deleted file mode 100644 index 126e1f1790..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_005.md +++ /dev/null @@ -1,41 +0,0 @@ -1. Import `pytest`. - -2. Use the `@pytest.fixture()` decorator on top of the function to tell pytest that this is a **fixture** function (equivalent to a FastAPI dependency). - - We also give it a name of `"session"`, this will be important in the testing function. - -3. Create the fixture function. This is equivalent to a FastAPI dependency function. - - In this fixture we create the custom **engine**, with the in-memory database, we create the tables, and we create the **session**. - - Then we `yield` the `session` object. - -4. The thing that we `return` or `yield` is what will be available to the test function, in this case, the `session` object. - - Here we use `yield` so that **pytest** comes back to execute "the rest of the code" in this function once the testing function is done. - - We don't have any more visible "rest of the code" after the `yield`, but we have the end of the `with` block that will close the **session**. - - By using `yield`, pytest will: - - * run the first part - * create the **session** object - * give it to the test function - * run the test function - * once the test function is done, it will continue here, right after the `yield`, and will correctly close the **session** object in the end of the `with` block. - -5. Now, in the test function, to tell **pytest** that this test wants to get the fixture, instead of declaring something like in FastAPI with: - - ```Python - session: Session = Depends(session_fixture) - ``` - - ...the way we tell pytest what is the fixture that we want is by using the **exact same name** of the fixture. - - In this case, we named it `session`, so the parameter has to be exactly named `session` for it to work. - - We also add the type annotation `session: Session` so that we can get autocompletion and inline error checks in our editor. - -6. Now in the dependency override function, we just return the same `session` object that came from outside it. - - The `session` object comes from the parameter passed to the test function, and we just re-use it and return it here in the dependency override. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_006.md b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_006.md deleted file mode 100644 index d44a3b67da..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/annotations/en/test_main_006.md +++ /dev/null @@ -1,23 +0,0 @@ -1. Create the new fixture named `"client"`. - -2. This **client fixture**, in turn, also requires the **session fixture**. - -3. Now we create the **dependency override** inside the client fixture. - -4. Set the **dependency override** in the `app.dependency_overrides` dictionary. - -5. Create the `TestClient` with the **FastAPI** `app`. - -6. `yield` the `TestClient` instance. - - By using `yield`, after the test function is done, pytest will come back to execute the rest of the code after `yield`. - -7. This is the cleanup code, after `yield`, and after the test function is done. - - Here we clear the dependency overrides (here it's only one) in the FastAPI `app`. - -8. Now the test function requires the **client fixture**. - - And inside the test function, the code is quite **simple**, we just use the `TestClient` to make requests to the API, check the data, and that's it. - - The fixtures take care of all the **setup** and **cleanup** code. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/main.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/main.py deleted file mode 100644 index 84da9fd610..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/main.py +++ /dev/null @@ -1,103 +0,0 @@ -from fastapi import Depends, FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: int | None = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: str | None = None - secret_name: str | None = None - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def get_session(): - with Session(engine) as session: - yield session - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero( - *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate -): - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_extra_coverage.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_extra_coverage.py deleted file mode 100644 index 1d8153ab9f..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_extra_coverage.py +++ /dev/null @@ -1,38 +0,0 @@ -from fastapi.testclient import TestClient -from sqlalchemy import Inspector, inspect -from sqlmodel import Session, create_engine - -from . import main as app_mod -from .test_main import client_fixture, session_fixture - -assert client_fixture, "This keeps the client fixture used below" -assert session_fixture, "This keeps the session fixture used by client_fixture" - - -def test_startup(): - app_mod.engine = create_engine("sqlite://") - app_mod.on_startup() - insp: Inspector = inspect(app_mod.engine) - assert insp.has_table(str(app_mod.Hero.__tablename__)) - - -def test_get_session(): - app_mod.engine = create_engine("sqlite://") - for session in app_mod.get_session(): - assert isinstance(session, Session) - assert session.bind == app_mod.engine - - -def test_read_hero_not_found(client: TestClient): - response = client.get("/heroes/9000") - assert response.status_code == 404 - - -def test_update_hero_not_found(client: TestClient): - response = client.patch("/heroes/9000", json={"name": "Very-Rusty-Man"}) - assert response.status_code == 404 - - -def test_delete_hero_not_found(client: TestClient): - response = client.delete("/heroes/9000") - assert response.status_code == 404 diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main.py deleted file mode 100644 index 435787c79b..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main.py +++ /dev/null @@ -1,125 +0,0 @@ -import pytest -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine -from sqlmodel.pool import StaticPool - -from .main import Hero, app, get_session - - -@pytest.fixture(name="session") -def session_fixture(): - engine = create_engine( - "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool - ) - SQLModel.metadata.create_all(engine) - with Session(engine) as session: - yield session - - -@pytest.fixture(name="client") -def client_fixture(session: Session): - def get_session_override(): - return session - - app.dependency_overrides[get_session] = get_session_override - client = TestClient(app) - yield client - app.dependency_overrides.clear() - - -def test_create_hero(client: TestClient): - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None - - -def test_create_hero_incomplete(client: TestClient): - # No secret_name - response = client.post("/heroes/", json={"name": "Deadpond"}) - assert response.status_code == 422 - - -def test_create_hero_invalid(client: TestClient): - # secret_name has an invalid type - response = client.post( - "/heroes/", - json={ - "name": "Deadpond", - "secret_name": {"message": "Do you wanna know my secret identity?"}, - }, - ) - assert response.status_code == 422 - - -def test_read_heroes(session: Session, client: TestClient): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") - hero_2 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) - session.add(hero_1) - session.add(hero_2) - session.commit() - - response = client.get("/heroes/") - data = response.json() - - assert response.status_code == 200 - - assert len(data) == 2 - assert data[0]["name"] == hero_1.name - assert data[0]["secret_name"] == hero_1.secret_name - assert data[0]["age"] == hero_1.age - assert data[0]["id"] == hero_1.id - assert data[1]["name"] == hero_2.name - assert data[1]["secret_name"] == hero_2.secret_name - assert data[1]["age"] == hero_2.age - assert data[1]["id"] == hero_2.id - - -def test_read_hero(session: Session, client: TestClient): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") - session.add(hero_1) - session.commit() - - response = client.get(f"/heroes/{hero_1.id}") - data = response.json() - - assert response.status_code == 200 - assert data["name"] == hero_1.name - assert data["secret_name"] == hero_1.secret_name - assert data["age"] == hero_1.age - assert data["id"] == hero_1.id - - -def test_update_hero(session: Session, client: TestClient): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") - session.add(hero_1) - session.commit() - - response = client.patch(f"/heroes/{hero_1.id}", json={"name": "Deadpuddle"}) - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpuddle" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] == hero_1.id - - -def test_delete_hero(session: Session, client: TestClient): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") - session.add(hero_1) - session.commit() - - response = client.delete(f"/heroes/{hero_1.id}") - - hero_in_db = session.get(Hero, hero_1.id) - - assert response.status_code == 200 - - assert hero_in_db is None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_001.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_001.py deleted file mode 100644 index 3ae40773f9..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_001.py +++ /dev/null @@ -1,32 +0,0 @@ -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine - -from .main import app, get_session # (1)! - - -def test_create_hero(): - engine = create_engine( - "sqlite:///testing.db", connect_args={"check_same_thread": False} - ) - SQLModel.metadata.create_all(engine) - - with Session(engine) as session: - - def get_session_override(): - return session - - app.dependency_overrides[get_session] = get_session_override - - client = TestClient(app) # (2)! - - response = client.post( # (3)! - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() - data = response.json() # (4)! - - assert response.status_code == 200 # (5)! - assert data["name"] == "Deadpond" # (6)! - assert data["secret_name"] == "Dive Wilson" # (7)! - assert data["age"] is None # (8)! - assert data["id"] is not None # (9)! diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_002.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_002.py deleted file mode 100644 index 727580b68f..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_002.py +++ /dev/null @@ -1,32 +0,0 @@ -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine - -from .main import app, get_session # (1)! - - -def test_create_hero(): - engine = create_engine( - "sqlite:///testing.db", connect_args={"check_same_thread": False} - ) - SQLModel.metadata.create_all(engine) - - with Session(engine) as session: - - def get_session_override(): # (2)! - return session # (3)! - - app.dependency_overrides[get_session] = get_session_override # (4)! - - client = TestClient(app) - - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() # (5)! - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_003.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_003.py deleted file mode 100644 index 465c525108..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_003.py +++ /dev/null @@ -1,33 +0,0 @@ -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine - -from .main import app, get_session # (1)! - - -def test_create_hero(): - engine = create_engine( # (2)! - "sqlite:///testing.db", connect_args={"check_same_thread": False} - ) - SQLModel.metadata.create_all(engine) # (3)! - - with Session(engine) as session: # (4)! - - def get_session_override(): - return session # (5)! - - app.dependency_overrides[get_session] = get_session_override # (4)! - - client = TestClient(app) - - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None - # (6)! diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_004.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_004.py deleted file mode 100644 index b770a9aa59..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_004.py +++ /dev/null @@ -1,35 +0,0 @@ -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine -from sqlmodel.pool import StaticPool # (1)! - -from .main import app, get_session - - -def test_create_hero(): - engine = create_engine( - "sqlite://", # (2)! - connect_args={"check_same_thread": False}, - poolclass=StaticPool, # (3)! - ) - SQLModel.metadata.create_all(engine) - - with Session(engine) as session: - - def get_session_override(): - return session - - app.dependency_overrides[get_session] = get_session_override - - client = TestClient(app) - - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_005.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_005.py deleted file mode 100644 index f653eef7ec..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_005.py +++ /dev/null @@ -1,37 +0,0 @@ -import pytest # (1)! -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine -from sqlmodel.pool import StaticPool - -from .main import app, get_session - - -@pytest.fixture(name="session") # (2)! -def session_fixture(): # (3)! - engine = create_engine( - "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool - ) - SQLModel.metadata.create_all(engine) - with Session(engine) as session: - yield session # (4)! - - -def test_create_hero(session: Session): # (5)! - def get_session_override(): - return session # (6)! - - app.dependency_overrides[get_session] = get_session_override - - client = TestClient(app) - - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_006.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_006.py deleted file mode 100644 index 8dbfd45caf..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py310/test_main_006.py +++ /dev/null @@ -1,41 +0,0 @@ -import pytest -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine -from sqlmodel.pool import StaticPool - -from .main import app, get_session - - -@pytest.fixture(name="session") -def session_fixture(): - engine = create_engine( - "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool - ) - SQLModel.metadata.create_all(engine) - with Session(engine) as session: - yield session - - -@pytest.fixture(name="client") # (1)! -def client_fixture(session: Session): # (2)! - def get_session_override(): # (3)! - return session - - app.dependency_overrides[get_session] = get_session_override # (4)! - - client = TestClient(app) # (5)! - yield client # (6)! - app.dependency_overrides.clear() # (7)! - - -def test_create_hero(client: TestClient): # (8)! - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/__init__.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_001.md b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_001.md deleted file mode 100644 index 936b84b92d..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_001.md +++ /dev/null @@ -1,17 +0,0 @@ -1. Import the `app` from the the `main` module. - -2. We create a `TestClient` for the FastAPI `app` and put it in the variable `client`. - -3. Then we use use this `client` to **talk to the API** and send a `POST` HTTP operation, creating a new hero. - -4. Then we get the **JSON data** from the response and put it in the variable `data`. - -5. Next we start testing the results with `assert` statements, we check that the status code of the response is `200`. - -6. We check that the `name` of the hero created is `"Deadpond"`. - -7. We check that the `secret_name` of the hero created is `"Dive Wilson"`. - -8. We check that the `age` of the hero created is `None`, because we didn't send an age. - -9. We check that the hero created has an `id` created by the database, so it's not `None`. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_002.md b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_002.md deleted file mode 100644 index 0f8555a8dd..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_002.md +++ /dev/null @@ -1,25 +0,0 @@ -1. Import the `get_session` dependency from the the `main` module. - -2. Define the new function that will be the new **dependency override**. - -3. This function will return a different **session** than the one that would be returned by the original `get_session` function. - - We haven't seen how this new **session** object is created yet, but the point is that this is a different session than the original one from the app. - - This session is attached to a different **engine**, and that different **engine** uses a different URL, for a database just for testing. - - We haven't defined that new **URL** nor the new **engine** yet, but here we already see the that this object `session` will override the one returned by the original dependency `get_session()`. - -4. Then, the FastAPI `app` object has an attribute `app.dependency_overrides`. - - This attribute is a dictionary, and we can put dependency overrides in it by passing, as the **key**, the **original dependency function**, and as the **value**, the **new overriding dependency function**. - - So, here we are telling the FastAPI app to use `get_session_override` instead of `get_session` in all the places in the code that depend on `get_session`, that is, all the parameters with something like: - - ```Python - session: Session = Depends(get_session) - ``` - -5. After we are done with the dependency override, we can restore the application back to normal, by removing all the values in this dictionary `app.dependency_overrides`. - - This way whenever a *path operation function* needs the dependency FastAPI will use the original one instead of the override. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_003.md b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_003.md deleted file mode 100644 index 2b48ebdacf..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_003.md +++ /dev/null @@ -1,37 +0,0 @@ -1. Here's a subtle thing to notice. - - Remember that [Order Matters](../create-db-and-table.md#sqlmodel-metadata-order-matters){.internal-link target=_blank} and we need to make sure all the **SQLModel** models are already defined and **imported** before calling `.create_all()`. - - IN this line, by importing something, *anything*, from `.main`, the code in `.main` will be executed, including the definition of the **table models**, and that will automatically register them in `SQLModel.metadata`. - -2. Here we create a new **engine**, completely different from the one in `main.py`. - - This is the engine we will use for the tests. - - We use the new URL of the database for tests: - - ``` - sqlite:///testing.db - ``` - - And again, we use the connection argument `check_same_thread=False`. - -3. Then we call: - - ```Python - SQLModel.metadata.create_all(engine) - ``` - - ...to make sure we create all the tables in the new testing database. - - The **table models** are registered in `SQLModel.metadata` just because we imported *something* from `.main`, and the code in `.main` was executed, creating the classes for the **table models** and automatically registering them in `SQLModel.metadata`. - - So, by the point we call this method, the **table models** are already registered there. πŸ’― - -4. Here's where we create the custom **session** object for this test in a `with` block. - - It uses the new custom **engine** we created, so anything that uses this session will be using the testing database. - -5. Now, back to the dependency override, it is just returning the same **session** object from outside, that's it, that's the whole trick. - -6. By this point, the testing **session** `with` block finishes, and the session is closed, the file is closed, etc. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_004.md b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_004.md deleted file mode 100644 index de754c5e76..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_004.md +++ /dev/null @@ -1,29 +0,0 @@ -1. Import `StaticPool` from `sqlmodel`, we will use it in a bit. - -2. For the **SQLite URL**, don't write any file name, leave it empty. - - So, instead of: - - ``` - sqlite:///testing.db - ``` - - ...just write: - - ``` - sqlite:// - ``` - - This is enough to tell **SQLModel** (actually SQLAlchemy) that we want to use an **in-memory SQLite database**. - -3. Remember that we told the **low-level** library in charge of communicating with SQLite that we want to be able to **access the database from different threads** with `check_same_thread=False`? - - Now that we use an **in-memory database**, we need to also tell SQLAlchemy that we want to be able to use the **same in-memory database** object from different threads. - - We tell it that with the `poolclass=StaticPool` parameter. - - /// info - - You can read more details in the SQLAlchemy documentation about Using a Memory Database in Multiple Threads - - /// diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_005.md b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_005.md deleted file mode 100644 index 126e1f1790..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_005.md +++ /dev/null @@ -1,41 +0,0 @@ -1. Import `pytest`. - -2. Use the `@pytest.fixture()` decorator on top of the function to tell pytest that this is a **fixture** function (equivalent to a FastAPI dependency). - - We also give it a name of `"session"`, this will be important in the testing function. - -3. Create the fixture function. This is equivalent to a FastAPI dependency function. - - In this fixture we create the custom **engine**, with the in-memory database, we create the tables, and we create the **session**. - - Then we `yield` the `session` object. - -4. The thing that we `return` or `yield` is what will be available to the test function, in this case, the `session` object. - - Here we use `yield` so that **pytest** comes back to execute "the rest of the code" in this function once the testing function is done. - - We don't have any more visible "rest of the code" after the `yield`, but we have the end of the `with` block that will close the **session**. - - By using `yield`, pytest will: - - * run the first part - * create the **session** object - * give it to the test function - * run the test function - * once the test function is done, it will continue here, right after the `yield`, and will correctly close the **session** object in the end of the `with` block. - -5. Now, in the test function, to tell **pytest** that this test wants to get the fixture, instead of declaring something like in FastAPI with: - - ```Python - session: Session = Depends(session_fixture) - ``` - - ...the way we tell pytest what is the fixture that we want is by using the **exact same name** of the fixture. - - In this case, we named it `session`, so the parameter has to be exactly named `session` for it to work. - - We also add the type annotation `session: Session` so that we can get autocompletion and inline error checks in our editor. - -6. Now in the dependency override function, we just return the same `session` object that came from outside it. - - The `session` object comes from the parameter passed to the test function, and we just re-use it and return it here in the dependency override. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_006.md b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_006.md deleted file mode 100644 index d44a3b67da..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/annotations/en/test_main_006.md +++ /dev/null @@ -1,23 +0,0 @@ -1. Create the new fixture named `"client"`. - -2. This **client fixture**, in turn, also requires the **session fixture**. - -3. Now we create the **dependency override** inside the client fixture. - -4. Set the **dependency override** in the `app.dependency_overrides` dictionary. - -5. Create the `TestClient` with the **FastAPI** `app`. - -6. `yield` the `TestClient` instance. - - By using `yield`, after the test function is done, pytest will come back to execute the rest of the code after `yield`. - -7. This is the cleanup code, after `yield`, and after the test function is done. - - Here we clear the dependency overrides (here it's only one) in the FastAPI `app`. - -8. Now the test function requires the **client fixture**. - - And inside the test function, the code is quite **simple**, we just use the `TestClient` to make requests to the API, check the data, and that's it. - - The fixtures take care of all the **setup** and **cleanup** code. diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/main.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/main.py deleted file mode 100644 index e7371d84e3..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/main.py +++ /dev/null @@ -1,105 +0,0 @@ -from typing import Optional - -from fastapi import Depends, FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def get_session(): - with Session(engine) as session: - yield session - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero( - *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate -): - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_extra_coverage.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_extra_coverage.py deleted file mode 100644 index 1d8153ab9f..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_extra_coverage.py +++ /dev/null @@ -1,38 +0,0 @@ -from fastapi.testclient import TestClient -from sqlalchemy import Inspector, inspect -from sqlmodel import Session, create_engine - -from . import main as app_mod -from .test_main import client_fixture, session_fixture - -assert client_fixture, "This keeps the client fixture used below" -assert session_fixture, "This keeps the session fixture used by client_fixture" - - -def test_startup(): - app_mod.engine = create_engine("sqlite://") - app_mod.on_startup() - insp: Inspector = inspect(app_mod.engine) - assert insp.has_table(str(app_mod.Hero.__tablename__)) - - -def test_get_session(): - app_mod.engine = create_engine("sqlite://") - for session in app_mod.get_session(): - assert isinstance(session, Session) - assert session.bind == app_mod.engine - - -def test_read_hero_not_found(client: TestClient): - response = client.get("/heroes/9000") - assert response.status_code == 404 - - -def test_update_hero_not_found(client: TestClient): - response = client.patch("/heroes/9000", json={"name": "Very-Rusty-Man"}) - assert response.status_code == 404 - - -def test_delete_hero_not_found(client: TestClient): - response = client.delete("/heroes/9000") - assert response.status_code == 404 diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main.py deleted file mode 100644 index 435787c79b..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main.py +++ /dev/null @@ -1,125 +0,0 @@ -import pytest -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine -from sqlmodel.pool import StaticPool - -from .main import Hero, app, get_session - - -@pytest.fixture(name="session") -def session_fixture(): - engine = create_engine( - "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool - ) - SQLModel.metadata.create_all(engine) - with Session(engine) as session: - yield session - - -@pytest.fixture(name="client") -def client_fixture(session: Session): - def get_session_override(): - return session - - app.dependency_overrides[get_session] = get_session_override - client = TestClient(app) - yield client - app.dependency_overrides.clear() - - -def test_create_hero(client: TestClient): - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None - - -def test_create_hero_incomplete(client: TestClient): - # No secret_name - response = client.post("/heroes/", json={"name": "Deadpond"}) - assert response.status_code == 422 - - -def test_create_hero_invalid(client: TestClient): - # secret_name has an invalid type - response = client.post( - "/heroes/", - json={ - "name": "Deadpond", - "secret_name": {"message": "Do you wanna know my secret identity?"}, - }, - ) - assert response.status_code == 422 - - -def test_read_heroes(session: Session, client: TestClient): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") - hero_2 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) - session.add(hero_1) - session.add(hero_2) - session.commit() - - response = client.get("/heroes/") - data = response.json() - - assert response.status_code == 200 - - assert len(data) == 2 - assert data[0]["name"] == hero_1.name - assert data[0]["secret_name"] == hero_1.secret_name - assert data[0]["age"] == hero_1.age - assert data[0]["id"] == hero_1.id - assert data[1]["name"] == hero_2.name - assert data[1]["secret_name"] == hero_2.secret_name - assert data[1]["age"] == hero_2.age - assert data[1]["id"] == hero_2.id - - -def test_read_hero(session: Session, client: TestClient): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") - session.add(hero_1) - session.commit() - - response = client.get(f"/heroes/{hero_1.id}") - data = response.json() - - assert response.status_code == 200 - assert data["name"] == hero_1.name - assert data["secret_name"] == hero_1.secret_name - assert data["age"] == hero_1.age - assert data["id"] == hero_1.id - - -def test_update_hero(session: Session, client: TestClient): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") - session.add(hero_1) - session.commit() - - response = client.patch(f"/heroes/{hero_1.id}", json={"name": "Deadpuddle"}) - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpuddle" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] == hero_1.id - - -def test_delete_hero(session: Session, client: TestClient): - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") - session.add(hero_1) - session.commit() - - response = client.delete(f"/heroes/{hero_1.id}") - - hero_in_db = session.get(Hero, hero_1.id) - - assert response.status_code == 200 - - assert hero_in_db is None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_001.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_001.py deleted file mode 100644 index 3ae40773f9..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_001.py +++ /dev/null @@ -1,32 +0,0 @@ -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine - -from .main import app, get_session # (1)! - - -def test_create_hero(): - engine = create_engine( - "sqlite:///testing.db", connect_args={"check_same_thread": False} - ) - SQLModel.metadata.create_all(engine) - - with Session(engine) as session: - - def get_session_override(): - return session - - app.dependency_overrides[get_session] = get_session_override - - client = TestClient(app) # (2)! - - response = client.post( # (3)! - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() - data = response.json() # (4)! - - assert response.status_code == 200 # (5)! - assert data["name"] == "Deadpond" # (6)! - assert data["secret_name"] == "Dive Wilson" # (7)! - assert data["age"] is None # (8)! - assert data["id"] is not None # (9)! diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_002.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_002.py deleted file mode 100644 index 727580b68f..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_002.py +++ /dev/null @@ -1,32 +0,0 @@ -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine - -from .main import app, get_session # (1)! - - -def test_create_hero(): - engine = create_engine( - "sqlite:///testing.db", connect_args={"check_same_thread": False} - ) - SQLModel.metadata.create_all(engine) - - with Session(engine) as session: - - def get_session_override(): # (2)! - return session # (3)! - - app.dependency_overrides[get_session] = get_session_override # (4)! - - client = TestClient(app) - - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() # (5)! - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_003.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_003.py deleted file mode 100644 index 465c525108..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_003.py +++ /dev/null @@ -1,33 +0,0 @@ -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine - -from .main import app, get_session # (1)! - - -def test_create_hero(): - engine = create_engine( # (2)! - "sqlite:///testing.db", connect_args={"check_same_thread": False} - ) - SQLModel.metadata.create_all(engine) # (3)! - - with Session(engine) as session: # (4)! - - def get_session_override(): - return session # (5)! - - app.dependency_overrides[get_session] = get_session_override # (4)! - - client = TestClient(app) - - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None - # (6)! diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_004.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_004.py deleted file mode 100644 index b770a9aa59..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_004.py +++ /dev/null @@ -1,35 +0,0 @@ -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine -from sqlmodel.pool import StaticPool # (1)! - -from .main import app, get_session - - -def test_create_hero(): - engine = create_engine( - "sqlite://", # (2)! - connect_args={"check_same_thread": False}, - poolclass=StaticPool, # (3)! - ) - SQLModel.metadata.create_all(engine) - - with Session(engine) as session: - - def get_session_override(): - return session - - app.dependency_overrides[get_session] = get_session_override - - client = TestClient(app) - - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_005.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_005.py deleted file mode 100644 index f653eef7ec..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_005.py +++ /dev/null @@ -1,37 +0,0 @@ -import pytest # (1)! -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine -from sqlmodel.pool import StaticPool - -from .main import app, get_session - - -@pytest.fixture(name="session") # (2)! -def session_fixture(): # (3)! - engine = create_engine( - "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool - ) - SQLModel.metadata.create_all(engine) - with Session(engine) as session: - yield session # (4)! - - -def test_create_hero(session: Session): # (5)! - def get_session_override(): - return session # (6)! - - app.dependency_overrides[get_session] = get_session_override - - client = TestClient(app) - - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - app.dependency_overrides.clear() - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None diff --git a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_006.py b/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_006.py deleted file mode 100644 index 8dbfd45caf..0000000000 --- a/docs_src/tutorial/fastapi/app_testing/tutorial001_py39/test_main_006.py +++ /dev/null @@ -1,41 +0,0 @@ -import pytest -from fastapi.testclient import TestClient -from sqlmodel import Session, SQLModel, create_engine -from sqlmodel.pool import StaticPool - -from .main import app, get_session - - -@pytest.fixture(name="session") -def session_fixture(): - engine = create_engine( - "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool - ) - SQLModel.metadata.create_all(engine) - with Session(engine) as session: - yield session - - -@pytest.fixture(name="client") # (1)! -def client_fixture(session: Session): # (2)! - def get_session_override(): # (3)! - return session - - app.dependency_overrides[get_session] = get_session_override # (4)! - - client = TestClient(app) # (5)! - yield client # (6)! - app.dependency_overrides.clear() # (7)! - - -def test_create_hero(client: TestClient): # (8)! - response = client.post( - "/heroes/", json={"name": "Deadpond", "secret_name": "Dive Wilson"} - ) - data = response.json() - - assert response.status_code == 200 - assert data["name"] == "Deadpond" - assert data["secret_name"] == "Dive Wilson" - assert data["age"] is None - assert data["id"] is not None diff --git a/docs_src/tutorial/fastapi/delete/__init__.py b/docs_src/tutorial/fastapi/delete/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/delete/tutorial001.py b/docs_src/tutorial/fastapi/delete/tutorial001.py deleted file mode 100644 index 977882c4c2..0000000000 --- a/docs_src/tutorial/fastapi/delete/tutorial001.py +++ /dev/null @@ -1,98 +0,0 @@ -from typing import List, Optional - -from fastapi import FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=List[HeroPublic]) -def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): - with Session(engine) as session: - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero(hero_id: int, hero: HeroUpdate): - with Session(engine) as session: - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/delete/tutorial001_py310.py b/docs_src/tutorial/fastapi/delete/tutorial001_py310.py deleted file mode 100644 index f7de4019d2..0000000000 --- a/docs_src/tutorial/fastapi/delete/tutorial001_py310.py +++ /dev/null @@ -1,96 +0,0 @@ -from fastapi import FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: int | None = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: str | None = None - secret_name: str | None = None - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): - with Session(engine) as session: - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero(hero_id: int, hero: HeroUpdate): - with Session(engine) as session: - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/delete/tutorial001_py39.py b/docs_src/tutorial/fastapi/delete/tutorial001_py39.py deleted file mode 100644 index 5d5f099abb..0000000000 --- a/docs_src/tutorial/fastapi/delete/tutorial001_py39.py +++ /dev/null @@ -1,98 +0,0 @@ -from typing import Optional - -from fastapi import FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): - with Session(engine) as session: - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero(hero_id: int, hero: HeroUpdate): - with Session(engine) as session: - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/limit_and_offset/__init__.py b/docs_src/tutorial/fastapi/limit_and_offset/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/limit_and_offset/tutorial001.py b/docs_src/tutorial/fastapi/limit_and_offset/tutorial001.py deleted file mode 100644 index ccf3d77036..0000000000 --- a/docs_src/tutorial/fastapi/limit_and_offset/tutorial001.py +++ /dev/null @@ -1,67 +0,0 @@ -from typing import List, Optional - -from fastapi import FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=List[HeroPublic]) -def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): - with Session(engine) as session: - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero diff --git a/docs_src/tutorial/fastapi/limit_and_offset/tutorial001_py310.py b/docs_src/tutorial/fastapi/limit_and_offset/tutorial001_py310.py deleted file mode 100644 index 3402d4045e..0000000000 --- a/docs_src/tutorial/fastapi/limit_and_offset/tutorial001_py310.py +++ /dev/null @@ -1,65 +0,0 @@ -from fastapi import FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: int | None = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): - with Session(engine) as session: - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero diff --git a/docs_src/tutorial/fastapi/limit_and_offset/tutorial001_py39.py b/docs_src/tutorial/fastapi/limit_and_offset/tutorial001_py39.py deleted file mode 100644 index 3d223f3290..0000000000 --- a/docs_src/tutorial/fastapi/limit_and_offset/tutorial001_py39.py +++ /dev/null @@ -1,67 +0,0 @@ -from typing import Optional - -from fastapi import FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): - with Session(engine) as session: - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero diff --git a/docs_src/tutorial/fastapi/multiple_models/__init__.py b/docs_src/tutorial/fastapi/multiple_models/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/multiple_models/tutorial001.py b/docs_src/tutorial/fastapi/multiple_models/tutorial001.py deleted file mode 100644 index 42ac8051b1..0000000000 --- a/docs_src/tutorial/fastapi/multiple_models/tutorial001.py +++ /dev/null @@ -1,60 +0,0 @@ -from typing import List, Optional - -from fastapi import FastAPI -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class HeroCreate(SQLModel): - name: str - secret_name: str - age: Optional[int] = None - - -class HeroPublic(SQLModel): - id: int - name: str - secret_name: str - age: Optional[int] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=List[HeroPublic]) -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes diff --git a/docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py b/docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py deleted file mode 100644 index b8dc44d981..0000000000 --- a/docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py +++ /dev/null @@ -1,58 +0,0 @@ -from fastapi import FastAPI -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -class HeroCreate(SQLModel): - name: str - secret_name: str - age: int | None = None - - -class HeroPublic(SQLModel): - id: int - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes diff --git a/docs_src/tutorial/fastapi/multiple_models/tutorial001_py39.py b/docs_src/tutorial/fastapi/multiple_models/tutorial001_py39.py deleted file mode 100644 index c6be23d441..0000000000 --- a/docs_src/tutorial/fastapi/multiple_models/tutorial001_py39.py +++ /dev/null @@ -1,60 +0,0 @@ -from typing import Optional - -from fastapi import FastAPI -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class HeroCreate(SQLModel): - name: str - secret_name: str - age: Optional[int] = None - - -class HeroPublic(SQLModel): - id: int - name: str - secret_name: str - age: Optional[int] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes diff --git a/docs_src/tutorial/fastapi/multiple_models/tutorial002.py b/docs_src/tutorial/fastapi/multiple_models/tutorial002.py deleted file mode 100644 index 79c71f1a2e..0000000000 --- a/docs_src/tutorial/fastapi/multiple_models/tutorial002.py +++ /dev/null @@ -1,58 +0,0 @@ -from typing import List, Optional - -from fastapi import FastAPI -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=List[HeroPublic]) -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes diff --git a/docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py b/docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py deleted file mode 100644 index 79e7447b53..0000000000 --- a/docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py +++ /dev/null @@ -1,56 +0,0 @@ -from fastapi import FastAPI -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: int | None = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes diff --git a/docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py b/docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py deleted file mode 100644 index 77093bcc7c..0000000000 --- a/docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py +++ /dev/null @@ -1,58 +0,0 @@ -from typing import Optional - -from fastapi import FastAPI -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes diff --git a/docs_src/tutorial/fastapi/read_one/__init__.py b/docs_src/tutorial/fastapi/read_one/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/read_one/tutorial001.py b/docs_src/tutorial/fastapi/read_one/tutorial001.py deleted file mode 100644 index a39945173b..0000000000 --- a/docs_src/tutorial/fastapi/read_one/tutorial001.py +++ /dev/null @@ -1,67 +0,0 @@ -from typing import List, Optional - -from fastapi import FastAPI, HTTPException -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=List[HeroPublic]) -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero diff --git a/docs_src/tutorial/fastapi/read_one/tutorial001_py310.py b/docs_src/tutorial/fastapi/read_one/tutorial001_py310.py deleted file mode 100644 index 1a4628137c..0000000000 --- a/docs_src/tutorial/fastapi/read_one/tutorial001_py310.py +++ /dev/null @@ -1,65 +0,0 @@ -from fastapi import FastAPI, HTTPException -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: int | None = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero diff --git a/docs_src/tutorial/fastapi/read_one/tutorial001_py39.py b/docs_src/tutorial/fastapi/read_one/tutorial001_py39.py deleted file mode 100644 index 9ac0a65088..0000000000 --- a/docs_src/tutorial/fastapi/read_one/tutorial001_py39.py +++ /dev/null @@ -1,67 +0,0 @@ -from typing import Optional - -from fastapi import FastAPI, HTTPException -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero diff --git a/docs_src/tutorial/fastapi/relationships/__init__.py b/docs_src/tutorial/fastapi/relationships/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/relationships/tutorial001.py b/docs_src/tutorial/fastapi/relationships/tutorial001.py deleted file mode 100644 index 59b44730ba..0000000000 --- a/docs_src/tutorial/fastapi/relationships/tutorial001.py +++ /dev/null @@ -1,199 +0,0 @@ -from typing import List, Optional - -from fastapi import Depends, FastAPI, HTTPException, Query -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class TeamBase(SQLModel): - name: str = Field(index=True) - headquarters: str - - -class Team(TeamBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - heroes: List["Hero"] = Relationship(back_populates="team") - - -class TeamCreate(TeamBase): - pass - - -class TeamPublic(TeamBase): - id: int - - -class TeamUpdate(SQLModel): - id: Optional[int] = None - name: Optional[str] = None - headquarters: Optional[str] = None - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - team: Optional[Team] = Relationship(back_populates="heroes") - - -class HeroPublic(HeroBase): - id: int - - -class HeroCreate(HeroBase): - pass - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - team_id: Optional[int] = None - - -class HeroPublicWithTeam(HeroPublic): - team: Optional[TeamPublic] = None - - -class TeamPublicWithHeroes(TeamPublic): - heroes: List[HeroPublic] = [] - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def get_session(): - with Session(engine) as session: - yield session - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=List[HeroPublic]) -def read_heroes( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublicWithTeam) -def read_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero( - *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate -): - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} - - -@app.post("/teams/", response_model=TeamPublic) -def create_team(*, session: Session = Depends(get_session), team: TeamCreate): - db_team = Team.model_validate(team) - session.add(db_team) - session.commit() - session.refresh(db_team) - return db_team - - -@app.get("/teams/", response_model=List[TeamPublic]) -def read_teams( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - teams = session.exec(select(Team).offset(offset).limit(limit)).all() - return teams - - -@app.get("/teams/{team_id}", response_model=TeamPublicWithHeroes) -def read_team(*, team_id: int, session: Session = Depends(get_session)): - team = session.get(Team, team_id) - if not team: - raise HTTPException(status_code=404, detail="Team not found") - return team - - -@app.patch("/teams/{team_id}", response_model=TeamPublic) -def update_team( - *, - session: Session = Depends(get_session), - team_id: int, - team: TeamUpdate, -): - db_team = session.get(Team, team_id) - if not db_team: - raise HTTPException(status_code=404, detail="Team not found") - team_data = team.model_dump(exclude_unset=True) - db_team.sqlmodel_update(team_data) - session.add(db_team) - session.commit() - session.refresh(db_team) - return db_team - - -@app.delete("/teams/{team_id}") -def delete_team(*, session: Session = Depends(get_session), team_id: int): - team = session.get(Team, team_id) - if not team: - raise HTTPException(status_code=404, detail="Team not found") - session.delete(team) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/relationships/tutorial001_py310.py b/docs_src/tutorial/fastapi/relationships/tutorial001_py310.py deleted file mode 100644 index 47c2e5f0bc..0000000000 --- a/docs_src/tutorial/fastapi/relationships/tutorial001_py310.py +++ /dev/null @@ -1,197 +0,0 @@ -from fastapi import Depends, FastAPI, HTTPException, Query -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class TeamBase(SQLModel): - name: str = Field(index=True) - headquarters: str - - -class Team(TeamBase, table=True): - id: int | None = Field(default=None, primary_key=True) - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class TeamCreate(TeamBase): - pass - - -class TeamPublic(TeamBase): - id: int - - -class TeamUpdate(SQLModel): - id: int | None = None - name: str | None = None - headquarters: str | None = None - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - - -class Hero(HeroBase, table=True): - id: int | None = Field(default=None, primary_key=True) - - team: Team | None = Relationship(back_populates="heroes") - - -class HeroPublic(HeroBase): - id: int - - -class HeroCreate(HeroBase): - pass - - -class HeroUpdate(SQLModel): - name: str | None = None - secret_name: str | None = None - age: int | None = None - team_id: int | None = None - - -class HeroPublicWithTeam(HeroPublic): - team: TeamPublic | None = None - - -class TeamPublicWithHeroes(TeamPublic): - heroes: list[HeroPublic] = [] - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def get_session(): - with Session(engine) as session: - yield session - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublicWithTeam) -def read_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero( - *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate -): - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} - - -@app.post("/teams/", response_model=TeamPublic) -def create_team(*, session: Session = Depends(get_session), team: TeamCreate): - db_team = Team.model_validate(team) - session.add(db_team) - session.commit() - session.refresh(db_team) - return db_team - - -@app.get("/teams/", response_model=list[TeamPublic]) -def read_teams( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - teams = session.exec(select(Team).offset(offset).limit(limit)).all() - return teams - - -@app.get("/teams/{team_id}", response_model=TeamPublicWithHeroes) -def read_team(*, team_id: int, session: Session = Depends(get_session)): - team = session.get(Team, team_id) - if not team: - raise HTTPException(status_code=404, detail="Team not found") - return team - - -@app.patch("/teams/{team_id}", response_model=TeamPublic) -def update_team( - *, - session: Session = Depends(get_session), - team_id: int, - team: TeamUpdate, -): - db_team = session.get(Team, team_id) - if not db_team: - raise HTTPException(status_code=404, detail="Team not found") - team_data = team.model_dump(exclude_unset=True) - db_team.sqlmodel_update(team_data) - session.add(db_team) - session.commit() - session.refresh(db_team) - return db_team - - -@app.delete("/teams/{team_id}") -def delete_team(*, session: Session = Depends(get_session), team_id: int): - team = session.get(Team, team_id) - if not team: - raise HTTPException(status_code=404, detail="Team not found") - session.delete(team) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/relationships/tutorial001_py39.py b/docs_src/tutorial/fastapi/relationships/tutorial001_py39.py deleted file mode 100644 index 1cfa298b8b..0000000000 --- a/docs_src/tutorial/fastapi/relationships/tutorial001_py39.py +++ /dev/null @@ -1,199 +0,0 @@ -from typing import Optional - -from fastapi import Depends, FastAPI, HTTPException, Query -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class TeamBase(SQLModel): - name: str = Field(index=True) - headquarters: str - - -class Team(TeamBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class TeamCreate(TeamBase): - pass - - -class TeamPublic(TeamBase): - id: int - - -class TeamUpdate(SQLModel): - id: Optional[int] = None - name: Optional[str] = None - headquarters: Optional[str] = None - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - team: Optional[Team] = Relationship(back_populates="heroes") - - -class HeroPublic(HeroBase): - id: int - - -class HeroCreate(HeroBase): - pass - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - team_id: Optional[int] = None - - -class HeroPublicWithTeam(HeroPublic): - team: Optional[TeamPublic] = None - - -class TeamPublicWithHeroes(TeamPublic): - heroes: list[HeroPublic] = [] - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def get_session(): - with Session(engine) as session: - yield session - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublicWithTeam) -def read_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero( - *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate -): - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} - - -@app.post("/teams/", response_model=TeamPublic) -def create_team(*, session: Session = Depends(get_session), team: TeamCreate): - db_team = Team.model_validate(team) - session.add(db_team) - session.commit() - session.refresh(db_team) - return db_team - - -@app.get("/teams/", response_model=list[TeamPublic]) -def read_teams( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - teams = session.exec(select(Team).offset(offset).limit(limit)).all() - return teams - - -@app.get("/teams/{team_id}", response_model=TeamPublicWithHeroes) -def read_team(*, team_id: int, session: Session = Depends(get_session)): - team = session.get(Team, team_id) - if not team: - raise HTTPException(status_code=404, detail="Team not found") - return team - - -@app.patch("/teams/{team_id}", response_model=TeamPublic) -def update_team( - *, - session: Session = Depends(get_session), - team_id: int, - team: TeamUpdate, -): - db_team = session.get(Team, team_id) - if not db_team: - raise HTTPException(status_code=404, detail="Team not found") - team_data = team.model_dump(exclude_unset=True) - db_team.sqlmodel_update(team_data) - session.add(db_team) - session.commit() - session.refresh(db_team) - return db_team - - -@app.delete("/teams/{team_id}") -def delete_team(*, session: Session = Depends(get_session), team_id: int): - team = session.get(Team, team_id) - if not team: - raise HTTPException(status_code=404, detail="Team not found") - session.delete(team) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/response_model/__init__.py b/docs_src/tutorial/fastapi/response_model/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/response_model/tutorial001.py b/docs_src/tutorial/fastapi/response_model/tutorial001.py deleted file mode 100644 index 57d8738395..0000000000 --- a/docs_src/tutorial/fastapi/response_model/tutorial001.py +++ /dev/null @@ -1,46 +0,0 @@ -from typing import List, Optional - -from fastapi import FastAPI -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=Hero) -def create_hero(hero: Hero): - with Session(engine) as session: - session.add(hero) - session.commit() - session.refresh(hero) - return hero - - -@app.get("/heroes/", response_model=List[Hero]) -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes diff --git a/docs_src/tutorial/fastapi/response_model/tutorial001_py310.py b/docs_src/tutorial/fastapi/response_model/tutorial001_py310.py deleted file mode 100644 index 25825b41ec..0000000000 --- a/docs_src/tutorial/fastapi/response_model/tutorial001_py310.py +++ /dev/null @@ -1,44 +0,0 @@ -from fastapi import FastAPI -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=Hero) -def create_hero(hero: Hero): - with Session(engine) as session: - session.add(hero) - session.commit() - session.refresh(hero) - return hero - - -@app.get("/heroes/", response_model=list[Hero]) -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes diff --git a/docs_src/tutorial/fastapi/response_model/tutorial001_py39.py b/docs_src/tutorial/fastapi/response_model/tutorial001_py39.py deleted file mode 100644 index 53b701deb1..0000000000 --- a/docs_src/tutorial/fastapi/response_model/tutorial001_py39.py +++ /dev/null @@ -1,46 +0,0 @@ -from typing import Optional - -from fastapi import FastAPI -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=Hero) -def create_hero(hero: Hero): - with Session(engine) as session: - session.add(hero) - session.commit() - session.refresh(hero) - return hero - - -@app.get("/heroes/", response_model=list[Hero]) -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes diff --git a/docs_src/tutorial/fastapi/session_with_dependency/__init__.py b/docs_src/tutorial/fastapi/session_with_dependency/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py b/docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py deleted file mode 100644 index f0a2559467..0000000000 --- a/docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py +++ /dev/null @@ -1,105 +0,0 @@ -from typing import List, Optional - -from fastapi import Depends, FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def get_session(): - with Session(engine) as session: - yield session - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=List[HeroPublic]) -def read_heroes( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero( - *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate -): - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py b/docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py deleted file mode 100644 index 84da9fd610..0000000000 --- a/docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py310.py +++ /dev/null @@ -1,103 +0,0 @@ -from fastapi import Depends, FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: int | None = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: str | None = None - secret_name: str | None = None - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def get_session(): - with Session(engine) as session: - yield session - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero( - *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate -): - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py b/docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py deleted file mode 100644 index e7371d84e3..0000000000 --- a/docs_src/tutorial/fastapi/session_with_dependency/tutorial001_py39.py +++ /dev/null @@ -1,105 +0,0 @@ -from typing import Optional - -from fastapi import Depends, FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def get_session(): - with Session(engine) as session: - yield session - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero( - *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate -): - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/simple_hero_api/__init__.py b/docs_src/tutorial/fastapi/simple_hero_api/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py b/docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py deleted file mode 100644 index 41eaa621d3..0000000000 --- a/docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py +++ /dev/null @@ -1,46 +0,0 @@ -from typing import Optional - -from fastapi import FastAPI -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/") -def create_hero(hero: Hero): - with Session(engine) as session: - session.add(hero) - session.commit() - session.refresh(hero) - return hero - - -@app.get("/heroes/") -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes diff --git a/docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py b/docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py deleted file mode 100644 index 0e113b0f16..0000000000 --- a/docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py +++ /dev/null @@ -1,44 +0,0 @@ -from fastapi import FastAPI -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/") -def create_hero(hero: Hero): - with Session(engine) as session: - session.add(hero) - session.commit() - session.refresh(hero) - return hero - - -@app.get("/heroes/") -def read_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - return heroes diff --git a/docs_src/tutorial/fastapi/teams/__init__.py b/docs_src/tutorial/fastapi/teams/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/teams/tutorial001.py b/docs_src/tutorial/fastapi/teams/tutorial001.py deleted file mode 100644 index 49dd83065a..0000000000 --- a/docs_src/tutorial/fastapi/teams/tutorial001.py +++ /dev/null @@ -1,190 +0,0 @@ -from typing import List, Optional - -from fastapi import Depends, FastAPI, HTTPException, Query -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class TeamBase(SQLModel): - name: str = Field(index=True) - headquarters: str - - -class Team(TeamBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - heroes: List["Hero"] = Relationship(back_populates="team") - - -class TeamCreate(TeamBase): - pass - - -class TeamPublic(TeamBase): - id: int - - -class TeamUpdate(SQLModel): - name: Optional[str] = None - headquarters: Optional[str] = None - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - team: Optional[Team] = Relationship(back_populates="heroes") - - -class HeroPublic(HeroBase): - id: int - - -class HeroCreate(HeroBase): - pass - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - team_id: Optional[int] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def get_session(): - with Session(engine) as session: - yield session - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=List[HeroPublic]) -def read_heroes( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero( - *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate -): - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} - - -@app.post("/teams/", response_model=TeamPublic) -def create_team(*, session: Session = Depends(get_session), team: TeamCreate): - db_team = Team.model_validate(team) - session.add(db_team) - session.commit() - session.refresh(db_team) - return db_team - - -@app.get("/teams/", response_model=List[TeamPublic]) -def read_teams( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - teams = session.exec(select(Team).offset(offset).limit(limit)).all() - return teams - - -@app.get("/teams/{team_id}", response_model=TeamPublic) -def read_team(*, team_id: int, session: Session = Depends(get_session)): - team = session.get(Team, team_id) - if not team: - raise HTTPException(status_code=404, detail="Team not found") - return team - - -@app.patch("/teams/{team_id}", response_model=TeamPublic) -def update_team( - *, - session: Session = Depends(get_session), - team_id: int, - team: TeamUpdate, -): - db_team = session.get(Team, team_id) - if not db_team: - raise HTTPException(status_code=404, detail="Team not found") - team_data = team.model_dump(exclude_unset=True) - db_team.sqlmodel_update(team_data) - session.add(db_team) - session.commit() - session.refresh(db_team) - return db_team - - -@app.delete("/teams/{team_id}") -def delete_team(*, session: Session = Depends(get_session), team_id: int): - team = session.get(Team, team_id) - if not team: - raise HTTPException(status_code=404, detail="Team not found") - session.delete(team) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/teams/tutorial001_py310.py b/docs_src/tutorial/fastapi/teams/tutorial001_py310.py deleted file mode 100644 index b78f059e12..0000000000 --- a/docs_src/tutorial/fastapi/teams/tutorial001_py310.py +++ /dev/null @@ -1,188 +0,0 @@ -from fastapi import Depends, FastAPI, HTTPException, Query -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class TeamBase(SQLModel): - name: str = Field(index=True) - headquarters: str - - -class Team(TeamBase, table=True): - id: int | None = Field(default=None, primary_key=True) - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class TeamCreate(TeamBase): - pass - - -class TeamPublic(TeamBase): - id: int - - -class TeamUpdate(SQLModel): - name: str | None = None - headquarters: str | None = None - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - - -class Hero(HeroBase, table=True): - id: int | None = Field(default=None, primary_key=True) - - team: Team | None = Relationship(back_populates="heroes") - - -class HeroPublic(HeroBase): - id: int - - -class HeroCreate(HeroBase): - pass - - -class HeroUpdate(SQLModel): - name: str | None = None - secret_name: str | None = None - age: int | None = None - team_id: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def get_session(): - with Session(engine) as session: - yield session - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero( - *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate -): - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} - - -@app.post("/teams/", response_model=TeamPublic) -def create_team(*, session: Session = Depends(get_session), team: TeamCreate): - db_team = Team.model_validate(team) - session.add(db_team) - session.commit() - session.refresh(db_team) - return db_team - - -@app.get("/teams/", response_model=list[TeamPublic]) -def read_teams( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - teams = session.exec(select(Team).offset(offset).limit(limit)).all() - return teams - - -@app.get("/teams/{team_id}", response_model=TeamPublic) -def read_team(*, team_id: int, session: Session = Depends(get_session)): - team = session.get(Team, team_id) - if not team: - raise HTTPException(status_code=404, detail="Team not found") - return team - - -@app.patch("/teams/{team_id}", response_model=TeamPublic) -def update_team( - *, - session: Session = Depends(get_session), - team_id: int, - team: TeamUpdate, -): - db_team = session.get(Team, team_id) - if not db_team: - raise HTTPException(status_code=404, detail="Team not found") - team_data = team.model_dump(exclude_unset=True) - db_team.sqlmodel_update(team_data) - session.add(db_team) - session.commit() - session.refresh(db_team) - return db_team - - -@app.delete("/teams/{team_id}") -def delete_team(*, session: Session = Depends(get_session), team_id: int): - team = session.get(Team, team_id) - if not team: - raise HTTPException(status_code=404, detail="Team not found") - session.delete(team) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/teams/tutorial001_py39.py b/docs_src/tutorial/fastapi/teams/tutorial001_py39.py deleted file mode 100644 index 928ec70976..0000000000 --- a/docs_src/tutorial/fastapi/teams/tutorial001_py39.py +++ /dev/null @@ -1,190 +0,0 @@ -from typing import Optional - -from fastapi import Depends, FastAPI, HTTPException, Query -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class TeamBase(SQLModel): - name: str = Field(index=True) - headquarters: str - - -class Team(TeamBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class TeamCreate(TeamBase): - pass - - -class TeamPublic(TeamBase): - id: int - - -class TeamUpdate(SQLModel): - name: Optional[str] = None - headquarters: Optional[str] = None - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - team: Optional[Team] = Relationship(back_populates="heroes") - - -class HeroPublic(HeroBase): - id: int - - -class HeroCreate(HeroBase): - pass - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - team_id: Optional[int] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def get_session(): - with Session(engine) as session: - yield session - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate): - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero( - *, session: Session = Depends(get_session), hero_id: int, hero: HeroUpdate -): - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.delete("/heroes/{hero_id}") -def delete_hero(*, session: Session = Depends(get_session), hero_id: int): - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - session.delete(hero) - session.commit() - return {"ok": True} - - -@app.post("/teams/", response_model=TeamPublic) -def create_team(*, session: Session = Depends(get_session), team: TeamCreate): - db_team = Team.model_validate(team) - session.add(db_team) - session.commit() - session.refresh(db_team) - return db_team - - -@app.get("/teams/", response_model=list[TeamPublic]) -def read_teams( - *, - session: Session = Depends(get_session), - offset: int = 0, - limit: int = Query(default=100, le=100), -): - teams = session.exec(select(Team).offset(offset).limit(limit)).all() - return teams - - -@app.get("/teams/{team_id}", response_model=TeamPublic) -def read_team(*, team_id: int, session: Session = Depends(get_session)): - team = session.get(Team, team_id) - if not team: - raise HTTPException(status_code=404, detail="Team not found") - return team - - -@app.patch("/teams/{team_id}", response_model=TeamPublic) -def update_team( - *, - session: Session = Depends(get_session), - team_id: int, - team: TeamUpdate, -): - db_team = session.get(Team, team_id) - if not db_team: - raise HTTPException(status_code=404, detail="Team not found") - team_data = team.model_dump(exclude_unset=True) - db_team.sqlmodel_update(team_data) - session.add(db_team) - session.commit() - session.refresh(db_team) - return db_team - - -@app.delete("/teams/{team_id}") -def delete_team(*, session: Session = Depends(get_session), team_id: int): - team = session.get(Team, team_id) - if not team: - raise HTTPException(status_code=404, detail="Team not found") - session.delete(team) - session.commit() - return {"ok": True} diff --git a/docs_src/tutorial/fastapi/update/__init__.py b/docs_src/tutorial/fastapi/update/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/fastapi/update/tutorial001.py b/docs_src/tutorial/fastapi/update/tutorial001.py deleted file mode 100644 index 6a02f6c015..0000000000 --- a/docs_src/tutorial/fastapi/update/tutorial001.py +++ /dev/null @@ -1,87 +0,0 @@ -from typing import List, Optional - -from fastapi import FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=List[HeroPublic]) -def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): - with Session(engine) as session: - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero(hero_id: int, hero: HeroUpdate): - with Session(engine) as session: - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero diff --git a/docs_src/tutorial/fastapi/update/tutorial001_py310.py b/docs_src/tutorial/fastapi/update/tutorial001_py310.py deleted file mode 100644 index a98ee68fbb..0000000000 --- a/docs_src/tutorial/fastapi/update/tutorial001_py310.py +++ /dev/null @@ -1,85 +0,0 @@ -from fastapi import FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: int | None = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: str | None = None - secret_name: str | None = None - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): - with Session(engine) as session: - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero(hero_id: int, hero: HeroUpdate): - with Session(engine) as session: - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero diff --git a/docs_src/tutorial/fastapi/update/tutorial001_py39.py b/docs_src/tutorial/fastapi/update/tutorial001_py39.py deleted file mode 100644 index b6d62bf81c..0000000000 --- a/docs_src/tutorial/fastapi/update/tutorial001_py39.py +++ /dev/null @@ -1,87 +0,0 @@ -from typing import Optional - -from fastapi import FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - - -class HeroCreate(HeroBase): - pass - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - with Session(engine) as session: - db_hero = Hero.model_validate(hero) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): - with Session(engine) as session: - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero(hero_id: int, hero: HeroUpdate): - with Session(engine) as session: - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - db_hero.sqlmodel_update(hero_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero diff --git a/docs_src/tutorial/fastapi/update/tutorial002.py b/docs_src/tutorial/fastapi/update/tutorial002.py deleted file mode 100644 index c8838aeffb..0000000000 --- a/docs_src/tutorial/fastapi/update/tutorial002.py +++ /dev/null @@ -1,101 +0,0 @@ -from typing import List, Optional - -from fastapi import FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - hashed_password: str = Field() - - -class HeroCreate(HeroBase): - password: str - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - password: Optional[str] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def hash_password(password: str) -> str: - # Use something like passlib here - return f"not really hashed {password} hehehe" - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - hashed_password = hash_password(hero.password) - with Session(engine) as session: - extra_data = {"hashed_password": hashed_password} - db_hero = Hero.model_validate(hero, update=extra_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=List[HeroPublic]) -def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): - with Session(engine) as session: - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero(hero_id: int, hero: HeroUpdate): - with Session(engine) as session: - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - extra_data = {} - if "password" in hero_data: - password = hero_data["password"] - hashed_password = hash_password(password) - extra_data["hashed_password"] = hashed_password - db_hero.sqlmodel_update(hero_data, update=extra_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero diff --git a/docs_src/tutorial/fastapi/update/tutorial002_py310.py b/docs_src/tutorial/fastapi/update/tutorial002_py310.py deleted file mode 100644 index d250fecf3a..0000000000 --- a/docs_src/tutorial/fastapi/update/tutorial002_py310.py +++ /dev/null @@ -1,99 +0,0 @@ -from fastapi import FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: int | None = Field(default=None, primary_key=True) - hashed_password: str = Field() - - -class HeroCreate(HeroBase): - password: str - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: str | None = None - secret_name: str | None = None - age: int | None = None - password: str | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def hash_password(password: str) -> str: - # Use something like passlib here - return f"not really hashed {password} hehehe" - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - hashed_password = hash_password(hero.password) - with Session(engine) as session: - extra_data = {"hashed_password": hashed_password} - db_hero = Hero.model_validate(hero, update=extra_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): - with Session(engine) as session: - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero(hero_id: int, hero: HeroUpdate): - with Session(engine) as session: - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - extra_data = {} - if "password" in hero_data: - password = hero_data["password"] - hashed_password = hash_password(password) - extra_data["hashed_password"] = hashed_password - db_hero.sqlmodel_update(hero_data, update=extra_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero diff --git a/docs_src/tutorial/fastapi/update/tutorial002_py39.py b/docs_src/tutorial/fastapi/update/tutorial002_py39.py deleted file mode 100644 index 14ad1b4826..0000000000 --- a/docs_src/tutorial/fastapi/update/tutorial002_py39.py +++ /dev/null @@ -1,101 +0,0 @@ -from typing import Optional - -from fastapi import FastAPI, HTTPException, Query -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class HeroBase(SQLModel): - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -class Hero(HeroBase, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - hashed_password: str = Field() - - -class HeroCreate(HeroBase): - password: str - - -class HeroPublic(HeroBase): - id: int - - -class HeroUpdate(SQLModel): - name: Optional[str] = None - secret_name: Optional[str] = None - age: Optional[int] = None - password: Optional[str] = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -connect_args = {"check_same_thread": False} -engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def hash_password(password: str) -> str: - # Use something like passlib here - return f"not really hashed {password} hehehe" - - -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): - create_db_and_tables() - - -@app.post("/heroes/", response_model=HeroPublic) -def create_hero(hero: HeroCreate): - hashed_password = hash_password(hero.password) - with Session(engine) as session: - extra_data = {"hashed_password": hashed_password} - db_hero = Hero.model_validate(hero, update=extra_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero - - -@app.get("/heroes/", response_model=list[HeroPublic]) -def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): - with Session(engine) as session: - heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() - return heroes - - -@app.get("/heroes/{hero_id}", response_model=HeroPublic) -def read_hero(hero_id: int): - with Session(engine) as session: - hero = session.get(Hero, hero_id) - if not hero: - raise HTTPException(status_code=404, detail="Hero not found") - return hero - - -@app.patch("/heroes/{hero_id}", response_model=HeroPublic) -def update_hero(hero_id: int, hero: HeroUpdate): - with Session(engine) as session: - db_hero = session.get(Hero, hero_id) - if not db_hero: - raise HTTPException(status_code=404, detail="Hero not found") - hero_data = hero.model_dump(exclude_unset=True) - extra_data = {} - if "password" in hero_data: - password = hero_data["password"] - hashed_password = hash_password(password) - extra_data["hashed_password"] = hashed_password - db_hero.sqlmodel_update(hero_data, update=extra_data) - session.add(db_hero) - session.commit() - session.refresh(db_hero) - return db_hero diff --git a/docs_src/tutorial/indexes/__init__.py b/docs_src/tutorial/indexes/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/indexes/tutorial001.py b/docs_src/tutorial/indexes/tutorial001.py deleted file mode 100644 index 539220c9b7..0000000000 --- a/docs_src/tutorial/indexes/tutorial001.py +++ /dev/null @@ -1,51 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Deadpond") - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/indexes/tutorial001_py310.py b/docs_src/tutorial/indexes/tutorial001_py310.py deleted file mode 100644 index 115c447bcf..0000000000 --- a/docs_src/tutorial/indexes/tutorial001_py310.py +++ /dev/null @@ -1,49 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Deadpond") - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/indexes/tutorial002.py b/docs_src/tutorial/indexes/tutorial002.py deleted file mode 100644 index ebc8d64f65..0000000000 --- a/docs_src/tutorial/indexes/tutorial002.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age <= 35) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/indexes/tutorial002_py310.py b/docs_src/tutorial/indexes/tutorial002_py310.py deleted file mode 100644 index c0b7a1e65e..0000000000 --- a/docs_src/tutorial/indexes/tutorial002_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age <= 35) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/insert/__init__.py b/docs_src/tutorial/insert/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/insert/annotations/en/tutorial003.md b/docs_src/tutorial/insert/annotations/en/tutorial003.md deleted file mode 100644 index 8236078567..0000000000 --- a/docs_src/tutorial/insert/annotations/en/tutorial003.md +++ /dev/null @@ -1,57 +0,0 @@ -1. We use a function `create_heroes()` to put this logic together. - -2. Create each of the objects/instances of the `Hero` model. - - Each of them represents the data for one row. - -3. Use a `with` block to create a `Session` using the `engine`. - - The new **sesion** will be assigned to the variable `session`. - - And it will be automatically closed when the `with` block is finished. - -4. Add each of the objects/instances to the **session**. - - Each of these objects represents a row in the database. - - They are all waiting there in the session to be saved. - -5. **Commit** the changes to the database. - - This will actually send the data to the database. - - It will start a transaction automatically and save all the data in a single batch. - -6. By this point, after the `with` block is finished, the **session** is automatically closed. - -7. We have a `main()` function with all the code that should be executed when the program is called as a **script from the console**. - - That way we can add more code later to this function. - - We then put this function `main()` in the main block below. - - And as it is a single function, other Python files could **import it** and call it directly. - -8. In this `main()` function, we are also creating the database and the tables. - - In the previous version, this function was called directly in the main block. - - But now it is just called in the `main()` function. - -9. And now we are also creating the heroes in this `main()` function. - -10. We still have a main block to execute some code when the program is run as a script from the command line, like: - -
- - ```console - $ python app.py - - // Do whatever is in the main block πŸš€ - ``` - -
- -11. There's a single `main()` function now that contains all the code that should be executed when running the program from the console. - - So this is all we need to have in the main block. Just call the `main()` function. diff --git a/docs_src/tutorial/insert/tutorial001.py b/docs_src/tutorial/insert/tutorial001.py deleted file mode 100644 index 17050e908c..0000000000 --- a/docs_src/tutorial/insert/tutorial001.py +++ /dev/null @@ -1,45 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine - - -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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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 = Session(engine) - - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - session.close() - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/insert/tutorial001_py310.py b/docs_src/tutorial/insert/tutorial001_py310.py deleted file mode 100644 index 72f95ee279..0000000000 --- a/docs_src/tutorial/insert/tutorial001_py310.py +++ /dev/null @@ -1,43 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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 = Session(engine) - - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - session.close() - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/insert/tutorial002.py b/docs_src/tutorial/insert/tutorial002.py deleted file mode 100644 index 3ff10c8909..0000000000 --- a/docs_src/tutorial/insert/tutorial002.py +++ /dev/null @@ -1,42 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine - - -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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/insert/tutorial002_py310.py b/docs_src/tutorial/insert/tutorial002_py310.py deleted file mode 100644 index 266fbb8d32..0000000000 --- a/docs_src/tutorial/insert/tutorial002_py310.py +++ /dev/null @@ -1,40 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/insert/tutorial003.py b/docs_src/tutorial/insert/tutorial003.py deleted file mode 100644 index 03f51d2359..0000000000 --- a/docs_src/tutorial/insert/tutorial003.py +++ /dev/null @@ -1,43 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine - - -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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): # (1)! - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") # (2)! - hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") - hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) - - with Session(engine) as session: # (3)! - session.add(hero_1) # (4)! - session.add(hero_2) - session.add(hero_3) - - session.commit() # (5)! - # (6)! - - -def main(): # (7)! - create_db_and_tables() # (8)! - create_heroes() # (9)! - - -if __name__ == "__main__": # (10)! - main() # (11)! diff --git a/docs_src/tutorial/insert/tutorial003_py310.py b/docs_src/tutorial/insert/tutorial003_py310.py deleted file mode 100644 index da94841aba..0000000000 --- a/docs_src/tutorial/insert/tutorial003_py310.py +++ /dev/null @@ -1,41 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): # (1)! - hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") # (2)! - hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") - hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) - - with Session(engine) as session: # (3)! - session.add(hero_1) # (4)! - session.add(hero_2) - session.add(hero_3) - - session.commit() # (5)! - # (6)! - - -def main(): # (7)! - create_db_and_tables() # (8)! - create_heroes() # (9)! - - -if __name__ == "__main__": # (10)! - main() # (11)! diff --git a/docs_src/tutorial/many_to_many/__init__.py b/docs_src/tutorial/many_to_many/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/many_to_many/tutorial001.py b/docs_src/tutorial/many_to_many/tutorial001.py deleted file mode 100644 index 79f9e7909a..0000000000 --- a/docs_src/tutorial/many_to_many/tutorial001.py +++ /dev/null @@ -1,84 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine - - -class HeroTeamLink(SQLModel, table=True): - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", primary_key=True - ) - hero_id: Optional[int] = Field( - default=None, foreign_key="hero.id", primary_key=True - ) - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="teams", link_model=HeroTeamLink) - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - teams: List[Team] = Relationship(back_populates="heroes", link_model=HeroTeamLink) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") - - hero_deadpond = Hero( - name="Deadpond", - secret_name="Dive Wilson", - teams=[team_z_force, team_preventers], - ) - hero_rusty_man = Hero( - name="Rusty-Man", - secret_name="Tommy Sharp", - age=48, - teams=[team_preventers], - ) - hero_spider_boy = Hero( - name="Spider-Boy", secret_name="Pedro Parqueador", teams=[team_preventers] - ) - session.add(hero_deadpond) - session.add(hero_rusty_man) - session.add(hero_spider_boy) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Deadpond:", hero_deadpond) - print("Deadpond teams:", hero_deadpond.teams) - print("Rusty-Man:", hero_rusty_man) - print("Rusty-Man Teams:", hero_rusty_man.teams) - print("Spider-Boy:", hero_spider_boy) - print("Spider-Boy Teams:", hero_spider_boy.teams) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/many_to_many/tutorial001_py310.py b/docs_src/tutorial/many_to_many/tutorial001_py310.py deleted file mode 100644 index e47d3920eb..0000000000 --- a/docs_src/tutorial/many_to_many/tutorial001_py310.py +++ /dev/null @@ -1,78 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine - - -class HeroTeamLink(SQLModel, table=True): - team_id: int | None = Field(default=None, foreign_key="team.id", primary_key=True) - hero_id: int | None = Field(default=None, foreign_key="hero.id", primary_key=True) - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="teams", link_model=HeroTeamLink) - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - teams: list[Team] = Relationship(back_populates="heroes", link_model=HeroTeamLink) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") - - hero_deadpond = Hero( - name="Deadpond", - secret_name="Dive Wilson", - teams=[team_z_force, team_preventers], - ) - hero_rusty_man = Hero( - name="Rusty-Man", - secret_name="Tommy Sharp", - age=48, - teams=[team_preventers], - ) - hero_spider_boy = Hero( - name="Spider-Boy", secret_name="Pedro Parqueador", teams=[team_preventers] - ) - session.add(hero_deadpond) - session.add(hero_rusty_man) - session.add(hero_spider_boy) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Deadpond:", hero_deadpond) - print("Deadpond teams:", hero_deadpond.teams) - print("Rusty-Man:", hero_rusty_man) - print("Rusty-Man Teams:", hero_rusty_man.teams) - print("Spider-Boy:", hero_spider_boy) - print("Spider-Boy Teams:", hero_spider_boy.teams) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/many_to_many/tutorial001_py39.py b/docs_src/tutorial/many_to_many/tutorial001_py39.py deleted file mode 100644 index c39fe91452..0000000000 --- a/docs_src/tutorial/many_to_many/tutorial001_py39.py +++ /dev/null @@ -1,84 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine - - -class HeroTeamLink(SQLModel, table=True): - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", primary_key=True - ) - hero_id: Optional[int] = Field( - default=None, foreign_key="hero.id", primary_key=True - ) - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="teams", link_model=HeroTeamLink) - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - teams: list[Team] = Relationship(back_populates="heroes", link_model=HeroTeamLink) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") - - hero_deadpond = Hero( - name="Deadpond", - secret_name="Dive Wilson", - teams=[team_z_force, team_preventers], - ) - hero_rusty_man = Hero( - name="Rusty-Man", - secret_name="Tommy Sharp", - age=48, - teams=[team_preventers], - ) - hero_spider_boy = Hero( - name="Spider-Boy", secret_name="Pedro Parqueador", teams=[team_preventers] - ) - session.add(hero_deadpond) - session.add(hero_rusty_man) - session.add(hero_spider_boy) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Deadpond:", hero_deadpond) - print("Deadpond teams:", hero_deadpond.teams) - print("Rusty-Man:", hero_rusty_man) - print("Rusty-Man Teams:", hero_rusty_man.teams) - print("Spider-Boy:", hero_spider_boy) - print("Spider-Boy Teams:", hero_spider_boy.teams) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/many_to_many/tutorial002.py b/docs_src/tutorial/many_to_many/tutorial002.py deleted file mode 100644 index 9845340ec4..0000000000 --- a/docs_src/tutorial/many_to_many/tutorial002.py +++ /dev/null @@ -1,107 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class HeroTeamLink(SQLModel, table=True): - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", primary_key=True - ) - hero_id: Optional[int] = Field( - default=None, foreign_key="hero.id", primary_key=True - ) - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="teams", link_model=HeroTeamLink) - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - teams: List[Team] = Relationship(back_populates="heroes", link_model=HeroTeamLink) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") - - hero_deadpond = Hero( - name="Deadpond", - secret_name="Dive Wilson", - teams=[team_z_force, team_preventers], - ) - hero_rusty_man = Hero( - name="Rusty-Man", - secret_name="Tommy Sharp", - age=48, - teams=[team_preventers], - ) - hero_spider_boy = Hero( - name="Spider-Boy", secret_name="Pedro Parqueador", teams=[team_preventers] - ) - session.add(hero_deadpond) - session.add(hero_rusty_man) - session.add(hero_spider_boy) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Deadpond:", hero_deadpond) - print("Deadpond teams:", hero_deadpond.teams) - print("Rusty-Man:", hero_rusty_man) - print("Rusty-Man Teams:", hero_rusty_man.teams) - print("Spider-Boy:", hero_spider_boy) - print("Spider-Boy Teams:", hero_spider_boy.teams) - - -def update_heroes(): - with Session(engine) as session: - hero_spider_boy = session.exec( - select(Hero).where(Hero.name == "Spider-Boy") - ).one() - team_z_force = session.exec(select(Team).where(Team.name == "Z-Force")).one() - - team_z_force.heroes.append(hero_spider_boy) - session.add(team_z_force) - session.commit() - - print("Updated Spider-Boy's Teams:", hero_spider_boy.teams) - print("Z-Force heroes:", team_z_force.heroes) - - hero_spider_boy.teams.remove(team_z_force) - session.add(team_z_force) - session.commit() - - print("Reverted Z-Force's heroes:", team_z_force.heroes) - print("Reverted Spider-Boy's teams:", hero_spider_boy.teams) - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/many_to_many/tutorial002_py310.py b/docs_src/tutorial/many_to_many/tutorial002_py310.py deleted file mode 100644 index 2668161e92..0000000000 --- a/docs_src/tutorial/many_to_many/tutorial002_py310.py +++ /dev/null @@ -1,101 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class HeroTeamLink(SQLModel, table=True): - team_id: int | None = Field(default=None, foreign_key="team.id", primary_key=True) - hero_id: int | None = Field(default=None, foreign_key="hero.id", primary_key=True) - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="teams", link_model=HeroTeamLink) - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - teams: list[Team] = Relationship(back_populates="heroes", link_model=HeroTeamLink) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") - - hero_deadpond = Hero( - name="Deadpond", - secret_name="Dive Wilson", - teams=[team_z_force, team_preventers], - ) - hero_rusty_man = Hero( - name="Rusty-Man", - secret_name="Tommy Sharp", - age=48, - teams=[team_preventers], - ) - hero_spider_boy = Hero( - name="Spider-Boy", secret_name="Pedro Parqueador", teams=[team_preventers] - ) - session.add(hero_deadpond) - session.add(hero_rusty_man) - session.add(hero_spider_boy) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Deadpond:", hero_deadpond) - print("Deadpond teams:", hero_deadpond.teams) - print("Rusty-Man:", hero_rusty_man) - print("Rusty-Man Teams:", hero_rusty_man.teams) - print("Spider-Boy:", hero_spider_boy) - print("Spider-Boy Teams:", hero_spider_boy.teams) - - -def update_heroes(): - with Session(engine) as session: - hero_spider_boy = session.exec( - select(Hero).where(Hero.name == "Spider-Boy") - ).one() - team_z_force = session.exec(select(Team).where(Team.name == "Z-Force")).one() - - team_z_force.heroes.append(hero_spider_boy) - session.add(team_z_force) - session.commit() - - print("Updated Spider-Boy's Teams:", hero_spider_boy.teams) - print("Z-Force heroes:", team_z_force.heroes) - - hero_spider_boy.teams.remove(team_z_force) - session.add(team_z_force) - session.commit() - - print("Reverted Z-Force's heroes:", team_z_force.heroes) - print("Reverted Spider-Boy's teams:", hero_spider_boy.teams) - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/many_to_many/tutorial002_py39.py b/docs_src/tutorial/many_to_many/tutorial002_py39.py deleted file mode 100644 index c3b5f88f0e..0000000000 --- a/docs_src/tutorial/many_to_many/tutorial002_py39.py +++ /dev/null @@ -1,107 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class HeroTeamLink(SQLModel, table=True): - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", primary_key=True - ) - hero_id: Optional[int] = Field( - default=None, foreign_key="hero.id", primary_key=True - ) - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="teams", link_model=HeroTeamLink) - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - teams: list[Team] = Relationship(back_populates="heroes", link_model=HeroTeamLink) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") - - hero_deadpond = Hero( - name="Deadpond", - secret_name="Dive Wilson", - teams=[team_z_force, team_preventers], - ) - hero_rusty_man = Hero( - name="Rusty-Man", - secret_name="Tommy Sharp", - age=48, - teams=[team_preventers], - ) - hero_spider_boy = Hero( - name="Spider-Boy", secret_name="Pedro Parqueador", teams=[team_preventers] - ) - session.add(hero_deadpond) - session.add(hero_rusty_man) - session.add(hero_spider_boy) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - session.refresh(hero_spider_boy) - - print("Deadpond:", hero_deadpond) - print("Deadpond teams:", hero_deadpond.teams) - print("Rusty-Man:", hero_rusty_man) - print("Rusty-Man Teams:", hero_rusty_man.teams) - print("Spider-Boy:", hero_spider_boy) - print("Spider-Boy Teams:", hero_spider_boy.teams) - - -def update_heroes(): - with Session(engine) as session: - hero_spider_boy = session.exec( - select(Hero).where(Hero.name == "Spider-Boy") - ).one() - team_z_force = session.exec(select(Team).where(Team.name == "Z-Force")).one() - - team_z_force.heroes.append(hero_spider_boy) - session.add(team_z_force) - session.commit() - - print("Updated Spider-Boy's Teams:", hero_spider_boy.teams) - print("Z-Force heroes:", team_z_force.heroes) - - hero_spider_boy.teams.remove(team_z_force) - session.add(team_z_force) - session.commit() - - print("Reverted Z-Force's heroes:", team_z_force.heroes) - print("Reverted Spider-Boy's teams:", hero_spider_boy.teams) - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/many_to_many/tutorial003.py b/docs_src/tutorial/many_to_many/tutorial003.py deleted file mode 100644 index 6c23878b01..0000000000 --- a/docs_src/tutorial/many_to_many/tutorial003.py +++ /dev/null @@ -1,123 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class HeroTeamLink(SQLModel, table=True): - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", primary_key=True - ) - hero_id: Optional[int] = Field( - default=None, foreign_key="hero.id", primary_key=True - ) - is_training: bool = False - - team: "Team" = Relationship(back_populates="hero_links") - hero: "Hero" = Relationship(back_populates="team_links") - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - hero_links: List[HeroTeamLink] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_links: List[HeroTeamLink] = Relationship(back_populates="hero") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") - - hero_deadpond = Hero( - name="Deadpond", - secret_name="Dive Wilson", - ) - hero_rusty_man = Hero( - name="Rusty-Man", - secret_name="Tommy Sharp", - age=48, - ) - hero_spider_boy = Hero( - name="Spider-Boy", - secret_name="Pedro Parqueador", - ) - deadpond_team_z_link = HeroTeamLink(team=team_z_force, hero=hero_deadpond) - deadpond_preventers_link = HeroTeamLink( - team=team_preventers, hero=hero_deadpond, is_training=True - ) - spider_boy_preventers_link = HeroTeamLink( - team=team_preventers, hero=hero_spider_boy, is_training=True - ) - rusty_man_preventers_link = HeroTeamLink( - team=team_preventers, hero=hero_rusty_man - ) - - session.add(deadpond_team_z_link) - session.add(deadpond_preventers_link) - session.add(spider_boy_preventers_link) - session.add(rusty_man_preventers_link) - session.commit() - - for link in team_z_force.hero_links: - print("Z-Force hero:", link.hero, "is training:", link.is_training) - - for link in team_preventers.hero_links: - print("Preventers hero:", link.hero, "is training:", link.is_training) - - -def update_heroes(): - with Session(engine) as session: - hero_spider_boy = session.exec( - select(Hero).where(Hero.name == "Spider-Boy") - ).one() - team_z_force = session.exec(select(Team).where(Team.name == "Z-Force")).one() - - spider_boy_z_force_link = HeroTeamLink( - team=team_z_force, hero=hero_spider_boy, is_training=True - ) - team_z_force.hero_links.append(spider_boy_z_force_link) - session.add(team_z_force) - session.commit() - - print("Updated Spider-Boy's Teams:", hero_spider_boy.team_links) - print("Z-Force heroes:", team_z_force.hero_links) - - for link in hero_spider_boy.team_links: - if link.team.name == "Preventers": - link.is_training = False - - session.add(hero_spider_boy) - session.commit() - - for link in hero_spider_boy.team_links: - print("Spider-Boy team:", link.team, "is training:", link.is_training) - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/many_to_many/tutorial003_py310.py b/docs_src/tutorial/many_to_many/tutorial003_py310.py deleted file mode 100644 index 4c1ad12259..0000000000 --- a/docs_src/tutorial/many_to_many/tutorial003_py310.py +++ /dev/null @@ -1,117 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class HeroTeamLink(SQLModel, table=True): - team_id: int | None = Field(default=None, foreign_key="team.id", primary_key=True) - hero_id: int | None = Field(default=None, foreign_key="hero.id", primary_key=True) - is_training: bool = False - - team: "Team" = Relationship(back_populates="hero_links") - hero: "Hero" = Relationship(back_populates="team_links") - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - hero_links: list[HeroTeamLink] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_links: list[HeroTeamLink] = Relationship(back_populates="hero") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") - - hero_deadpond = Hero( - name="Deadpond", - secret_name="Dive Wilson", - ) - hero_rusty_man = Hero( - name="Rusty-Man", - secret_name="Tommy Sharp", - age=48, - ) - hero_spider_boy = Hero( - name="Spider-Boy", - secret_name="Pedro Parqueador", - ) - deadpond_team_z_link = HeroTeamLink(team=team_z_force, hero=hero_deadpond) - deadpond_preventers_link = HeroTeamLink( - team=team_preventers, hero=hero_deadpond, is_training=True - ) - spider_boy_preventers_link = HeroTeamLink( - team=team_preventers, hero=hero_spider_boy, is_training=True - ) - rusty_man_preventers_link = HeroTeamLink( - team=team_preventers, hero=hero_rusty_man - ) - - session.add(deadpond_team_z_link) - session.add(deadpond_preventers_link) - session.add(spider_boy_preventers_link) - session.add(rusty_man_preventers_link) - session.commit() - - for link in team_z_force.hero_links: - print("Z-Force hero:", link.hero, "is training:", link.is_training) - - for link in team_preventers.hero_links: - print("Preventers hero:", link.hero, "is training:", link.is_training) - - -def update_heroes(): - with Session(engine) as session: - hero_spider_boy = session.exec( - select(Hero).where(Hero.name == "Spider-Boy") - ).one() - team_z_force = session.exec(select(Team).where(Team.name == "Z-Force")).one() - - spider_boy_z_force_link = HeroTeamLink( - team=team_z_force, hero=hero_spider_boy, is_training=True - ) - team_z_force.hero_links.append(spider_boy_z_force_link) - session.add(team_z_force) - session.commit() - - print("Updated Spider-Boy's Teams:", hero_spider_boy.team_links) - print("Z-Force heroes:", team_z_force.hero_links) - - for link in hero_spider_boy.team_links: - if link.team.name == "Preventers": - link.is_training = False - - session.add(hero_spider_boy) - session.commit() - - for link in hero_spider_boy.team_links: - print("Spider-Boy team:", link.team, "is training:", link.is_training) - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/many_to_many/tutorial003_py39.py b/docs_src/tutorial/many_to_many/tutorial003_py39.py deleted file mode 100644 index 175fbf318b..0000000000 --- a/docs_src/tutorial/many_to_many/tutorial003_py39.py +++ /dev/null @@ -1,123 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class HeroTeamLink(SQLModel, table=True): - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", primary_key=True - ) - hero_id: Optional[int] = Field( - default=None, foreign_key="hero.id", primary_key=True - ) - is_training: bool = False - - team: "Team" = Relationship(back_populates="hero_links") - hero: "Hero" = Relationship(back_populates="team_links") - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - hero_links: list[HeroTeamLink] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_links: list[HeroTeamLink] = Relationship(back_populates="hero") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - team_z_force = Team(name="Z-Force", headquarters="Sister Margaret's Bar") - - hero_deadpond = Hero( - name="Deadpond", - secret_name="Dive Wilson", - ) - hero_rusty_man = Hero( - name="Rusty-Man", - secret_name="Tommy Sharp", - age=48, - ) - hero_spider_boy = Hero( - name="Spider-Boy", - secret_name="Pedro Parqueador", - ) - deadpond_team_z_link = HeroTeamLink(team=team_z_force, hero=hero_deadpond) - deadpond_preventers_link = HeroTeamLink( - team=team_preventers, hero=hero_deadpond, is_training=True - ) - spider_boy_preventers_link = HeroTeamLink( - team=team_preventers, hero=hero_spider_boy, is_training=True - ) - rusty_man_preventers_link = HeroTeamLink( - team=team_preventers, hero=hero_rusty_man - ) - - session.add(deadpond_team_z_link) - session.add(deadpond_preventers_link) - session.add(spider_boy_preventers_link) - session.add(rusty_man_preventers_link) - session.commit() - - for link in team_z_force.hero_links: - print("Z-Force hero:", link.hero, "is training:", link.is_training) - - for link in team_preventers.hero_links: - print("Preventers hero:", link.hero, "is training:", link.is_training) - - -def update_heroes(): - with Session(engine) as session: - hero_spider_boy = session.exec( - select(Hero).where(Hero.name == "Spider-Boy") - ).one() - team_z_force = session.exec(select(Team).where(Team.name == "Z-Force")).one() - - spider_boy_z_force_link = HeroTeamLink( - team=team_z_force, hero=hero_spider_boy, is_training=True - ) - team_z_force.hero_links.append(spider_boy_z_force_link) - session.add(team_z_force) - session.commit() - - print("Updated Spider-Boy's Teams:", hero_spider_boy.team_links) - print("Z-Force heroes:", team_z_force.hero_links) - - for link in hero_spider_boy.team_links: - if link.team.name == "Preventers": - link.is_training = False - - session.add(hero_spider_boy) - session.commit() - - for link in hero_spider_boy.team_links: - print("Spider-Boy team:", link.team, "is training:", link.is_training) - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/offset_and_limit/__init__.py b/docs_src/tutorial/offset_and_limit/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/offset_and_limit/tutorial001.py b/docs_src/tutorial/offset_and_limit/tutorial001.py deleted file mode 100644 index 1b033accb9..0000000000 --- a/docs_src/tutorial/offset_and_limit/tutorial001.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).limit(3) - results = session.exec(statement) - heroes = results.all() - print(heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/offset_and_limit/tutorial001_py310.py b/docs_src/tutorial/offset_and_limit/tutorial001_py310.py deleted file mode 100644 index 931f46e247..0000000000 --- a/docs_src/tutorial/offset_and_limit/tutorial001_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).limit(3) - results = session.exec(statement) - heroes = results.all() - print(heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/offset_and_limit/tutorial002.py b/docs_src/tutorial/offset_and_limit/tutorial002.py deleted file mode 100644 index 65a62369f4..0000000000 --- a/docs_src/tutorial/offset_and_limit/tutorial002.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).offset(3).limit(3) - results = session.exec(statement) - heroes = results.all() - print(heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/offset_and_limit/tutorial002_py310.py b/docs_src/tutorial/offset_and_limit/tutorial002_py310.py deleted file mode 100644 index ab5c89b7cc..0000000000 --- a/docs_src/tutorial/offset_and_limit/tutorial002_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).offset(3).limit(3) - results = session.exec(statement) - heroes = results.all() - print(heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/offset_and_limit/tutorial003.py b/docs_src/tutorial/offset_and_limit/tutorial003.py deleted file mode 100644 index 36cae9c42c..0000000000 --- a/docs_src/tutorial/offset_and_limit/tutorial003.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).offset(6).limit(3) - results = session.exec(statement) - heroes = results.all() - print(heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/offset_and_limit/tutorial003_py310.py b/docs_src/tutorial/offset_and_limit/tutorial003_py310.py deleted file mode 100644 index 5ac24937d6..0000000000 --- a/docs_src/tutorial/offset_and_limit/tutorial003_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).offset(6).limit(3) - results = session.exec(statement) - heroes = results.all() - print(heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/offset_and_limit/tutorial004.py b/docs_src/tutorial/offset_and_limit/tutorial004.py deleted file mode 100644 index 43828b853f..0000000000 --- a/docs_src/tutorial/offset_and_limit/tutorial004.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age > 32).offset(1).limit(2) - results = session.exec(statement) - heroes = results.all() - print(heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/offset_and_limit/tutorial004_py310.py b/docs_src/tutorial/offset_and_limit/tutorial004_py310.py deleted file mode 100644 index c7e7bbb414..0000000000 --- a/docs_src/tutorial/offset_and_limit/tutorial004_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age > 32).offset(1).limit(2) - results = session.exec(statement) - heroes = results.all() - print(heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/__init__.py b/docs_src/tutorial/one/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/one/tutorial001.py b/docs_src/tutorial/one/tutorial001.py deleted file mode 100644 index 072f4a3bf5..0000000000 --- a/docs_src/tutorial/one/tutorial001.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age <= 35) - results = session.exec(statement) - hero = results.first() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial001_py310.py b/docs_src/tutorial/one/tutorial001_py310.py deleted file mode 100644 index bb1326deef..0000000000 --- a/docs_src/tutorial/one/tutorial001_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age <= 35) - results = session.exec(statement) - hero = results.first() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial002.py b/docs_src/tutorial/one/tutorial002.py deleted file mode 100644 index c24490659f..0000000000 --- a/docs_src/tutorial/one/tutorial002.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age < 25) - results = session.exec(statement) - hero = results.first() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial002_py310.py b/docs_src/tutorial/one/tutorial002_py310.py deleted file mode 100644 index b82fcfd845..0000000000 --- a/docs_src/tutorial/one/tutorial002_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age < 25) - results = session.exec(statement) - hero = results.first() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial003.py b/docs_src/tutorial/one/tutorial003.py deleted file mode 100644 index f8fb0bcd7d..0000000000 --- a/docs_src/tutorial/one/tutorial003.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Deadpond") - results = session.exec(statement) - hero = results.one() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial003_py310.py b/docs_src/tutorial/one/tutorial003_py310.py deleted file mode 100644 index f674c8a686..0000000000 --- a/docs_src/tutorial/one/tutorial003_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Deadpond") - results = session.exec(statement) - hero = results.one() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial004.py b/docs_src/tutorial/one/tutorial004.py deleted file mode 100644 index 8688fc4799..0000000000 --- a/docs_src/tutorial/one/tutorial004.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age <= 35) - results = session.exec(statement) - hero = results.one() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial004_py310.py b/docs_src/tutorial/one/tutorial004_py310.py deleted file mode 100644 index e55b55304f..0000000000 --- a/docs_src/tutorial/one/tutorial004_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age <= 35) - results = session.exec(statement) - hero = results.one() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial005.py b/docs_src/tutorial/one/tutorial005.py deleted file mode 100644 index f624d4cb68..0000000000 --- a/docs_src/tutorial/one/tutorial005.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age < 25) - results = session.exec(statement) - hero = results.one() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial005_py310.py b/docs_src/tutorial/one/tutorial005_py310.py deleted file mode 100644 index 6c51d8fab2..0000000000 --- a/docs_src/tutorial/one/tutorial005_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age < 25) - results = session.exec(statement) - hero = results.one() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial006.py b/docs_src/tutorial/one/tutorial006.py deleted file mode 100644 index afc48547d9..0000000000 --- a/docs_src/tutorial/one/tutorial006.py +++ /dev/null @@ -1,57 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - hero = session.exec(select(Hero).where(Hero.name == "Deadpond")).one() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial006_py310.py b/docs_src/tutorial/one/tutorial006_py310.py deleted file mode 100644 index 6f9b7371ab..0000000000 --- a/docs_src/tutorial/one/tutorial006_py310.py +++ /dev/null @@ -1,55 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - hero = session.exec(select(Hero).where(Hero.name == "Deadpond")).one() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial007.py b/docs_src/tutorial/one/tutorial007.py deleted file mode 100644 index 15df5baa94..0000000000 --- a/docs_src/tutorial/one/tutorial007.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.id == 1) - results = session.exec(statement) - hero = results.first() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial007_py310.py b/docs_src/tutorial/one/tutorial007_py310.py deleted file mode 100644 index f065f5ac78..0000000000 --- a/docs_src/tutorial/one/tutorial007_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.id == 1) - results = session.exec(statement) - hero = results.first() - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial008.py b/docs_src/tutorial/one/tutorial008.py deleted file mode 100644 index 9aa077ca38..0000000000 --- a/docs_src/tutorial/one/tutorial008.py +++ /dev/null @@ -1,57 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - hero = session.get(Hero, 1) - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial008_py310.py b/docs_src/tutorial/one/tutorial008_py310.py deleted file mode 100644 index af9169704b..0000000000 --- a/docs_src/tutorial/one/tutorial008_py310.py +++ /dev/null @@ -1,55 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - hero = session.get(Hero, 1) - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial009.py b/docs_src/tutorial/one/tutorial009.py deleted file mode 100644 index f4ee23b355..0000000000 --- a/docs_src/tutorial/one/tutorial009.py +++ /dev/null @@ -1,57 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - hero = session.get(Hero, 9001) - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/one/tutorial009_py310.py b/docs_src/tutorial/one/tutorial009_py310.py deleted file mode 100644 index 57db99ea42..0000000000 --- a/docs_src/tutorial/one/tutorial009_py310.py +++ /dev/null @@ -1,55 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - hero = session.get(Hero, 9001) - print("Hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/__init__.py b/docs_src/tutorial/relationship_attributes/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/relationship_attributes/back_populates/__init__.py b/docs_src/tutorial/relationship_attributes/back_populates/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py b/docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py deleted file mode 100644 index 39230cb697..0000000000 --- a/docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py +++ /dev/null @@ -1,143 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship() - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship() - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Preventers") - result = session.exec(statement) - team_preventers = result.one() - - print("Preventers heroes:", team_preventers.heroes) - - -def update_heroes(): - with Session(engine) as session: - hero_spider_boy = session.exec( - select(Hero).where(Hero.name == "Spider-Boy") - ).one() - - preventers_team = session.exec( - select(Team).where(Team.name == "Preventers") - ).one() - - print("Hero Spider-Boy:", hero_spider_boy) - print("Preventers Team:", preventers_team) - print("Preventers Team Heroes:", preventers_team.heroes) - - hero_spider_boy.team = None - - print("Spider-Boy without team:", hero_spider_boy) - - print("Preventers Team Heroes again:", preventers_team.heroes) - - session.add(hero_spider_boy) - session.commit() - print("After committing") - - session.refresh(hero_spider_boy) - print("Spider-Boy after commit:", hero_spider_boy) - - print("Preventers Team Heroes after commit:", preventers_team.heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py b/docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py deleted file mode 100644 index a6ee1cafba..0000000000 --- a/docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py +++ /dev/null @@ -1,141 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship() - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - team: Team | None = Relationship() - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Preventers") - result = session.exec(statement) - team_preventers = result.one() - - print("Preventers heroes:", team_preventers.heroes) - - -def update_heroes(): - with Session(engine) as session: - hero_spider_boy = session.exec( - select(Hero).where(Hero.name == "Spider-Boy") - ).one() - - preventers_team = session.exec( - select(Team).where(Team.name == "Preventers") - ).one() - - print("Hero Spider-Boy:", hero_spider_boy) - print("Preventers Team:", preventers_team) - print("Preventers Team Heroes:", preventers_team.heroes) - - hero_spider_boy.team = None - - print("Spider-Boy without team:", hero_spider_boy) - - print("Preventers Team Heroes again:", preventers_team.heroes) - - session.add(hero_spider_boy) - session.commit() - print("After committing") - - session.refresh(hero_spider_boy) - print("Spider-Boy after commit:", hero_spider_boy) - - print("Preventers Team Heroes after commit:", preventers_team.heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py b/docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py deleted file mode 100644 index 6ec5e72a7c..0000000000 --- a/docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py +++ /dev/null @@ -1,143 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship() - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship() - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Preventers") - result = session.exec(statement) - team_preventers = result.one() - - print("Preventers heroes:", team_preventers.heroes) - - -def update_heroes(): - with Session(engine) as session: - hero_spider_boy = session.exec( - select(Hero).where(Hero.name == "Spider-Boy") - ).one() - - preventers_team = session.exec( - select(Team).where(Team.name == "Preventers") - ).one() - - print("Hero Spider-Boy:", hero_spider_boy) - print("Preventers Team:", preventers_team) - print("Preventers Team Heroes:", preventers_team.heroes) - - hero_spider_boy.team = None - - print("Spider-Boy without team:", hero_spider_boy) - - print("Preventers Team Heroes again:", preventers_team.heroes) - - session.add(hero_spider_boy) - session.commit() - print("After committing") - - session.refresh(hero_spider_boy) - print("Spider-Boy after commit:", hero_spider_boy) - - print("Preventers Team Heroes after commit:", preventers_team.heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py b/docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py deleted file mode 100644 index a1add4bccb..0000000000 --- a/docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py +++ /dev/null @@ -1,143 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Preventers") - result = session.exec(statement) - team_preventers = result.one() - - print("Preventers heroes:", team_preventers.heroes) - - -def update_heroes(): - with Session(engine) as session: - hero_spider_boy = session.exec( - select(Hero).where(Hero.name == "Spider-Boy") - ).one() - - preventers_team = session.exec( - select(Team).where(Team.name == "Preventers") - ).one() - - print("Hero Spider-Boy:", hero_spider_boy) - print("Preventers Team:", preventers_team) - print("Preventers Team Heroes:", preventers_team.heroes) - - hero_spider_boy.team = None - - print("Spider-Boy without team:", hero_spider_boy) - - print("Preventers Team Heroes again:", preventers_team.heroes) - - session.add(hero_spider_boy) - session.commit() - print("After committing") - - session.refresh(hero_spider_boy) - print("Spider-Boy after commit:", hero_spider_boy) - - print("Preventers Team Heroes after commit:", preventers_team.heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py b/docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py deleted file mode 100644 index 978ed99787..0000000000 --- a/docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py +++ /dev/null @@ -1,141 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - team: Team | None = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Preventers") - result = session.exec(statement) - team_preventers = result.one() - - print("Preventers heroes:", team_preventers.heroes) - - -def update_heroes(): - with Session(engine) as session: - hero_spider_boy = session.exec( - select(Hero).where(Hero.name == "Spider-Boy") - ).one() - - preventers_team = session.exec( - select(Team).where(Team.name == "Preventers") - ).one() - - print("Hero Spider-Boy:", hero_spider_boy) - print("Preventers Team:", preventers_team) - print("Preventers Team Heroes:", preventers_team.heroes) - - hero_spider_boy.team = None - - print("Spider-Boy without team:", hero_spider_boy) - - print("Preventers Team Heroes again:", preventers_team.heroes) - - session.add(hero_spider_boy) - session.commit() - print("After committing") - - session.refresh(hero_spider_boy) - print("Spider-Boy after commit:", hero_spider_boy) - - print("Preventers Team Heroes after commit:", preventers_team.heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py b/docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py deleted file mode 100644 index d001ef03da..0000000000 --- a/docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py +++ /dev/null @@ -1,143 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Preventers") - result = session.exec(statement) - team_preventers = result.one() - - print("Preventers heroes:", team_preventers.heroes) - - -def update_heroes(): - with Session(engine) as session: - hero_spider_boy = session.exec( - select(Hero).where(Hero.name == "Spider-Boy") - ).one() - - preventers_team = session.exec( - select(Team).where(Team.name == "Preventers") - ).one() - - print("Hero Spider-Boy:", hero_spider_boy) - print("Preventers Team:", preventers_team) - print("Preventers Team Heroes:", preventers_team.heroes) - - hero_spider_boy.team = None - - print("Spider-Boy without team:", hero_spider_boy) - - print("Preventers Team Heroes again:", preventers_team.heroes) - - session.add(hero_spider_boy) - session.commit() - print("After committing") - - session.refresh(hero_spider_boy) - print("Spider-Boy after commit:", hero_spider_boy) - - print("Preventers Team Heroes after commit:", preventers_team.heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/back_populates/tutorial003.py b/docs_src/tutorial/relationship_attributes/back_populates/tutorial003.py deleted file mode 100644 index 98e197002e..0000000000 --- a/docs_src/tutorial/relationship_attributes/back_populates/tutorial003.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, SQLModel, create_engine - - -class Weapon(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - - hero: "Hero" = Relationship(back_populates="weapon") - - -class Power(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - - hero_id: int = Field(foreign_key="hero.id") - hero: "Hero" = Relationship(back_populates="powers") - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship(back_populates="heroes") - - weapon_id: Optional[int] = Field(default=None, foreign_key="weapon.id") - weapon: Optional[Weapon] = Relationship(back_populates="hero") - - powers: List[Power] = Relationship(back_populates="hero") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def main(): - create_db_and_tables() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py310.py b/docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py310.py deleted file mode 100644 index aa850b3f69..0000000000 --- a/docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Relationship, SQLModel, create_engine - - -class Weapon(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - - hero: "Hero" = Relationship(back_populates="weapon") - - -class Power(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - - hero_id: int = Field(foreign_key="hero.id") - hero: "Hero" = Relationship(back_populates="powers") - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - team: Team | None = Relationship(back_populates="heroes") - - weapon_id: int | None = Field(default=None, foreign_key="weapon.id") - weapon: Weapon | None = Relationship(back_populates="hero") - - powers: list[Power] = Relationship(back_populates="hero") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def main(): - create_db_and_tables() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py39.py b/docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py39.py deleted file mode 100644 index fc0d08df52..0000000000 --- a/docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py39.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, SQLModel, create_engine - - -class Weapon(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - - hero: "Hero" = Relationship(back_populates="weapon") - - -class Power(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - - hero_id: int = Field(foreign_key="hero.id") - hero: "Hero" = Relationship(back_populates="powers") - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship(back_populates="heroes") - - weapon_id: Optional[int] = Field(default=None, foreign_key="weapon.id") - weapon: Optional[Weapon] = Relationship(back_populates="hero") - - powers: list[Power] = Relationship(back_populates="hero") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def main(): - create_db_and_tables() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/__init__.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py deleted file mode 100644 index 877aeb3c67..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py +++ /dev/null @@ -1,110 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team", cascade_delete=True) - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", ondelete="CASCADE" - ) - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion not found:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E not found:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - delete_team() - select_deleted_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py deleted file mode 100644 index 8757aaa774..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py +++ /dev/null @@ -1,106 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team", cascade_delete=True) - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id", ondelete="CASCADE") - team: Team | None = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion not found:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E not found:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - delete_team() - select_deleted_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py deleted file mode 100644 index 201f07675d..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py +++ /dev/null @@ -1,110 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team", cascade_delete=True) - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", ondelete="CASCADE" - ) - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion not found:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E not found:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - delete_team() - select_deleted_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002.py deleted file mode 100644 index ac97a20f28..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002.py +++ /dev/null @@ -1,110 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", ondelete="SET NULL" - ) - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion has no team:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E has no team:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - delete_team() - select_deleted_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py deleted file mode 100644 index be42dba697..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py +++ /dev/null @@ -1,108 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field( - default=None, foreign_key="team.id", ondelete="SET NULL" - ) - team: Team | None = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion has no team:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E has no team:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - delete_team() - select_deleted_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py39.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py39.py deleted file mode 100644 index 7a22f6ee16..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py39.py +++ /dev/null @@ -1,110 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", ondelete="SET NULL" - ) - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion has no team:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E has no team:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - delete_team() - select_deleted_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003.py deleted file mode 100644 index 0424f75614..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003.py +++ /dev/null @@ -1,112 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select, text - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team", passive_deletes="all") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", ondelete="SET NULL" - ) - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - with engine.connect() as connection: - connection.execute(text("PRAGMA foreign_keys=ON")) # for SQLite only - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion has no team:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E has no team:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - delete_team() - select_deleted_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py deleted file mode 100644 index 4623da1ed0..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py +++ /dev/null @@ -1,110 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select, text - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team", passive_deletes="all") - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field( - default=None, foreign_key="team.id", ondelete="SET NULL" - ) - team: Team | None = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - with engine.connect() as connection: - connection.execute(text("PRAGMA foreign_keys=ON")) # for SQLite only - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion has no team:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E has no team:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - delete_team() - select_deleted_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py39.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py39.py deleted file mode 100644 index 0327e69f50..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py39.py +++ /dev/null @@ -1,112 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select, text - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team", passive_deletes="all") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", ondelete="SET NULL" - ) - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - with engine.connect() as connection: - connection.execute(text("PRAGMA foreign_keys=ON")) # for SQLite only - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion has no team:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E has no team:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - delete_team() - select_deleted_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004.py deleted file mode 100644 index 00c5ac1765..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004.py +++ /dev/null @@ -1,111 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select, text - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team", passive_deletes="all") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", ondelete="RESTRICT" - ) - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - with engine.connect() as connection: - connection.execute(text("PRAGMA foreign_keys=ON")) # for SQLite only - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion has no team:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E has no team:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - delete_team() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py deleted file mode 100644 index 051c14db34..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py +++ /dev/null @@ -1,109 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select, text - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team", passive_deletes="all") - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field( - default=None, foreign_key="team.id", ondelete="RESTRICT" - ) - team: Team | None = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - with engine.connect() as connection: - connection.execute(text("PRAGMA foreign_keys=ON")) # for SQLite only - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion has no team:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E has no team:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - delete_team() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py39.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py39.py deleted file mode 100644 index 25badb1fdf..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py39.py +++ /dev/null @@ -1,111 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select, text - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team", passive_deletes="all") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", ondelete="RESTRICT" - ) - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - with engine.connect() as connection: - connection.execute(text("PRAGMA foreign_keys=ON")) # for SQLite only - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion has no team:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E has no team:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - delete_team() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005.py deleted file mode 100644 index b46a7781bc..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005.py +++ /dev/null @@ -1,124 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select, text - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team", passive_deletes="all") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", ondelete="RESTRICT" - ) - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - with engine.connect() as connection: - connection.execute(text("PRAGMA foreign_keys=ON")) # for SQLite only - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def remove_team_heroes(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - team.heroes.clear() - session.add(team) - session.commit() - session.refresh(team) - print("Team with removed heroes:", team) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion has no team:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E has no team:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - remove_team_heroes() - delete_team() - select_deleted_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py310.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py310.py deleted file mode 100644 index 1d89bcae07..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py310.py +++ /dev/null @@ -1,122 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select, text - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team", passive_deletes="all") - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field( - default=None, foreign_key="team.id", ondelete="RESTRICT" - ) - team: Team | None = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - with engine.connect() as connection: - connection.execute(text("PRAGMA foreign_keys=ON")) # for SQLite only - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def remove_team_heroes(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - team.heroes.clear() - session.add(team) - session.commit() - session.refresh(team) - print("Team with removed heroes:", team) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion has no team:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E has no team:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - remove_team_heroes() - delete_team() - select_deleted_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py39.py b/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py39.py deleted file mode 100644 index edc8fb0bd5..0000000000 --- a/docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py39.py +++ /dev/null @@ -1,124 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select, text - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team", passive_deletes="all") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field( - default=None, foreign_key="team.id", ondelete="RESTRICT" - ) - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - with engine.connect() as connection: - connection.execute(text("PRAGMA foreign_keys=ON")) # for SQLite only - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - -def remove_team_heroes(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - team.heroes.clear() - session.add(team) - session.commit() - session.refresh(team) - print("Team with removed heroes:", team) - - -def delete_team(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Wakaland") - team = session.exec(statement).one() - session.delete(team) - session.commit() - print("Deleted team:", team) - - -def select_deleted_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Black Lion") - result = session.exec(statement) - hero = result.first() - print("Black Lion has no team:", hero) - - statement = select(Hero).where(Hero.name == "Princess Sure-E") - result = session.exec(statement) - hero = result.first() - print("Princess Sure-E has no team:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - remove_team_heroes() - delete_team() - select_deleted_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/create_and_update_relationships/__init__.py b/docs_src/tutorial/relationship_attributes/create_and_update_relationships/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py b/docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py deleted file mode 100644 index 73c68cf965..0000000000 --- a/docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py +++ /dev/null @@ -1,102 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py b/docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py deleted file mode 100644 index 35567360e1..0000000000 --- a/docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py +++ /dev/null @@ -1,100 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - team: Team | None = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py b/docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py deleted file mode 100644 index 43c699d3d9..0000000000 --- a/docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py +++ /dev/null @@ -1,102 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/define_relationship_attributes/__init__.py b/docs_src/tutorial/relationship_attributes/define_relationship_attributes/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py b/docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py deleted file mode 100644 index af218b9567..0000000000 --- a/docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py +++ /dev/null @@ -1,70 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py b/docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py deleted file mode 100644 index 5df80e9f86..0000000000 --- a/docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py +++ /dev/null @@ -1,68 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - team: Team | None = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py b/docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py deleted file mode 100644 index 8530f67b39..0000000000 --- a/docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py +++ /dev/null @@ -1,70 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - - -def main(): - create_db_and_tables() - create_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/read_relationships/__init__.py b/docs_src/tutorial/relationship_attributes/read_relationships/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py b/docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py deleted file mode 100644 index b8bbe70a4c..0000000000 --- a/docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py +++ /dev/null @@ -1,117 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - result = session.exec(statement) - hero_spider_boy = result.one() - - statement = select(Team).where(Team.id == hero_spider_boy.team_id) - result = session.exec(statement) - team = result.first() - print("Spider-Boy's team:", team) - - print("Spider-Boy's team again:", hero_spider_boy.team) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py b/docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py deleted file mode 100644 index abae89d2eb..0000000000 --- a/docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py +++ /dev/null @@ -1,115 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - team: Team | None = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - result = session.exec(statement) - hero_spider_boy = result.one() - - statement = select(Team).where(Team.id == hero_spider_boy.team_id) - result = session.exec(statement) - team = result.first() - print("Spider-Boy's team:", team) - - print("Spider-Boy's team again:", hero_spider_boy.team) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py b/docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py deleted file mode 100644 index 2219c82dab..0000000000 --- a/docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py +++ /dev/null @@ -1,117 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - result = session.exec(statement) - hero_spider_boy = result.one() - - statement = select(Team).where(Team.id == hero_spider_boy.team_id) - result = session.exec(statement) - team = result.first() - print("Spider-Boy's team:", team) - - print("Spider-Boy's team again:", hero_spider_boy.team) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py b/docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py deleted file mode 100644 index 30fa840878..0000000000 --- a/docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py +++ /dev/null @@ -1,127 +0,0 @@ -from typing import List, Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: List["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Preventers") - result = session.exec(statement) - team_preventers = result.one() - - print("Preventers heroes:", team_preventers.heroes) - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - result = session.exec(statement) - hero_spider_boy = result.one() - - hero_spider_boy.team = None - session.add(hero_spider_boy) - session.commit() - - session.refresh(hero_spider_boy) - print("Spider-Boy without team:", hero_spider_boy) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py b/docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py deleted file mode 100644 index 0221d8834c..0000000000 --- a/docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py +++ /dev/null @@ -1,125 +0,0 @@ -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - team_id: int | None = Field(default=None, foreign_key="team.id") - team: Team | None = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Preventers") - result = session.exec(statement) - team_preventers = result.one() - - print("Preventers heroes:", team_preventers.heroes) - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - result = session.exec(statement) - hero_spider_boy = result.one() - - hero_spider_boy.team = None - session.add(hero_spider_boy) - session.commit() - - session.refresh(hero_spider_boy) - print("Spider-Boy without team:", hero_spider_boy) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py b/docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py deleted file mode 100644 index 393bdc8a5e..0000000000 --- a/docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py +++ /dev/null @@ -1,127 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select - - -class Team(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - headquarters: str - - heroes: list["Hero"] = Relationship(back_populates="team") - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - team_id: Optional[int] = Field(default=None, foreign_key="team.id") - team: Optional[Team] = Relationship(back_populates="heroes") - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -def create_heroes(): - with Session(engine) as session: - team_preventers = Team(name="Preventers", headquarters="Sharp Tower") - 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 - ) - hero_rusty_man = Hero( - name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers - ) - 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) - session.commit() - - session.refresh(hero_deadpond) - session.refresh(hero_rusty_man) - 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 = team_preventers - session.add(hero_spider_boy) - session.commit() - session.refresh(hero_spider_boy) - print("Updated hero:", hero_spider_boy) - - hero_black_lion = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_sure_e = Hero(name="Princess Sure-E", secret_name="Sure-E") - team_wakaland = Team( - name="Wakaland", - headquarters="Wakaland Capital City", - heroes=[hero_black_lion, hero_sure_e], - ) - session.add(team_wakaland) - session.commit() - session.refresh(team_wakaland) - print("Team Wakaland:", team_wakaland) - - hero_tarantula = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_dr_weird = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_cap = Hero( - name="Captain North America", secret_name="Esteban Rogelios", age=93 - ) - - team_preventers.heroes.append(hero_tarantula) - team_preventers.heroes.append(hero_dr_weird) - team_preventers.heroes.append(hero_cap) - session.add(team_preventers) - session.commit() - session.refresh(hero_tarantula) - session.refresh(hero_dr_weird) - session.refresh(hero_cap) - print("Preventers new hero:", hero_tarantula) - print("Preventers new hero:", hero_dr_weird) - print("Preventers new hero:", hero_cap) - - -def select_heroes(): - with Session(engine) as session: - statement = select(Team).where(Team.name == "Preventers") - result = session.exec(statement) - team_preventers = result.one() - - print("Preventers heroes:", team_preventers.heroes) - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - result = session.exec(statement) - hero_spider_boy = result.one() - - hero_spider_boy.team = None - session.add(hero_spider_boy) - session.commit() - - session.refresh(hero_spider_boy) - print("Spider-Boy without team:", hero_spider_boy) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/select/__init__.py b/docs_src/tutorial/select/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/select/annotations/en/tutorial002.md b/docs_src/tutorial/select/annotations/en/tutorial002.md deleted file mode 100644 index fa7e6f3a55..0000000000 --- a/docs_src/tutorial/select/annotations/en/tutorial002.md +++ /dev/null @@ -1,66 +0,0 @@ -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/select/tutorial001.py b/docs_src/tutorial/select/tutorial001.py deleted file mode 100644 index 285f711221..0000000000 --- a/docs_src/tutorial/select/tutorial001.py +++ /dev/null @@ -1,51 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/select/tutorial001_py310.py b/docs_src/tutorial/select/tutorial001_py310.py deleted file mode 100644 index 29bce28251..0000000000 --- a/docs_src/tutorial/select/tutorial001_py310.py +++ /dev/null @@ -1,49 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/select/tutorial002.py b/docs_src/tutorial/select/tutorial002.py deleted file mode 100644 index 2229f3fc30..0000000000 --- a/docs_src/tutorial/select/tutorial002.py +++ /dev/null @@ -1,52 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select # (1)! - - -class Hero(SQLModel, table=True): # (2)! - 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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) # (3)! - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) # (4)! - - -def create_heroes(): - 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) - - with Session(engine) as session: # (6)! - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: # (7)! - statement = select(Hero) # (8)! - results = session.exec(statement) # (9)! - for hero in results: # (10)! - print(hero) # (11)! - # (12)! - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() # (13)! - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/select/tutorial002_py310.py b/docs_src/tutorial/select/tutorial002_py310.py deleted file mode 100644 index b2f9d4d22c..0000000000 --- a/docs_src/tutorial/select/tutorial002_py310.py +++ /dev/null @@ -1,50 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select # (1)! - - -class Hero(SQLModel, table=True): # (2)! - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) # (3)! - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) # (4)! - - -def create_heroes(): - 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) - - with Session(engine) as session: # (6)! - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: # (7)! - statement = select(Hero) # (8)! - results = session.exec(statement) # (9)! - for hero in results: # (10)! - print(hero) # (11)! - # (12)! - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() # (13)! - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/select/tutorial003.py b/docs_src/tutorial/select/tutorial003.py deleted file mode 100644 index 97eab13682..0000000000 --- a/docs_src/tutorial/select/tutorial003.py +++ /dev/null @@ -1,51 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero) - results = session.exec(statement) - heroes = results.all() - print(heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/select/tutorial003_py310.py b/docs_src/tutorial/select/tutorial003_py310.py deleted file mode 100644 index 836998e24e..0000000000 --- a/docs_src/tutorial/select/tutorial003_py310.py +++ /dev/null @@ -1,49 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero) - results = session.exec(statement) - heroes = results.all() - print(heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/select/tutorial004.py b/docs_src/tutorial/select/tutorial004.py deleted file mode 100644 index e9ddbbb2bf..0000000000 --- a/docs_src/tutorial/select/tutorial004.py +++ /dev/null @@ -1,49 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - print(heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/select/tutorial004_py310.py b/docs_src/tutorial/select/tutorial004_py310.py deleted file mode 100644 index 6366d40865..0000000000 --- a/docs_src/tutorial/select/tutorial004_py310.py +++ /dev/null @@ -1,47 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - heroes = session.exec(select(Hero)).all() - print(heroes) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/update/__init__.py b/docs_src/tutorial/update/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/update/annotations/en/tutorial002.md b/docs_src/tutorial/update/annotations/en/tutorial002.md deleted file mode 100644 index 7cb7c28ba8..0000000000 --- a/docs_src/tutorial/update/annotations/en/tutorial002.md +++ /dev/null @@ -1,71 +0,0 @@ -1. Select the hero we will work with. - -2. Execute the query with the select statement object. - - This generates the output: - - ``` - INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age - FROM hero - WHERE hero.name = ? - INFO Engine [no key 0.00017s] ('Spider-Boy',) - ``` - -3. Get one hero object, expecting exactly one. - - /// tip - - This ensures there's no more than one, and that there's exactly one, not `None`. - - This would never return `None`, instead it would raise an exception. - - /// - -4. Print the hero object. - - This generates the output: - - ``` - Hero: name='Spider-Boy' secret_name='Pedro Parqueador' age=None id=2 - ``` - -5. Set the hero's age field to the new value `16`. - - Now the `hero` object in memory has a different value for the age, but it is still not saved to the database. - -6. Add the hero to the session. - - This puts it in that temporary place in the session before committing. - - But it's still not saved in the database yet. - -7. Commit the session. - - This saves the updated hero to the database. - - And this generates the output: - - ``` - INFO Engine UPDATE hero SET age=? WHERE hero.id = ? - INFO Engine [generated in 0.00017s] (16, 2) - INFO Engine COMMIT - ``` - -8. Refresh the hero object to have the recent data, including the age we just committed. - - This generates the output: - - ``` - INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age - FROM hero - WHERE hero.id = ? - INFO Engine [generated in 0.00018s] (2,) - ``` - -9. Print the updated hero object. - - This generates the output: - - ``` - Updated hero: name='Spider-Boy' secret_name='Pedro Parqueador' age=16 id=2 - ``` diff --git a/docs_src/tutorial/update/annotations/en/tutorial004.md b/docs_src/tutorial/update/annotations/en/tutorial004.md deleted file mode 100644 index bbb713ead5..0000000000 --- a/docs_src/tutorial/update/annotations/en/tutorial004.md +++ /dev/null @@ -1,171 +0,0 @@ -1. Select the hero `Spider-Boy`. - -2. Execute the select statement. - - This generates the output: - - ``` - INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age - FROM hero - WHERE hero.name = ? - INFO Engine [no key 0.00018s] ('Spider-Boy',) - ``` - -3. Get one hero object, the only one that should be there for **Spider-Boy**. - -4. Print this hero. - - This generates the output: - - ``` - Hero 1: name='Spider-Boy' secret_name='Pedro Parqueador' age=None id=2 - ``` - -5. Select another hero. - -6. Execute the select statement. - - This generates the output: - - ``` - INFO Engine BEGIN (implicit) - INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age - FROM hero - WHERE hero.name = ? - INFO Engine [no key 0.00020s] ('Captain North America',) - ``` - - /// tip - - See the `BEGIN` at the top? - - This is SQLAlchemy automatically starting a transaction for us. - - This way, we could revert the last changes (if there were some) if we wanted to, even if the SQL to create them was already sent to the database. - - /// - -7. Get one hero object for this new query. - - The only one that should be there for **Captain North America**. - -8. Print this second hero. - - This generates the output: - - ``` - Hero 2: name='Captain North America' secret_name='Esteban Rogelios' age=93 id=7 - ``` - -9. Update the age for the first hero. - - Set the value of the attribute `age` to `16`. - - This updates the hero object in memory, but not yet in the database. - -10. Update the name of the first hero. - - Now the name of the hero will not be `"Spider-Boy"` but `"Spider-Youngster"`. - - Also, this updates the object in memory, but not yet in the database. - -11. Add this first hero to the session. - - This puts it in the temporary space in the **session** before committing it to the database. - - It is not saved yet. - -12. Update the name of the second hero. - - Now the hero has a bit more precision in the name. 😜 - - This updates the object in memory, but not yet in the database. - -13. Update the age of the second hero. - - This updates the object in memory, but not yet in the database. - -14. Add the second hero to the session. - - This puts it in the temporary space in the **session** before committing it to the database. - -15. Commit all the changes tracked in the session. - - This commits everything in one single batch. - - This generates the output: - - ``` - INFO Engine UPDATE hero SET name=?, age=? WHERE hero.id = ? - INFO Engine [generated in 0.00028s] (('Spider-Youngster', 16, 2), ('Captain North America Except Canada', 110, 7)) - INFO Engine COMMIT - ``` - - /// tip - - See how SQLAlchemy (that powers SQLModel) optimizes the SQL to do as much work as possible in a single batch. - - Here it updates both heroes in a single SQL query. - - /// - -16. Refresh the first hero. - - This generates the output: - - ``` - INFO Engine BEGIN (implicit) - INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age - FROM hero - WHERE hero.id = ? - INFO Engine [generated in 0.00023s] (2,) - ``` - - /// tip - - Because we just committed a SQL transaction with `COMMIT`, SQLAlchemy will automatically start a new transaction with `BEGIN`. - - /// - -17. Refresh the second hero. - - This generates the output: - - ``` - INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age - FROM hero - WHERE hero.id = ? - INFO Engine [cached since 0.001709s ago] (7,) - ``` - - /// tip - - SQLAlchemy is still using the previous transaction, so it doesn't have to create a new one. - - /// - -18. Print the first hero, now updated. - - This generates the output: - - ``` - Updated hero 1: name='Spider-Youngster' secret_name='Pedro Parqueador' age=16 id=2 - ``` - -19. Print the second hero, now updated. - - This generates the output: - - ``` - Updated hero 2: name='Captain North America Except Canada' secret_name='Esteban Rogelios' age=110 id=7 - ``` - -20. Here is the end of the `with` block statement, so the session can execute its terminating code. - - The session will `ROLLBACK` (undo) any possible changes in the last transaction that were not committed. - - This generates the output: - - ``` - INFO Engine ROLLBACK - ``` diff --git a/docs_src/tutorial/update/tutorial001.py b/docs_src/tutorial/update/tutorial001.py deleted file mode 100644 index 37868acc96..0000000000 --- a/docs_src/tutorial/update/tutorial001.py +++ /dev/null @@ -1,65 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - results = session.exec(statement) - hero = results.one() - print("Hero:", hero) - - hero.age = 16 - session.add(hero) - session.commit() - session.refresh(hero) - print("Updated hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/update/tutorial001_py310.py b/docs_src/tutorial/update/tutorial001_py310.py deleted file mode 100644 index 6cc4f48934..0000000000 --- a/docs_src/tutorial/update/tutorial001_py310.py +++ /dev/null @@ -1,63 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - results = session.exec(statement) - hero = results.one() - print("Hero:", hero) - - hero.age = 16 - session.add(hero) - session.commit() - session.refresh(hero) - print("Updated hero:", hero) - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/update/tutorial002.py b/docs_src/tutorial/update/tutorial002.py deleted file mode 100644 index 572198563c..0000000000 --- a/docs_src/tutorial/update/tutorial002.py +++ /dev/null @@ -1,65 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") # (1)! - results = session.exec(statement) # (2)! - hero = results.one() # (3)! - print("Hero:", hero) # (4)! - - hero.age = 16 # (5)! - session.add(hero) # (6)! - session.commit() # (7)! - session.refresh(hero) # (8)! - print("Updated hero:", hero) # (9)! - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/update/tutorial002_py310.py b/docs_src/tutorial/update/tutorial002_py310.py deleted file mode 100644 index 64cb6916d8..0000000000 --- a/docs_src/tutorial/update/tutorial002_py310.py +++ /dev/null @@ -1,63 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") # (1)! - results = session.exec(statement) # (2)! - hero = results.one() # (3)! - print("Hero:", hero) # (4)! - - hero.age = 16 # (5)! - session.add(hero) # (6)! - session.commit() # (7)! - session.refresh(hero) # (8)! - print("Updated hero:", hero) # (9)! - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/update/tutorial003.py b/docs_src/tutorial/update/tutorial003.py deleted file mode 100644 index fd2ed75f0b..0000000000 --- a/docs_src/tutorial/update/tutorial003.py +++ /dev/null @@ -1,79 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - results = session.exec(statement) - hero_1 = results.one() - print("Hero 1:", hero_1) - - statement = select(Hero).where(Hero.name == "Captain North America") - results = session.exec(statement) - hero_2 = results.one() - print("Hero 2:", hero_2) - - hero_1.age = 16 - hero_1.name = "Spider-Youngster" - session.add(hero_1) - - hero_2.name = "Captain North America Except Canada" - hero_2.age = 110 - session.add(hero_2) - - session.commit() - session.refresh(hero_1) - session.refresh(hero_2) - - print("Updated hero 1:", hero_1) - print("Updated hero 2:", hero_2) - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/update/tutorial003_py310.py b/docs_src/tutorial/update/tutorial003_py310.py deleted file mode 100644 index f250b071c1..0000000000 --- a/docs_src/tutorial/update/tutorial003_py310.py +++ /dev/null @@ -1,77 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") - results = session.exec(statement) - hero_1 = results.one() - print("Hero 1:", hero_1) - - statement = select(Hero).where(Hero.name == "Captain North America") - results = session.exec(statement) - hero_2 = results.one() - print("Hero 2:", hero_2) - - hero_1.age = 16 - hero_1.name = "Spider-Youngster" - session.add(hero_1) - - hero_2.name = "Captain North America Except Canada" - hero_2.age = 110 - session.add(hero_2) - - session.commit() - session.refresh(hero_1) - session.refresh(hero_2) - - print("Updated hero 1:", hero_1) - print("Updated hero 2:", hero_2) - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/update/tutorial004.py b/docs_src/tutorial/update/tutorial004.py deleted file mode 100644 index c74316e7b6..0000000000 --- a/docs_src/tutorial/update/tutorial004.py +++ /dev/null @@ -1,80 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: Optional[int] = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: Optional[int] = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") # (1)! - results = session.exec(statement) # (2)! - hero_1 = results.one() # (3)! - print("Hero 1:", hero_1) # (4)! - - statement = select(Hero).where(Hero.name == "Captain North America") # (5)! - results = session.exec(statement) # (6)! - hero_2 = results.one() # (7)! - print("Hero 2:", hero_2) # (8)! - - hero_1.age = 16 # (9)! - hero_1.name = "Spider-Youngster" # (10)! - session.add(hero_1) # (11)! - - hero_2.name = "Captain North America Except Canada" # (12)! - hero_2.age = 110 # (13)! - session.add(hero_2) # (14)! - - session.commit() # (15)! - session.refresh(hero_1) # (16)! - session.refresh(hero_2) # (17)! - - print("Updated hero 1:", hero_1) # (18)! - print("Updated hero 2:", hero_2) # (19)! - # (20)! - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/update/tutorial004_py310.py b/docs_src/tutorial/update/tutorial004_py310.py deleted file mode 100644 index 09e54e1cce..0000000000 --- a/docs_src/tutorial/update/tutorial004_py310.py +++ /dev/null @@ -1,78 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str = Field(index=True) - secret_name: str - age: int | None = Field(default=None, index=True) - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def update_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Spider-Boy") # (1)! - results = session.exec(statement) # (2)! - hero_1 = results.one() # (3)! - print("Hero 1:", hero_1) # (4)! - - statement = select(Hero).where(Hero.name == "Captain North America") # (5)! - results = session.exec(statement) # (6)! - hero_2 = results.one() # (7)! - print("Hero 2:", hero_2) # (8)! - - hero_1.age = 16 # (9)! - hero_1.name = "Spider-Youngster" # (10)! - session.add(hero_1) # (11)! - - hero_2.name = "Captain North America Except Canada" # (12)! - hero_2.age = 110 # (13)! - session.add(hero_2) # (14)! - - session.commit() # (15)! - session.refresh(hero_1) # (16)! - session.refresh(hero_2) # (17)! - - print("Updated hero 1:", hero_1) # (18)! - print("Updated hero 2:", hero_2) # (19)! - # (20)! - - -def main(): - create_db_and_tables() - create_heroes() - update_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/__init__.py b/docs_src/tutorial/where/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs_src/tutorial/where/tutorial001.py b/docs_src/tutorial/where/tutorial001.py deleted file mode 100644 index 71fcbc2626..0000000000 --- a/docs_src/tutorial/where/tutorial001.py +++ /dev/null @@ -1,51 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Deadpond") - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial001_py310.py b/docs_src/tutorial/where/tutorial001_py310.py deleted file mode 100644 index a59e5fc281..0000000000 --- a/docs_src/tutorial/where/tutorial001_py310.py +++ /dev/null @@ -1,49 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name == "Deadpond") - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial002.py b/docs_src/tutorial/where/tutorial002.py deleted file mode 100644 index 7e34448726..0000000000 --- a/docs_src/tutorial/where/tutorial002.py +++ /dev/null @@ -1,51 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name != "Deadpond") - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial002_py310.py b/docs_src/tutorial/where/tutorial002_py310.py deleted file mode 100644 index 5db10c5808..0000000000 --- a/docs_src/tutorial/where/tutorial002_py310.py +++ /dev/null @@ -1,49 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.name != "Deadpond") - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial003.py b/docs_src/tutorial/where/tutorial003.py deleted file mode 100644 index 2aa31ccaf7..0000000000 --- a/docs_src/tutorial/where/tutorial003.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age > 35) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial003_py310.py b/docs_src/tutorial/where/tutorial003_py310.py deleted file mode 100644 index c368add499..0000000000 --- a/docs_src/tutorial/where/tutorial003_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age > 35) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial004.py b/docs_src/tutorial/where/tutorial004.py deleted file mode 100644 index eeec6aaa5e..0000000000 --- a/docs_src/tutorial/where/tutorial004.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age >= 35) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial004_py310.py b/docs_src/tutorial/where/tutorial004_py310.py deleted file mode 100644 index 5733b71795..0000000000 --- a/docs_src/tutorial/where/tutorial004_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age >= 35) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial005.py b/docs_src/tutorial/where/tutorial005.py deleted file mode 100644 index 505663e274..0000000000 --- a/docs_src/tutorial/where/tutorial005.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age < 35) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial005_py310.py b/docs_src/tutorial/where/tutorial005_py310.py deleted file mode 100644 index 5251506970..0000000000 --- a/docs_src/tutorial/where/tutorial005_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age < 35) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial006.py b/docs_src/tutorial/where/tutorial006.py deleted file mode 100644 index 91bbca5615..0000000000 --- a/docs_src/tutorial/where/tutorial006.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age <= 35) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial006_py310.py b/docs_src/tutorial/where/tutorial006_py310.py deleted file mode 100644 index a3ab8507e3..0000000000 --- a/docs_src/tutorial/where/tutorial006_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age <= 35) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial007.py b/docs_src/tutorial/where/tutorial007.py deleted file mode 100644 index 97250b54fd..0000000000 --- a/docs_src/tutorial/where/tutorial007.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age >= 35).where(Hero.age < 40) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial007_py310.py b/docs_src/tutorial/where/tutorial007_py310.py deleted file mode 100644 index 589bc98671..0000000000 --- a/docs_src/tutorial/where/tutorial007_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age >= 35).where(Hero.age < 40) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial008.py b/docs_src/tutorial/where/tutorial008.py deleted file mode 100644 index 41d2ae6772..0000000000 --- a/docs_src/tutorial/where/tutorial008.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age >= 35, Hero.age < 40) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial008_py310.py b/docs_src/tutorial/where/tutorial008_py310.py deleted file mode 100644 index f32260c9f9..0000000000 --- a/docs_src/tutorial/where/tutorial008_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(Hero.age >= 35, Hero.age < 40) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial009.py b/docs_src/tutorial/where/tutorial009.py deleted file mode 100644 index 9f69d65663..0000000000 --- a/docs_src/tutorial/where/tutorial009.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_engine, or_, 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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(or_(Hero.age <= 35, Hero.age > 90)) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial009_py310.py b/docs_src/tutorial/where/tutorial009_py310.py deleted file mode 100644 index 0681d1c0a5..0000000000 --- a/docs_src/tutorial/where/tutorial009_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, or_, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(or_(Hero.age <= 35, Hero.age > 90)) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial010.py b/docs_src/tutorial/where/tutorial010.py deleted file mode 100644 index c35eb2c7ba..0000000000 --- a/docs_src/tutorial/where/tutorial010.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, create_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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where((Hero.age <= 35) | (Hero.age > 90)) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial010_py310.py b/docs_src/tutorial/where/tutorial010_py310.py deleted file mode 100644 index a65c47acf2..0000000000 --- a/docs_src/tutorial/where/tutorial010_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where((Hero.age <= 35) | (Hero.age > 90)) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial011.py b/docs_src/tutorial/where/tutorial011.py deleted file mode 100644 index e365a3acfb..0000000000 --- a/docs_src/tutorial/where/tutorial011.py +++ /dev/null @@ -1,59 +0,0 @@ -from typing import Optional - -from sqlmodel import Field, Session, SQLModel, col, create_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:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(col(Hero.age) >= 35) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/docs_src/tutorial/where/tutorial011_py310.py b/docs_src/tutorial/where/tutorial011_py310.py deleted file mode 100644 index 73aa4aa8c0..0000000000 --- a/docs_src/tutorial/where/tutorial011_py310.py +++ /dev/null @@ -1,57 +0,0 @@ -from sqlmodel import Field, Session, SQLModel, col, create_engine, select - - -class Hero(SQLModel, table=True): - id: int | None = Field(default=None, primary_key=True) - name: str - secret_name: str - age: int | None = None - - -sqlite_file_name = "database.db" -sqlite_url = f"sqlite:///{sqlite_file_name}" - -engine = create_engine(sqlite_url, echo=True) - - -def create_db_and_tables(): - SQLModel.metadata.create_all(engine) - - -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) - hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) - hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) - hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) - hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) - - with Session(engine) as session: - session.add(hero_1) - session.add(hero_2) - session.add(hero_3) - session.add(hero_4) - session.add(hero_5) - session.add(hero_6) - session.add(hero_7) - - session.commit() - - -def select_heroes(): - with Session(engine) as session: - statement = select(Hero).where(col(Hero.age) >= 35) - results = session.exec(statement) - for hero in results: - print(hero) - - -def main(): - create_db_and_tables() - create_heroes() - select_heroes() - - -if __name__ == "__main__": - main() diff --git a/mkdocs.insiders.yml b/mkdocs.insiders.yml deleted file mode 100644 index 80d2d4b640..0000000000 --- a/mkdocs.insiders.yml +++ /dev/null @@ -1,7 +0,0 @@ -plugins: - typeset: -markdown_extensions: - material.extensions.preview: - targets: - include: - - "*" diff --git a/mkdocs.maybe-insiders.yml b/mkdocs.maybe-insiders.yml deleted file mode 100644 index 07aefaaa99..0000000000 --- a/mkdocs.maybe-insiders.yml +++ /dev/null @@ -1,6 +0,0 @@ -# Define this here and not in the main mkdocs.yml file because that one could be auto -# updated and written, and the script would remove the env var -INHERIT: !ENV [INSIDERS_FILE, './mkdocs.no-insiders.yml'] -markdown_extensions: - pymdownx.highlight: - linenums: !ENV [LINENUMS, false] diff --git a/mkdocs.no-insiders.yml b/mkdocs.no-insiders.yml deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/mkdocs.yml b/mkdocs.yml deleted file mode 100644 index c59ccd245a..0000000000 --- a/mkdocs.yml +++ /dev/null @@ -1,213 +0,0 @@ -INHERIT: ./mkdocs.maybe-insiders.yml -site_name: SQLModel -site_description: SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness. -site_url: https://sqlmodel.tiangolo.com/ -theme: - name: material - custom_dir: docs/overrides - palette: - - media: "(prefers-color-scheme)" - toggle: - icon: material/lightbulb-auto - name: Switch to light mode - - media: '(prefers-color-scheme: light)' - scheme: default - primary: deep purple - accent: amber - toggle: - icon: material/lightbulb - name: Switch to dark mode - - media: '(prefers-color-scheme: dark)' - scheme: slate - primary: deep purple - accent: amber - toggle: - icon: material/lightbulb-outline - name: Switch to system preference - features: - - content.code.annotate - - content.code.copy - # - content.code.select - - content.footnote.tooltips - - content.tabs.link - - content.tooltips - - navigation.footer - - navigation.indexes - - navigation.instant - - navigation.instant.prefetch - # - navigation.instant.preview - - navigation.instant.progress - - navigation.path - - navigation.tabs - - navigation.tabs.sticky - - navigation.top - - navigation.tracking - - search.highlight - - search.share - - search.suggest - - toc.follow - - icon: - repo: fontawesome/brands/github-alt - logo: img/icon-white.svg - favicon: img/favicon.png - language: en -repo_name: fastapi/sqlmodel -repo_url: https://github.com/fastapi/sqlmodel -plugins: - # Material for MkDocs - search: - social: - # Other plugins - macros: - include_yaml: - - sponsors: data/sponsors.yml - - members: data/members.yml - -nav: - - SQLModel: index.md - - features.md - - Learn: - - learn/index.md - - databases.md - - db-to-code.md - - environment-variables.md - - virtual-environments.md - - install.md - - Tutorial - User Guide: - - tutorial/index.md - - tutorial/create-db-and-table-with-db-browser.md - - tutorial/create-db-and-table.md - - tutorial/insert.md - - tutorial/automatic-id-none-refresh.md - - tutorial/select.md - - tutorial/where.md - - tutorial/indexes.md - - tutorial/one.md - - tutorial/limit-and-offset.md - - tutorial/update.md - - tutorial/delete.md - - Connect Tables - JOIN: - - tutorial/connect/index.md - - tutorial/connect/create-connected-tables.md - - tutorial/connect/create-connected-rows.md - - tutorial/connect/read-connected-data.md - - tutorial/connect/update-data-connections.md - - tutorial/connect/remove-data-connections.md - - Relationship Attributes: - - tutorial/relationship-attributes/index.md - - tutorial/relationship-attributes/define-relationships-attributes.md - - tutorial/relationship-attributes/create-and-update-relationships.md - - tutorial/relationship-attributes/read-relationships.md - - tutorial/relationship-attributes/remove-relationships.md - - tutorial/relationship-attributes/back-populates.md - - tutorial/relationship-attributes/cascade-delete-relationships.md - - tutorial/relationship-attributes/type-annotation-strings.md - - Many to Many: - - tutorial/many-to-many/index.md - - tutorial/many-to-many/create-models-with-link.md - - tutorial/many-to-many/create-data.md - - tutorial/many-to-many/update-remove-relationships.md - - tutorial/many-to-many/link-with-extra-fields.md - - tutorial/code-structure.md - - FastAPI and Pydantic: - - tutorial/fastapi/index.md - - tutorial/fastapi/simple-hero-api.md - - tutorial/fastapi/response-model.md - - tutorial/fastapi/multiple-models.md - - tutorial/fastapi/read-one.md - - tutorial/fastapi/limit-and-offset.md - - tutorial/fastapi/update.md - - tutorial/fastapi/update-extra-data.md - - tutorial/fastapi/delete.md - - tutorial/fastapi/session-with-dependency.md - - tutorial/fastapi/teams.md - - tutorial/fastapi/relationships.md - - tutorial/fastapi/tests.md - - Advanced User Guide: - - advanced/index.md - - advanced/decimal.md - - advanced/uuid.md - - Resources: - - resources/index.md - - help.md - - contributing.md - - management-tasks.md - - About: - - about/index.md - - alternatives.md - - management.md - - release-notes.md - -markdown_extensions: - # Python Markdown - abbr: - attr_list: - footnotes: - md_in_html: - tables: - toc: - permalink: true - - # Python Markdown Extensions - pymdownx.betterem: - smart_enable: all - pymdownx.caret: - pymdownx.highlight: - line_spans: __span - pymdownx.inlinehilite: - pymdownx.keys: - pymdownx.mark: - pymdownx.superfences: - custom_fences: - - name: mermaid - class: mermaid - format: !!python/name:pymdownx.superfences.fence_code_format - pymdownx.tilde: - - # pymdownx blocks - pymdownx.blocks.admonition: - types: - - note - - attention - - caution - - danger - - error - - tip - - hint - - warning - # Custom types - - info - pymdownx.blocks.details: - pymdownx.blocks.tab: - alternate_style: True - - # Other extensions - mdx_include: - markdown_include_variants: - -extra: - social: - - icon: fontawesome/brands/github-alt - link: https://github.com/fastapi/sqlmodel - - icon: fontawesome/brands/twitter - link: https://twitter.com/tiangolo - - icon: fontawesome/brands/linkedin - link: https://www.linkedin.com/in/tiangolo - - icon: fontawesome/brands/dev - link: https://dev.to/tiangolo - - icon: fontawesome/brands/medium - link: https://medium.com/@tiangolo - - icon: fontawesome/solid/globe - link: https://tiangolo.com - -extra_css: - - css/termynal.css - - css/custom.css - -extra_javascript: - - js/termynal.js - - js/custom.js - -hooks: - - scripts/mkdocs_hooks.py diff --git a/pdm_build.py b/pdm_build.py deleted file mode 100644 index 2324670159..0000000000 --- a/pdm_build.py +++ /dev/null @@ -1,39 +0,0 @@ -import os -from typing import Any, Dict, List - -from pdm.backend.hooks import Context - -TIANGOLO_BUILD_PACKAGE = os.getenv("TIANGOLO_BUILD_PACKAGE", "sqlmodel") - - -def pdm_build_initialize(context: Context) -> None: - metadata = context.config.metadata - # Get custom config for the current package, from the env var - config: Dict[str, Any] = context.config.data["tool"]["tiangolo"][ - "_internal-slim-build" - ]["packages"][TIANGOLO_BUILD_PACKAGE] - project_config: Dict[str, Any] = config["project"] - # Get main optional dependencies, extras - optional_dependencies: Dict[str, List[str]] = metadata.get( - "optional-dependencies", {} - ) - # Get custom optional dependencies name to always include in this (non-slim) package - include_optional_dependencies: List[str] = config.get( - "include-optional-dependencies", [] - ) - # Override main [project] configs with custom configs for this package - for key, value in project_config.items(): - metadata[key] = value - # Get custom build config for the current package - build_config: Dict[str, Any] = ( - config.get("tool", {}).get("pdm", {}).get("build", {}) - ) - # Override PDM build config with custom build config for this package - for key, value in build_config.items(): - context.config.build_config[key] = value - # Get main dependencies - dependencies: List[str] = metadata.get("dependencies", []) - # Add optional dependencies to the default dependencies for this (non-slim) package - for include_optional in include_optional_dependencies: - optional_dependencies_group = optional_dependencies.get(include_optional, []) - dependencies.extend(optional_dependencies_group) diff --git a/pyproject.toml b/pyproject.toml index cd47f5ecd9..c4cfcb43cc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,30 +1,26 @@ [build-system] -requires = ["pdm-backend"] -build-backend = "pdm.backend" +requires = ["uv_build>=0.9.6,<0.10.0"] +build-backend = "uv_build" [project] -name = "sqlmodel" +name = "grpcmodel" dynamic = ["version"] -description = "SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness." +description = "gRPCModel, gRPC models and schemas in Python, designed for simplicity, compatibility, and robustness." readme = "README.md" -requires-python = ">=3.8" +requires-python = ">=3.13" authors = [ - { name = "SebastiΓ‘n RamΓ­rez", email = "tiangolo@gmail.com" }, + { name = "River Studio", email = "git@river-studio.net" }, ] classifiers = [ "Development Status :: 4 - Beta", "Framework :: AsyncIO", + "Framework :: gRPC", "Intended Audience :: Developers", "Intended Audience :: Science/Research", "Intended Audience :: System Administrators", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", - "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", "Programming Language :: Python :: 3.14", "Topic :: Database", @@ -37,46 +33,21 @@ classifiers = [ dependencies = [ "SQLAlchemy >=2.0.14,<2.1.0", + "protobuf>=5.29.5", "pydantic >=1.10.13,<3.0.0", ] [project.urls] -Homepage = "https://github.com/fastapi/sqlmodel" -Documentation = "https://sqlmodel.tiangolo.com" -Repository = "https://github.com/fastapi/sqlmodel" -Issues = "https://github.com/fastapi/sqlmodel/issues" -Changelog = "https://sqlmodel.tiangolo.com/release-notes/" - -[tool.pdm] -version = { source = "file", path = "sqlmodel/__init__.py" } -distribution = true - -[tool.pdm.build] -source-includes = [ - "tests/", - "docs_src/", - "requirements*.txt", - "scripts/", - "sqlmodel/sql/expression.py.jinja2", - ] - -[tool.tiangolo._internal-slim-build.packages.sqlmodel-slim.project] -name = "sqlmodel-slim" - -[tool.tiangolo._internal-slim-build.packages.sqlmodel] -# include-optional-dependencies = ["standard"] - -[tool.tiangolo._internal-slim-build.packages.sqlmodel.project] -optional-dependencies = {} - -# [tool.tiangolo._internal-slim-build.packages.sqlmodel.project.scripts] -# sqlmodel = "sqlmodel.cli:main" +Homepage = "https://github.com/river-studio-net/grpcmodel" +Documentation = "https://github.com/river-studio-net/grpcmodel?tab=readme-ov-file" +Repository = "https://github.com/river-studio-net/grpcmodel" +Issues = "https://github.com/river-studio-net/grpcmodel/issues" +Changelog = "https://github.com/river-studio-net/grpcmodel/releases" [tool.coverage.run] parallel = true data_file = "coverage/.coverage" source = [ - "docs_src", "tests", "sqlmodel" ] diff --git a/requirements-docs-insiders.txt b/requirements-docs-insiders.txt deleted file mode 100644 index d8d3c37a9f..0000000000 --- a/requirements-docs-insiders.txt +++ /dev/null @@ -1,3 +0,0 @@ -git+https://${TOKEN}@github.com/squidfunk/mkdocs-material-insiders.git@9.5.30-insiders-4.53.11 -git+https://${TOKEN}@github.com/pawamoy-insiders/griffe-typing-deprecated.git -git+https://${TOKEN}@github.com/pawamoy-insiders/mkdocstrings-python.git diff --git a/requirements-docs-tests.txt b/requirements-docs-tests.txt deleted file mode 100644 index c65317a7cf..0000000000 --- a/requirements-docs-tests.txt +++ /dev/null @@ -1,2 +0,0 @@ -# For mkdocstrings and code generator using templates -black >=22.10 diff --git a/requirements-docs.txt b/requirements-docs.txt deleted file mode 100644 index d0c0b1a728..0000000000 --- a/requirements-docs.txt +++ /dev/null @@ -1,18 +0,0 @@ --e . --r requirements-docs-tests.txt -mkdocs-material==9.6.22 -mdx-include >=1.4.1,<2.0.0 -mkdocs-redirects>=1.2.1,<1.3.0 -pyyaml >=5.3.1,<7.0.0 -# For Material for MkDocs, Chinese search -# jieba==0.42.1 -# For image processing by Material for MkDocs -pillow==11.3.0 -# For image processing by Material for MkDocs -cairosvg==2.8.2 -# mkdocstrings[python]==0.25.1 -griffe-typingdoc==0.2.9 -# For griffe, it formats with black -typer == 0.20.0 -mkdocs-macros-plugin==1.4.0 -markdown-include-variants==0.0.5 diff --git a/requirements-github-actions.txt b/requirements-github-actions.txt deleted file mode 100644 index 5c3e02d8ae..0000000000 --- a/requirements-github-actions.txt +++ /dev/null @@ -1,5 +0,0 @@ -PyGithub>=2.3.0,<3.0.0 -pydantic>=2.5.3,<3.0.0 -pydantic-settings>=2.1.0,<3.0.0 -httpx>=0.27.0,<0.29.0 -smokeshow diff --git a/requirements-tests.txt b/requirements-tests.txt deleted file mode 100644 index 70dc2a9a09..0000000000 --- a/requirements-tests.txt +++ /dev/null @@ -1,16 +0,0 @@ --e . --r requirements-docs-tests.txt -pytest >=7.0.1,<9.0.0 -coverage[toml] >=6.2,<8.0 -# Remove when support for Python 3.8 is dropped -mypy ==1.14.1; python_version < "3.9" -mypy ==1.18.2; python_version >= "3.9" -ruff ==0.14.1 -# For FastAPI tests -fastapi >=0.103.2 -httpx ==0.28.1 -dirty-equals ==0.9.0 -jinja2 ==3.1.6 -# Remove when support for Python 3.8 is dropped -typing-extensions ==4.13.2; python_version < "3.9" -typing-extensions ==4.15.0; python_version >= "3.9" diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index f17705f380..0000000000 --- a/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ --e . - --r requirements-tests.txt --r requirements-docs.txt - -pre-commit >=2.17.0,<5.0.0 diff --git a/scripts/deploy_docs_status.py b/scripts/deploy_docs_status.py deleted file mode 100644 index 635711bdbf..0000000000 --- a/scripts/deploy_docs_status.py +++ /dev/null @@ -1,127 +0,0 @@ -import logging -import re -from typing import Literal - -from github import Auth, Github -from pydantic import BaseModel, SecretStr -from pydantic_settings import BaseSettings - -site_domain = "sqlmodel.tiangolo.com" - - -class Settings(BaseSettings): - github_repository: str - github_token: SecretStr - deploy_url: str | None = None - commit_sha: str - run_id: int - state: Literal["pending", "success", "error"] = "pending" - - -class LinkData(BaseModel): - previous_link: str - preview_link: str - - -def main() -> None: - logging.basicConfig(level=logging.INFO) - settings = Settings() - - logging.info(f"Using config: {settings.model_dump_json()}") - g = Github(auth=Auth.Token(settings.github_token.get_secret_value())) - repo = g.get_repo(settings.github_repository) - use_pr = next( - (pr for pr in repo.get_pulls() if pr.head.sha == settings.commit_sha), None - ) - if not use_pr: - logging.error(f"No PR found for hash: {settings.commit_sha}") - return - commits = list(use_pr.get_commits()) - current_commit = [c for c in commits if c.sha == settings.commit_sha][0] - run_url = f"https://github.com/{settings.github_repository}/actions/runs/{settings.run_id}" - if settings.state == "pending": - current_commit.create_status( - state="pending", - description="Deploying Docs", - context="deploy-docs", - target_url=run_url, - ) - logging.info("No deploy URL available yet") - return - if settings.state == "error": - current_commit.create_status( - state="error", - description="Error Deploying Docs", - context="deploy-docs", - target_url=run_url, - ) - logging.info("Error deploying docs") - return - assert settings.state == "success" - if not settings.deploy_url: - current_commit.create_status( - state="success", - description="No Docs Changes", - context="deploy-docs", - target_url=run_url, - ) - logging.info("No docs changes found") - return - assert settings.deploy_url - current_commit.create_status( - state="success", - description="Docs Deployed", - context="deploy-docs", - target_url=run_url, - ) - - files = list(use_pr.get_files()) - docs_files = [f for f in files if f.filename.startswith("docs/")] - - deploy_url = settings.deploy_url.rstrip("/") - links: list[LinkData] = [] - for f in docs_files: - match = re.match(r"docs/(.*)", f.filename) - if not match: - continue - path = match.group(1) - if path.endswith("index.md"): - use_path = path.replace("index.md", "") - else: - use_path = path.replace(".md", "/") - link = LinkData( - previous_link=f"https://{site_domain}/{use_path}", - preview_link=f"{deploy_url}/{use_path}", - ) - links.append(link) - links.sort(key=lambda x: x.preview_link) - - header = "## πŸ“ Docs preview" - message = header - message += f"\n\nLast commit {settings.commit_sha} at: {deploy_url}" - - if links: - message += "\n\n### Modified Pages\n\n" - for link in links: - message += f"* {link.preview_link}" - message += f" - ([before]({link.previous_link}))" - message += "\n" - - print(message) - issue = use_pr.as_issue() - comments = list(issue.get_comments()) - for comment in comments: - if ( - comment.body.startswith(header) - and comment.user.login == "github-actions[bot]" - ): - comment.edit(message) - break - else: - issue.create_comment(message) - - logging.info("Finished") - - -if __name__ == "__main__": - main() diff --git a/scripts/docs.py b/scripts/docs.py deleted file mode 100644 index d018ace86f..0000000000 --- a/scripts/docs.py +++ /dev/null @@ -1,161 +0,0 @@ -import logging -import os -import re -import subprocess -from functools import lru_cache -from http.server import HTTPServer, SimpleHTTPRequestHandler -from importlib import metadata -from pathlib import Path - -import mkdocs.utils -import typer -from jinja2 import Template - -logging.basicConfig(level=logging.INFO) - -mkdocs_name = "mkdocs.yml" -en_docs_path = Path("") - -app = typer.Typer() - - -@lru_cache -def is_mkdocs_insiders() -> bool: - version = metadata.version("mkdocs-material") - return "insiders" in version - - -@app.callback() -def callback() -> None: - if is_mkdocs_insiders(): - os.environ["INSIDERS_FILE"] = "./mkdocs.insiders.yml" - # For MacOS with insiders and Cairo - os.environ["DYLD_FALLBACK_LIBRARY_PATH"] = "/opt/homebrew/lib" - - -index_sponsors_template = """ -{% if sponsors %} -{% for sponsor in sponsors.gold -%} - -{% endfor -%} -{%- for sponsor in sponsors.silver -%} - -{% endfor %} -{% endif %} -""" - - -def generate_readme_content() -> str: - en_index = en_docs_path / "docs" / "index.md" - content = en_index.read_text("utf-8") - match_pre = re.search(r"\n\n", content) - match_start = re.search(r"", content) - match_end = re.search(r"", content) - sponsors_data_path = en_docs_path / "data" / "sponsors.yml" - sponsors = mkdocs.utils.yaml_load(sponsors_data_path.read_text(encoding="utf-8")) - if not (match_start and match_end): - raise RuntimeError("Couldn't auto-generate sponsors section") - if not match_pre: - raise RuntimeError("Couldn't find pre section (