{"id":399612,"date":"2024-06-29T14:46:30","date_gmt":"2024-06-29T14:46:30","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=399612"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-29T14:46:30","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=399612","title":{"rendered":"<span>\u041a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c SQLAlchemy, SQLModel \u0438 Alembic \u0434\u043b\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 FastAPI<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043e\u043f\u044b\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 FastAPI \u0438 Postgres \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Docker. \u0412\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c, \u0447\u0442\u043e\u0431\u044b \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 FastAPI, Postgres \u0438 Docker? \u041d\u0430\u0447\u043d\u0438\u0442\u0435 \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/testdriven.io\/blog\/fastapi-crud\/\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e API \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e FastAPI \u0438 Pytest<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/testdriven.io\/courses\/tdd-fastapi\/\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e FastAPI \u0438 \u0434\u043e\u043a\u0435\u0440\u0430<\/a><\/p>\n<\/li>\n<\/ol>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/9ad\/034\/0bb\/9ad0340bb261b23c75a2a762efe78c67.jpg\" width=\"1082\" height=\"720\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9ad\/034\/0bb\/9ad0340bb261b23c75a2a762efe78c67.jpg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<h3>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430  <\/h3>\n<p>\u041d\u0430\u0447\u043d\u0438\u0442\u0435 \u0441 \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438\u0437 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f <a href=\"https:\/\/github.com\/testdrivenio\/fastapi-sqlmodel-alembic\" rel=\"noopener noreferrer nofollow\">fastapi-sqlmodel-alembic<\/a>:  <\/p>\n<pre><code class=\"python\">$ git clone -b base https:\/\/github.com\/testdrivenio\/fastapi-sqlmodel-alembic $ cd fastapi-sqlmodel-alembic<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u043c \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043e\u0431\u0440\u0430\u0437\u044b \u0438 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0438\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b Docker:  <\/p>\n<pre><code class=\"python\">$ docker-compose up -d --build<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0431\u043e\u0440\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0439\u0434\u0438\u0442\u0435 \u043a <a href=\"http:\/\/localhost:8004\/ping\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:8004\/ping<\/a>. \u0412\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c: <\/p>\n<pre><code class=\"python\">{   \"ping\": \"pong!\" }<\/code><\/pre>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435, \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043d\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0430.<\/p>\n<h2>SQLModel  <\/h2>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <a href=\"https:\/\/sqlmodel.tiangolo.com\/\" rel=\"noopener noreferrer nofollow\">SQLModel<\/a> \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 SQL-\u0431\u0430\u0437\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 Python. \u041f\u043e \u0441\u0443\u0442\u0438, \u044d\u0442\u043e \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0430 \u043f\u043e\u0432\u0435\u0440\u0445 <a href=\"https:\/\/pydantic-docs.helpmanual.io\/\" rel=\"noopener noreferrer nofollow\">pydantic<\/a> \u0438 <a href=\"https:\/\/www.sqlalchemy.org\/\" rel=\"noopener noreferrer nofollow\">SQLAlchemy<\/a>, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043d\u0430 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f\u0445 \u0442\u0438\u043f\u043e\u0432 Python.<\/p>\n<p>\u041d\u0430\u043c \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f <a href=\"https:\/\/www.psycopg.org\/\" rel=\"noopener noreferrer nofollow\">Psycopg<\/a>.<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0434\u0432\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 <em>project\/requirements.txt<\/em>:<\/p>\n<pre><code class=\"python\">fastapi==0.68.1 psycopg2-binary==2.9.1 sqlmodel==0.0.4 uvicorn==0.15.0<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0434\u0432\u0430 \u043d\u043e\u0432\u044b\u0445 \u0444\u0430\u0439\u043b\u0430 \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 &#171;\u043f\u0440\u043e\u0435\u043a\u0442\/\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435&#187;, <em>db.py<\/em> \u0438 <em>models.py<\/em>.<\/p>\n<p><em>project\/app\/models.py<\/em>:<\/p>\n<pre><code class=\"python\">from sqlmodel import SQLModel, Field   class SongBase(SQLModel):     name: str     artist: str   class Song(SongBase, table=True):     id: int = Field(default=None, primary_key=True)   class SongCreate(SongBase):     pass<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438 \u0442\u0440\u0438 \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<ol>\n<li>\n<p>SongBase \u044d\u0442\u043e \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u0438\u0435. \u0423 \u043d\u0435\u0451 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 name \u0438 artist, \u043e\u0431\u0430 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438. \u042d\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u044b\u0445 , \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u043d\u0435\u0439 \u043d\u0435\u0442 table=True, \u0430 \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u043e\u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 pydantic.<\/p>\n<\/li>\n<li>\n<p>Song \u0422\u0435\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 id \u0432 \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c. \u042d\u0442\u043e \u0442\u0430\u0431\u043b\u0438\u0447\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u0442\u0430\u043a \u0447\u0442\u043e \u044d\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c pydantic \u0438 SQLAlchemy. \u041e\u043d\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p>SongCreate \u044d\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c pydantic, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u044b\u0445 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 \u043f\u0435\u0441\u0435\u043d.<\/p>\n<\/li>\n<\/ol>\n<p><em>project\/app\/db.py<\/em>:<\/p>\n<pre><code class=\"python\">import os  from sqlmodel import create_engine, SQLModel, Session   DATABASE_URL = os.environ.get(\"DATABASE_URL\")  engine = create_engine(DATABASE_URL, echo=True)   def init_db():     SQLModel.metadata.create_all(engine)   def get_session():     with Session(engine) as session:         yield session<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b:<\/p>\n<ol>\n<li>\n<p>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b \u043d\u043e\u0432\u044b\u0439 <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/core\/engines.html\" rel=\"noopener noreferrer nofollow\">\u0434\u0432\u0438\u0436\u043e\u043a SQLAlchemy<\/a> \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c create_engine \u0438\u0437 SQLModel. \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u0440\u0441\u0438\u0435\u0439 create_engine \u0438\u0437 SQLModel \u0438 \u0432\u0435\u0440\u0441\u0438\u0435\u0439 SQLAlchemy \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u044e\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432\u0435\u0440\u0441\u0438\u044f SQLModel \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u0442\u0438\u043f\u043e\u0432 (\u0434\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0430) \u0438 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/core\/future.html\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0438\u043b\u044c \u0434\u0432\u0438\u0436\u043a\u043e\u0432 \u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 SQLAlchemy &#171;2.0&#187;<\/a>. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c echo=True \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0432\u0438\u0434\u0435\u0442\u044c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435. \u042d\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u0440\u0438\u044f\u0442\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0432 \u0440\u0435\u0436\u0438\u043c\u0435 \u043e\u0442\u043b\u0430\u0434\u043a\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043b\u0438 \u0441\u0435\u0430\u043d\u0441 SQLAlchemy.<\/p>\n<\/li>\n<\/ol>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0432\u043d\u0443\u0442\u0440\u0438 <em>project\/app\/main.py<\/em>, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u0440\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0438 startup:<\/p>\n<pre><code class=\"python\">from fastapi import FastAPI  from app.db import init_db from app.models import Song  app = FastAPI()   @app.on_event(\"startup\") def on_startup():     init_db()   @app.get(\"\/ping\") async def pong():     return {\"ping\": \"pong!\"}<\/code><\/pre>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u0442\u0440\u043e\u0447\u043a\u0430 \u00a0<code>from app.models import Song<\/code>  \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u0430. \u0411\u0435\u0437 \u043d\u0435\u0451 \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043f\u0435\u0441\u0435\u043d \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0430.<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0443\u0434\u0430\u043b\u0438\u0442\u0435 \u0441\u0442\u0430\u0440\u044b\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b \u0438 \u0442\u043e\u043c\u0430, \u0441\u043e\u0431\u0435\u0440\u0438\u0442\u0435 \u0437\u0430\u043d\u043e\u0432\u043e \u043e\u0431\u0440\u0430\u0437\u044b \u0438 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0438\u0442\u0435 \u043d\u043e\u0432\u044b\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b:<\/p>\n<pre><code class=\"python\">$ docker-compose down -v $ docker-compose up -d --build<\/code><\/pre>\n<p>\u041e\u0442\u043a\u0440\u043e\u0439\u0442\u0435 \u043b\u043e\u0433\u0438 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 docker-compose logs web. \u0412\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c:  <\/p>\n<pre><code class=\"python\">web_1  | CREATE TABLE song ( web_1  |    name VARCHAR NOT NULL, web_1  |    artist VARCHAR NOT NULL, web_1  |    id SERIAL, web_1  |    PRIMARY KEY (id) web_1  | )<\/code><\/pre>\n<p>\u041e\u0442\u043a\u0440\u043e\u0439\u0442\u0435 psql:  <\/p>\n<pre><code class=\"python\">$ docker-compose exec db psql --username=postgres --dbname=foo  psql (13.4 (Debian 13.4-1.pgdg100+1)) Type \"help\" for help.  foo=# \\dt          List of relations  Schema | Name | Type  |  Owner --------+------+-------+----------  public | song | table | postgres (1 row)  foo=# \\q<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0430, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u043e\u0432\u044b\u0445 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u043e\u0432 \u0432 <em>project\/app\/main.py<\/em>:<\/p>\n<pre><code class=\"python\">from fastapi import Depends, FastAPI from sqlalchemy import select from sqlmodel import Session  from app.db import get_session, init_db from app.models import Song, SongCreate  app = FastAPI()   @app.on_event(\"startup\") def on_startup():     init_db()   @app.get(\"\/ping\") async def pong():     return {\"ping\": \"pong!\"}   @app.get(\"\/songs\", response_model=list[Song]) def get_songs(session: Session = Depends(get_session)):     result = session.execute(select(Song))     songs = result.scalars().all()     return [Song(name=song.name, artist=song.artist, id=song.id) for song in songs]   @app.post(\"\/songs\") def add_song(song: SongCreate, session: Session = Depends(get_session)):     song = Song(name=song.name, artist=song.artist)     session.add(song)     session.commit()     session.refresh(song)     return song<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u043f\u0435\u0441\u043d\u044e:  <\/p>\n<pre><code class=\"python\">$ curl -d '{\"name\":\"Midnight Fit\", \"artist\":\"Mogwai\"}' -H \"Content-Type: application\/json\" -X POST http:\/\/localhost:8004\/songs  {   \"id\": 1,   \"name\": \"Midnight Fit\",   \"artist\": \"Mogwai\" }<\/code><\/pre>\n<p>\u0412 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u043f\u0435\u0440\u0435\u0439\u0434\u0438\u0442\u0435 \u043a <a href=\"http:\/\/localhost:8004\/songs\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:8004\/songs<\/a>. \u0412\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c:  <\/p>\n<pre><code>{   \"id\": 1,   \"name\": \"Midnight Fit\",   \"artist\": \"Mogwai\" }<\/code><\/pre>\n<h3>\u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c SQLModel<\/h3>\n<p>\u0414\u0432\u0438\u0433\u0430\u044f\u0441\u044c \u0434\u0430\u043b\u044c\u0448\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438 \u0432 SQLModel.<\/p>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b \u0438 \u0442\u043e\u043c\u0430:<\/p>\n<pre><code class=\"python\">$ docker-compose down -v<\/code><\/pre>\n<p>\u041e\u0431\u043d\u043e\u0432\u0438\u0442\u0435 URI \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 <em>docker-compose.yml<\/em>, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u0432  <\/p>\n<pre><code class=\"python\">+asyncpg: environment:   - DATABASE_URL=postgresql+asyncpg:\/\/postgres:postgres@db:5432\/foo<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u0435 Psycopg \u043d\u0430 <a href=\"https:\/\/magicstack.github.io\/asyncpg\/\" rel=\"noopener noreferrer nofollow\">asyncpg<\/a>:  <\/p>\n<pre><code>asyncpg==0.24.0 fastapi==0.68.1 sqlmodel==0.0.4 uvicorn==0.15.0<\/code><\/pre>\n<p>\u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 <em>project\/app\/db.py<\/em>: \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u044c \u0432 SQLAlchemy engine \u0438 session:  <\/p>\n<pre><code>import os  from sqlmodel import SQLModel  from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker   DATABASE_URL = os.environ.get(\"DATABASE_URL\")  engine = create_async_engine(DATABASE_URL, echo=True, future=True)   async def init_db():     async with engine.begin() as conn:         # await conn.run_sync(SQLModel.metadata.drop_all)         await conn.run_sync(SQLModel.metadata.create_all)   async def get_session() -> AsyncSession:     async_session = sessionmaker(         engine, class_=AsyncSession, expire_on_commit=False     )     async with async_session() as session:         yield session<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u044f:<\/p>\n<ol>\n<li>\n<p>\u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 SQLAlchemy, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/orm\/extensions\/asyncio.html#sqlalchemy.ext.asyncio.create_async_engine\" rel=\"noopener noreferrer nofollow\">create_async_engine<\/a> \u0438 <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/orm\/extensions\/asyncio.html#sqlalchemy.ext.asyncio.AsyncSession\" rel=\"noopener noreferrer nofollow\">AsyncSession<\/a>, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f SQLModel \u0434\u043b\u044f \u043d\u0438\u0445 \u043d\u0435 \u0431\u044b\u043b\u043e \u043e\u0431\u0451\u0440\u0442\u043e\u043a.<\/p>\n<\/li>\n<li>\n<p>\u041c\u044b \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u043b\u0438 <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/orm\/extensions\/asyncio.html#preventing-implicit-io-when-using-asyncsession\" rel=\"noopener noreferrer nofollow\">expire on commit<\/a> \u043f\u0435\u0440\u0435\u0434\u0430\u0432 <code>expire_on_commit=False<\/code> .<\/p>\n<\/li>\n<li>\n<p><code>metadata.create_all<\/code> \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/orm\/extensions\/asyncio.html#sqlalchemy.ext.asyncio.AsyncSession.run_sync\" rel=\"noopener noreferrer nofollow\">run_sync<\/a> \u0434\u043b\u044f \u0435\u0433\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.   <\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u0435 <code>on_startup<\/code> \u0432 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432 <em>project\/app\/main.py<\/em>:  <\/p>\n<pre><code>@app.on_event(\"startup\") async def on_startup():     await init_db()<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u0438 \u0432\u0441\u0435. \u041f\u0435\u0440\u0435\u0441\u043e\u0431\u0435\u0440\u0438\u0442\u0435 \u043e\u0431\u0440\u0430\u0437\u044b \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b:  <\/p>\n<pre><code>$ docker-compose up -d --build<\/code><\/pre>\n<p>\u0423\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0431\u044b\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u044b.<\/p>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u043e\u0432 \u0432 <em>project\/app\/main.py<\/em> \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435:<\/p>\n<pre><code>from fastapi import Depends, FastAPI from sqlalchemy.future import select from sqlalchemy.ext.asyncio import AsyncSession  from app.db import get_session, init_db from app.models import Song, SongCreate  app = FastAPI()   @app.on_event(\"startup\") async def on_startup():     await init_db()   @app.get(\"\/ping\") async def pong():     return {\"ping\": \"pong!\"}   @app.get(\"\/songs\", response_model=list[Song]) async def get_songs(session: AsyncSession = Depends(get_session)):     result = await session.execute(select(Song))     songs = result.scalars().all()     return [Song(name=song.name, artist=song.artist, id=song.id) for song in songs]   @app.post(\"\/songs\") async def add_song(song: SongCreate, session: AsyncSession = Depends(get_session)):     song = Song(name=song.name, artist=song.artist)     session.add(song)     await session.commit()     await session.refresh(song)     return song<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u043d\u043e\u0432\u0443\u044e \u043f\u0435\u0441\u043d\u044e \u0438 \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c<a href=\"http:\/\/localhost:8004\/songs\" rel=\"noopener noreferrer nofollow\">, \u0447\u0442\u043e http:\/\/localhost:8004\/songs<\/a> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043a\u0430\u043a \u0438 \u043e\u0436\u0438\u0434\u0430\u043b\u043e\u0441\u044c.  <\/p>\n<h2>Alembic  <\/h2>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c <a href=\"https:\/\/alembic.sqlalchemy.org\/\" rel=\"noopener noreferrer nofollow\">Alembic<\/a>, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u0445\u0435\u043c\u044b \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0435\u0433\u043e \u0432 \u0444\u0430\u0439\u043b \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0439:<\/p>\n<pre><code>alembic==1.7.1 asyncpg==0.24.0 fastapi==0.68.1 sqlmodel==0.0.4 uvicorn==0.15.0<\/code><\/pre>\n<p>\u0423\u0434\u0430\u043b\u0438\u0442\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0438\u0437 <em>project\/app\/main.py<\/em> \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0445\u043e\u0442\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0438\u0441\u044c \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435:  <\/p>\n<pre><code>@app.on_event(\"startup\") async def on_startup():     await init_db()<\/code><\/pre>\n<p>\u041e\u043f\u044f\u0442\u044c \u0436\u0435, \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b \u0438 \u0442\u043e\u043c\u0430:  <\/p>\n<pre><code>$ docker-compose down -v<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b:  <\/p>\n<pre><code>$ docker-compose up -d --build<\/code><\/pre>\n<p>\u0412\u0437\u0433\u043b\u044f\u043d\u0438\u0442\u0435 \u043d\u0430 <a href=\"https:\/\/alembic.sqlalchemy.org\/en\/latest\/cookbook.html#using-asyncio-with-alembic\" rel=\"noopener noreferrer nofollow\">\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Asyncio \u0441 Alembic<\/a>, \u043f\u043e\u043a\u0430 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0435 \u043e\u0431\u0440\u0430\u0437\u044b.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0439\u0442\u0435 Alembic \u0441 <a href=\"https:\/\/github.com\/sqlalchemy\/alembic\/tree\/rel_1_7_1\/alembic\/templates\/async\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e<\/a> \u0448\u0430\u0431\u043b\u043e\u043d\u0430:<\/p>\n<pre><code>$ docker-compose exec web alembic init -t async migrations<\/code><\/pre>\n<p>\u0412 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0439 \u043f\u0430\u043f\u043a\u0435 &#171;\u043f\u0440\u043e\u0435\u043a\u0442\/\u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438&#187; \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0439\u0442\u0435 SQLModel \u0432 <em>script.py.mako<\/em>, \u0444\u0430\u0439\u043b \u0448\u0430\u0431\u043b\u043e\u043d\u0430 Mako:  <\/p>\n<pre><code class=\"python\">\"\"\"${message}  Revision ID: ${up_revision} Revises: ${down_revision | comma,n} Create Date: ${create_date}  \"\"\" from alembic import op import sqlalchemy as sa import sqlmodel             # NEW ${imports if imports else \"\"}  # revision identifiers, used by Alembic. revision = ${repr(up_revision)} down_revision = ${repr(down_revision)} branch_labels = ${repr(branch_labels)} depends_on = ${repr(depends_on)}   def upgrade():     ${upgrades if upgrades else \"pass\"}   def downgrade():     ${downgrades if downgrades else \"pass\"}<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d \u043d\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438, \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c <code>import sqlmodel<\/code> .<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u044e\u044e \u0447\u0430\u0441\u0442\u044c <em>project\/migrations\/env.py<\/em> \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code>import asyncio from logging.config import fileConfig  from sqlalchemy import engine_from_config from sqlalchemy import pool from sqlalchemy.ext.asyncio import AsyncEngine from sqlmodel import SQLModel                       # NEW  from alembic import context  from app.models import Song                         # NEW  # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config  # Interpret the config file for Python logging. # This line sets up loggers basically. fileConfig(config.config_file_name)  # add your model's MetaData object here # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata target_metadata = SQLModel.metadata             # UPDATED  # other values from the config, defined by the needs of env.py, # can be acquired: # my_important_option = config.get_main_option(\"my_important_option\") # ... etc.  ...<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0438 SQLModel \u0438 \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0435\u0441\u043d\u0438. \u0417\u0430\u0442\u0435\u043c \u043c\u044b \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0432 <code>target_metadata<\/code> <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/core\/metadata.html#sqlalchemy.schema.MetaData\" rel=\"noopener noreferrer nofollow\">\u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438<\/a>, <code>SQLModel.metadata<\/code>. \u0414\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e\u0431 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0435 <code>target_metadata<\/code> \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u044c\u0442\u0435\u0441\u044c \u0441 <a href=\"https:\/\/alembic.sqlalchemy.org\/en\/latest\/autogenerate.html\" rel=\"noopener noreferrer nofollow\">\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439<\/a> \u0438\u0437 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 Alembic.<\/p>\n<p>\u041e\u0431\u043d\u043e\u0432\u0438\u0442\u0435 <code>sqlalchemy.url<\/code>\u0432 <em>\u043f\u0440\u043e\u0435\u043a\u0442\u0435\/alembic.ini<\/em>:<\/p>\n<pre><code class=\"python\">sqlalchemy.url = postgresql+asyncpg:\/\/postgres:postgres@db:5432\/foo<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435:  <\/p>\n<pre><code class=\"python\">$ docker-compose exec web alembic revision --autogenerate -m \"init\"<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u043e\u0448\u043b\u043e \u0445\u043e\u0440\u043e\u0448\u043e, \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 &#171;project\/migrations\/versions&#187;, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"python\">\"\"\"init  Revision ID: f9c634db477d Revises: Create Date: 2021-09-10 00:24:32.718895  \"\"\" from alembic import op import sqlalchemy as sa import sqlmodel   # revision identifiers, used by Alembic. revision = 'f9c634db477d' down_revision = None branch_labels = None depends_on = None   def upgrade():     # ### commands auto generated by Alembic - please adjust! ###     op.create_table('song',     sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),     sa.Column('artist', sqlmodel.sql.sqltypes.AutoString(), nullable=False),     sa.Column('id', sa.Integer(), nullable=True),     sa.PrimaryKeyConstraint('id')     )     op.create_index(op.f('ix_song_artist'), 'song', ['artist'], unique=False)     op.create_index(op.f('ix_song_id'), 'song', ['id'], unique=False)     op.create_index(op.f('ix_song_name'), 'song', ['name'], unique=False)     # ### end Alembic commands ###   def downgrade():     # ### commands auto generated by Alembic - please adjust! ###     op.drop_index(op.f('ix_song_name'), table_name='song')     op.drop_index(op.f('ix_song_id'), table_name='song')     op.drop_index(op.f('ix_song_artist'), table_name='song')     op.drop_table('song')     # ### end Alembic commands ###<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u044e:  <\/p>\n<pre><code>$ docker-compose exec web alembic upgrade head<\/code><\/pre>\n<p>\u0423\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0435\u0441\u043d\u044e.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0431\u044b\u0441\u0442\u0440\u043e \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b. \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u0435 \u043c\u043e\u0434\u0435\u043b\u044c\u00a0 <code>SongBase<\/code> \u0432 <em>project\/app\/models.py<\/em>:<\/p>\n<pre><code class=\"kotlin\">class SongBase(SQLModel):     name: str     artist: str     year: Optional[int] = None<\/code><\/pre>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0439\u0442\u0435 \u043e\u0431 \u0438\u043c\u043f\u043e\u0440\u0442\u0435:  <\/p>\n<pre><code>from typing import Optional<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438:  <\/p>\n<pre><code class=\"python\">$ docker-compose exec web alembic revision --autogenerate -m \"add year\"<\/code><\/pre>\n<p>\u041e\u0431\u043d\u043e\u0432\u0438\u0442\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>upgrade \u0438 downgrade<\/code> \u0438\u0437 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:  <\/p>\n<pre><code class=\"kotlin\">def upgrade():     # ### commands auto generated by Alembic - please adjust! ###     op.add_column('song', sa.Column('year', sa.Integer(), nullable=True))     op.create_index(op.f('ix_song_year'), 'song', ['year'], unique=False)     # ### end Alembic commands ###   def downgrade():     # ### commands auto generated by Alembic - please adjust! ###     op.drop_index(op.f('ix_song_year'), table_name='song')     op.drop_column('song', 'year')     # ### end Alembic commands ###<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u044e:  <\/p>\n<pre><code class=\"kotlin\">$ docker-compose exec web alembic upgrade head<\/code><\/pre>\n<p>\u041e\u0431\u043d\u043e\u0432\u0438\u0442\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u043e\u0432:  <\/p>\n<pre><code class=\"kotlin\">@app.get(\"\/songs\", response_model=list[Song]) async def get_songs(session: AsyncSession = Depends(get_session)):     result = await session.execute(select(Song))     songs = result.scalars().all()     return [Song(name=song.name, artist=song.artist, year=song.year, id=song.id) for song in songs]   @app.post(\"\/songs\") async def add_song(song: SongCreate, session: AsyncSession = Depends(get_session)):     song = Song(name=song.name, artist=song.artist, year=song.year)     session.add(song)     await session.commit()     await session.refresh(song)     return song<\/code><\/pre>\n<p>\u0422\u0435\u0441\u0442:  <\/p>\n<pre><code class=\"kotlin\">$ curl -d '{\"name\":\"Midnight Fit\", \"artist\":\"Mogwai\", \"year\":\"2021\"}' -H \"Content-Type: application\/json\" -X POST http:\/\/localhost:8004\/songs<\/code><\/pre>\n<h2>\u0412\u044b\u0432\u043e\u0434  <\/h2>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438, \u043a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c SQLAlchemy, SQLModel \u0438 Alembic \u0434\u043b\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 FastAPI.<\/p>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0439\u0442\u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 <a href=\"https:\/\/github.com\/testdrivenio\/fastapi-sqlmodel-alembic\" rel=\"noopener noreferrer nofollow\">fastapi-sqlmodel-alembic<\/a>. \u0412\u0430\u0448\u0435 \u0437\u0434\u043e\u0440\u043e\u0432\u044c\u0435!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/580866\/\"> https:\/\/habr.com\/ru\/articles\/580866\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u044d\u0442\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043e\u043f\u044b\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 FastAPI \u0438 Postgres \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Docker. \u0412\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c, \u0447\u0442\u043e\u0431\u044b \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 FastAPI, Postgres \u0438 Docker? \u041d\u0430\u0447\u043d\u0438\u0442\u0435 \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432:<\/p>\n<ol>\n<li>\n<p><a href=\"https:\/\/testdriven.io\/blog\/fastapi-crud\/\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e API \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e FastAPI \u0438 Pytest<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/testdriven.io\/courses\/tdd-fastapi\/\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e FastAPI \u0438 \u0434\u043e\u043a\u0435\u0440\u0430<\/a><\/p>\n<\/li>\n<\/ol>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h3>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430  <\/h3>\n<p>\u041d\u0430\u0447\u043d\u0438\u0442\u0435 \u0441 \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438\u0437 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f <a href=\"https:\/\/github.com\/testdrivenio\/fastapi-sqlmodel-alembic\" rel=\"noopener noreferrer nofollow\">fastapi-sqlmodel-alembic<\/a>:  <\/p>\n<pre><code class=\"python\">$ git clone -b base https:\/\/github.com\/testdrivenio\/fastapi-sqlmodel-alembic $ cd fastapi-sqlmodel-alembic<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u043c \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043e\u0431\u0440\u0430\u0437\u044b \u0438 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0438\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b Docker:  <\/p>\n<pre><code class=\"python\">$ docker-compose up -d --build<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0431\u043e\u0440\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0439\u0434\u0438\u0442\u0435 \u043a <a href=\"http:\/\/localhost:8004\/ping\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:8004\/ping<\/a>. \u0412\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c: <\/p>\n<pre><code class=\"python\">{   \"ping\": \"pong!\" }<\/code><\/pre>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435, \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043d\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0430.<\/p>\n<h2>SQLModel  <\/h2>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <a href=\"https:\/\/sqlmodel.tiangolo.com\/\" rel=\"noopener noreferrer nofollow\">SQLModel<\/a> \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 SQL-\u0431\u0430\u0437\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 Python. \u041f\u043e \u0441\u0443\u0442\u0438, \u044d\u0442\u043e \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0430 \u043f\u043e\u0432\u0435\u0440\u0445 <a href=\"https:\/\/pydantic-docs.helpmanual.io\/\" rel=\"noopener noreferrer nofollow\">pydantic<\/a> \u0438 <a href=\"https:\/\/www.sqlalchemy.org\/\" rel=\"noopener noreferrer nofollow\">SQLAlchemy<\/a>, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043d\u0430 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f\u0445 \u0442\u0438\u043f\u043e\u0432 Python.<\/p>\n<p>\u041d\u0430\u043c \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f <a href=\"https:\/\/www.psycopg.org\/\" rel=\"noopener noreferrer nofollow\">Psycopg<\/a>.<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0434\u0432\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 <em>project\/requirements.txt<\/em>:<\/p>\n<pre><code class=\"python\">fastapi==0.68.1 psycopg2-binary==2.9.1 sqlmodel==0.0.4 uvicorn==0.15.0<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0434\u0432\u0430 \u043d\u043e\u0432\u044b\u0445 \u0444\u0430\u0439\u043b\u0430 \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 &#171;\u043f\u0440\u043e\u0435\u043a\u0442\/\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435&#187;, <em>db.py<\/em> \u0438 <em>models.py<\/em>.<\/p>\n<p><em>project\/app\/models.py<\/em>:<\/p>\n<pre><code class=\"python\">from sqlmodel import SQLModel, Field   class SongBase(SQLModel):     name: str     artist: str   class Song(SongBase, table=True):     id: int = Field(default=None, primary_key=True)   class SongCreate(SongBase):     pass<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438 \u0442\u0440\u0438 \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<ol>\n<li>\n<p>SongBase \u044d\u0442\u043e \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u0438\u0435. \u0423 \u043d\u0435\u0451 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 name \u0438 artist, \u043e\u0431\u0430 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438. \u042d\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u044b\u0445 , \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u043d\u0435\u0439 \u043d\u0435\u0442 table=True, \u0430 \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u043e\u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 pydantic.<\/p>\n<\/li>\n<li>\n<p>Song \u0422\u0435\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 id \u0432 \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c. \u042d\u0442\u043e \u0442\u0430\u0431\u043b\u0438\u0447\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u0442\u0430\u043a \u0447\u0442\u043e \u044d\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c pydantic \u0438 SQLAlchemy. \u041e\u043d\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p>SongCreate \u044d\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c pydantic, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u044b\u0445 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 \u043f\u0435\u0441\u0435\u043d.<\/p>\n<\/li>\n<\/ol>\n<p><em>project\/app\/db.py<\/em>:<\/p>\n<pre><code class=\"python\">import os  from sqlmodel import create_engine, SQLModel, Session   DATABASE_URL = os.environ.get(\"DATABASE_URL\")  engine = create_engine(DATABASE_URL, echo=True)   def init_db():     SQLModel.metadata.create_all(engine)   def get_session():     with Session(engine) as session:         yield session<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b:<\/p>\n<ol>\n<li>\n<p>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b \u043d\u043e\u0432\u044b\u0439 <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/core\/engines.html\" rel=\"noopener noreferrer nofollow\">\u0434\u0432\u0438\u0436\u043e\u043a SQLAlchemy<\/a> \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c create_engine \u0438\u0437 SQLModel. \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u0440\u0441\u0438\u0435\u0439 create_engine \u0438\u0437 SQLModel \u0438 \u0432\u0435\u0440\u0441\u0438\u0435\u0439 SQLAlchemy \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u044e\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432\u0435\u0440\u0441\u0438\u044f SQLModel \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u0442\u0438\u043f\u043e\u0432 (\u0434\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0430) \u0438 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/core\/future.html\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0438\u043b\u044c \u0434\u0432\u0438\u0436\u043a\u043e\u0432 \u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 SQLAlchemy &#171;2.0&#187;<\/a>. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c echo=True \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0432\u0438\u0434\u0435\u0442\u044c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435. \u042d\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u0440\u0438\u044f\u0442\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0432 \u0440\u0435\u0436\u0438\u043c\u0435 \u043e\u0442\u043b\u0430\u0434\u043a\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u043b\u0438 \u0441\u0435\u0430\u043d\u0441 SQLAlchemy.<\/p>\n<\/li>\n<\/ol>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0432\u043d\u0443\u0442\u0440\u0438 <em>project\/app\/main.py<\/em>, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u0440\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0438 startup:<\/p>\n<pre><code class=\"python\">from fastapi import FastAPI  from app.db import init_db from app.models import Song  app = FastAPI()   @app.on_event(\"startup\") def on_startup():     init_db()   @app.get(\"\/ping\") async def pong():     return {\"ping\": \"pong!\"}<\/code><\/pre>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u0442\u0440\u043e\u0447\u043a\u0430 \u00a0<code>from app.models import Song<\/code>  \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u0430. \u0411\u0435\u0437 \u043d\u0435\u0451 \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043f\u0435\u0441\u0435\u043d \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0430.<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0443\u0434\u0430\u043b\u0438\u0442\u0435 \u0441\u0442\u0430\u0440\u044b\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b \u0438 \u0442\u043e\u043c\u0430, \u0441\u043e\u0431\u0435\u0440\u0438\u0442\u0435 \u0437\u0430\u043d\u043e\u0432\u043e \u043e\u0431\u0440\u0430\u0437\u044b \u0438 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0438\u0442\u0435 \u043d\u043e\u0432\u044b\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b:<\/p>\n<pre><code class=\"python\">$ docker-compose down -v $ docker-compose up -d --build<\/code><\/pre>\n<p>\u041e\u0442\u043a\u0440\u043e\u0439\u0442\u0435 \u043b\u043e\u0433\u0438 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 docker-compose logs web. \u0412\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c:  <\/p>\n<pre><code class=\"python\">web_1  | CREATE TABLE song ( web_1  |    name VARCHAR NOT NULL, web_1  |    artist VARCHAR NOT NULL, web_1  |    id SERIAL, web_1  |    PRIMARY KEY (id) web_1  | )<\/code><\/pre>\n<p>\u041e\u0442\u043a\u0440\u043e\u0439\u0442\u0435 psql:  <\/p>\n<pre><code class=\"python\">$ docker-compose exec db psql --username=postgres --dbname=foo  psql (13.4 (Debian 13.4-1.pgdg100+1)) Type \"help\" for help.  foo=# \\dt          List of relations  Schema | Name | Type  |  Owner --------+------+-------+----------  public | song | table | postgres (1 row)  foo=# \\q<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0430, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u043e\u0432\u044b\u0445 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u043e\u0432 \u0432 <em>project\/app\/main.py<\/em>:<\/p>\n<pre><code class=\"python\">from fastapi import Depends, FastAPI from sqlalchemy import select from sqlmodel import Session  from app.db import get_session, init_db from app.models import Song, SongCreate  app = FastAPI()   @app.on_event(\"startup\") def on_startup():     init_db()   @app.get(\"\/ping\") async def pong():     return {\"ping\": \"pong!\"}   @app.get(\"\/songs\", response_model=list[Song]) def get_songs(session: Session = Depends(get_session)):     result = session.execute(select(Song))     songs = result.scalars().all()     return [Song(name=song.name, artist=song.artist, id=song.id) for song in songs]   @app.post(\"\/songs\") def add_song(song: SongCreate, session: Session = Depends(get_session)):     song = Song(name=song.name, artist=song.artist)     session.add(song)     session.commit()     session.refresh(song)     return song<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u043f\u0435\u0441\u043d\u044e:  <\/p>\n<pre><code class=\"python\">$ curl -d '{\"name\":\"Midnight Fit\", \"artist\":\"Mogwai\"}' -H \"Content-Type: application\/json\" -X POST http:\/\/localhost:8004\/songs  {   \"id\": 1,   \"name\": \"Midnight Fit\",   \"artist\": \"Mogwai\" }<\/code><\/pre>\n<p>\u0412 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u043f\u0435\u0440\u0435\u0439\u0434\u0438\u0442\u0435 \u043a <a href=\"http:\/\/localhost:8004\/songs\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:8004\/songs<\/a>. \u0412\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c:  <\/p>\n<pre><code>{   \"id\": 1,   \"name\": \"Midnight Fit\",   \"artist\": \"Mogwai\" }<\/code><\/pre>\n<h3>\u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c SQLModel<\/h3>\n<p>\u0414\u0432\u0438\u0433\u0430\u044f\u0441\u044c \u0434\u0430\u043b\u044c\u0448\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u0438 \u0432 SQLModel.<\/p>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b \u0438 \u0442\u043e\u043c\u0430:<\/p>\n<pre><code class=\"python\">$ docker-compose down -v<\/code><\/pre>\n<p>\u041e\u0431\u043d\u043e\u0432\u0438\u0442\u0435 URI \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 <em>docker-compose.yml<\/em>, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u0432  <\/p>\n<pre><code class=\"python\">+asyncpg: environment:   - DATABASE_URL=postgresql+asyncpg:\/\/postgres:postgres@db:5432\/foo<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u0435 Psycopg \u043d\u0430 <a href=\"https:\/\/magicstack.github.io\/asyncpg\/\" rel=\"noopener noreferrer nofollow\">asyncpg<\/a>:  <\/p>\n<pre><code>asyncpg==0.24.0 fastapi==0.68.1 sqlmodel==0.0.4 uvicorn==0.15.0<\/code><\/pre>\n<p>\u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 <em>project\/app\/db.py<\/em>: \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0441\u0442\u044c \u0432 SQLAlchemy engine \u0438 session:  <\/p>\n<pre><code>import os  from sqlmodel import SQLModel  from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker   DATABASE_URL = os.environ.get(\"DATABASE_URL\")  engine = create_async_engine(DATABASE_URL, echo=True, future=True)   async def init_db():     async with engine.begin() as conn:         # await conn.run_sync(SQLModel.metadata.drop_all)         await conn.run_sync(SQLModel.metadata.create_all)   async def get_session() -> AsyncSession:     async_session = sessionmaker(         engine, class_=AsyncSession, expire_on_commit=False     )     async with async_session() as session:         yield session<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u044f:<\/p>\n<ol>\n<li>\n<p>\u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 SQLAlchemy, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/orm\/extensions\/asyncio.html#sqlalchemy.ext.asyncio.create_async_engine\" rel=\"noopener noreferrer nofollow\">create_async_engine<\/a> \u0438 <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/orm\/extensions\/asyncio.html#sqlalchemy.ext.asyncio.AsyncSession\" rel=\"noopener noreferrer nofollow\">AsyncSession<\/a>, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f SQLModel \u0434\u043b\u044f \u043d\u0438\u0445 \u043d\u0435 \u0431\u044b\u043b\u043e \u043e\u0431\u0451\u0440\u0442\u043e\u043a.<\/p>\n<\/li>\n<li>\n<p>\u041c\u044b \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u043b\u0438 <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/orm\/extensions\/asyncio.html#preventing-implicit-io-when-using-asyncsession\" rel=\"noopener noreferrer nofollow\">expire on commit<\/a> \u043f\u0435\u0440\u0435\u0434\u0430\u0432 <code>expire_on_commit=False<\/code> .<\/p>\n<\/li>\n<li>\n<p><code>metadata.create_all<\/code> \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 <a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/orm\/extensions\/asyncio.html#sqlalchemy.ext.asyncio.AsyncSession.run_sync\" rel=\"noopener noreferrer nofollow\">run_sync<\/a> \u0434\u043b\u044f \u0435\u0433\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.   <\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u0435 <code>on_startup<\/code> \u0432 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432 <em>project\/app\/main.py<\/em>:  <\/p>\n<pre><code>@app.on_event(\"startup\") async def on_startup():     await init_db()<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u0438 \u0432\u0441\u0435. \u041f\u0435\u0440\u0435\u0441\u043e\u0431\u0435\u0440\u0438\u0442\u0435 \u043e\u0431\u0440\u0430\u0437\u044b \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b:  <\/p>\n<pre><code>$ docker-compose up -d --build<\/code><\/pre>\n<p>\u0423\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0431\u044b\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u044b.<\/p>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u043e\u0432 \u0432 <em>project\/app\/main.py<\/em> \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435:<\/p>\n<pre><code>from fastapi import Depends, FastAPI from sqlalchemy.future import select from sqlalchemy.ext.asyncio import AsyncSession  from app.db import get_session, init_db from app.models import Song, SongCreate  app = FastAPI()   @app.on_event(\"startup\") async def on_startup():     await init_db()   @app.get(\"\/ping\") async def pong():     return {\"ping\": \"pong!\"}   @app.get(\"\/songs\", response_model=list[Song]) async def get_songs(session: AsyncSession = Depends(get_session)):     result = await session.execute(select(Song))     songs = result.scalars().all()     return [Song(name=song.name, artist=song.artist, id=song.id) for song in songs]   @app.post(\"\/songs\") async def add_song(song: SongCreate, session: AsyncSession = Depends(get_session)):     song = Song(name=song.name, artist=song.artist)     session.add(song)     await session.commit()     await session.refresh(song)     return song<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u043d\u043e\u0432\u0443\u044e \u043f\u0435\u0441\u043d\u044e \u0438 \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c<a href=\"http:\/\/localhost:8004\/songs\" rel=\"noopener noreferrer nofollow\">, \u0447\u0442\u043e http:\/\/localhost:8004\/songs<\/a> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043a\u0430\u043a \u0438 \u043e\u0436\u0438\u0434\u0430\u043b\u043e\u0441\u044c.  <\/p>\n<h2>Alembic  <\/h2>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c <a href=\"https:\/\/alembic.sqlalchemy.org\/\" rel=\"noopener noreferrer nofollow\">Alembic<\/a>, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u0445\u0435\u043c\u044b \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0435\u0433\u043e \u0432 \u0444\u0430\u0439\u043b \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0439:<\/p>\n<pre><code>alembic==1.7.1 asyncpg==0.24.0 fastapi==0.68.1 sqlmodel==0.0.4 uvicorn==0.15.0<\/code><\/pre>\n<p>\u0423\u0434\u0430\u043b\u0438\u0442\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0438\u0437 <em>project\/app\/main.py<\/em> \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0445\u043e\u0442\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0438\u0441\u044c \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435:  <\/p>\n<pre><code>@app.on_event(\"startup\") async def on_startup():     await init_db()<\/code><\/pre>\n<p>\u041e\u043f\u044f\u0442\u044c \u0436\u0435, \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b \u0438 \u0442\u043e\u043c\u0430:  <\/p>\n<pre><code>$ docker-compose down -v<\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b:  <\/p>\n<pre><code>$ docker-compose up -d --build<\/code><\/pre>\n<p>\u0412\u0437\u0433\u043b\u044f\u043d\u0438\u0442\u0435 \u043d\u0430 <a href=\"https:\/\/alembic.sqlalchemy.org\/en\/latest\/cookbook.html#using-asyncio-with-alembic\" rel=\"noopener noreferrer nofollow\">\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Asyncio \u0441 Alembic<\/a>, \u043f\u043e\u043a\u0430 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0435 \u043e\u0431\u0440\u0430\u0437\u044b.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0439\u0442\u0435 Alembic \u0441 <a href=\"https:\/\/github.com\/sqlalchemy\/alembic\/tree\/rel_1_7_1\/alembic\/templates\/async\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e<\/a> \u0448\u0430\u0431\u043b\u043e\u043d\u0430:<\/p>\n<pre><code>$ docker-compose exec web alembic init -t async migrations<\/code><\/pre>\n<p>\u0412 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0439 \u043f\u0430\u043f\u043a\u0435 &#171;\u043f\u0440\u043e\u0435\u043a\u0442\/\u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438&#187; \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0439\u0442\u0435 SQLModel \u0432 <em>script.py.mako<\/em>, \u0444\u0430\u0439\u043b \u0448\u0430\u0431\u043b\u043e\u043d\u0430 Mako:  <\/p>\n<pre><code class=\"python\">\"\"\"${message}  Revision ID: ${up_revision} Revises: ${down_revision | comma,n} Create Date: ${create_date}  \"\"\" from alembic import op import sqlalchemy as sa import sqlmodel             # NEW ${imports if imports else \"\"}  # revision identifiers, used by Alembic. revision = ${repr(up_revision)} down_revision = ${repr(down_revision)} branch_labels = ${repr(branch_labels)} depends_on = ${repr(depends_on)}   def upgrade():     ${upgrades if upgrades else \"pass\"}   def downgrade():     ${downgrades if downgrades else \"pass\"}<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d \u043d\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438, \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c <code>import sqlmodel<\/code> .<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u044e\u044e \u0447\u0430\u0441\u0442\u044c <em>project\/migrations\/env.py<\/em> \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code>import asyncio from logging.config import fileConfig  from sqlalchemy import engine_from_config from sqlalchemy import pool from sqlalchemy.ext.asyncio import AsyncEngine from sqlmodel import SQLModel                       # NEW  from alembic import context  from app.models import Song                         # NEW  # this is the Alembic Config object,<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-399612","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/399612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=399612"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/399612\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=399612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=399612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=399612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}