๐ ๋ฌธ์ (Problem) – Alembic ๋ง์ด๊ทธ๋ ์ด์ ์ค๋ฅ ๋ถ์
๋ค์์ Python FastAPI + SQLAlchemy + Alembic์ ์ฌ์ฉํ์ฌ SQLite ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ค์ ํ๋ ์ค ๋ฐ์ํ ์ค์ ์ฝ์ ์ถ๋ ฅ์ด๋ค.
[์ง๋ฌธ]
๊ฐ๋ฐ์๋ SQLite ๋ฐ์ดํฐ๋ฒ ์ด์ค(myapi.db)์ Alembic์ ์ด์ฉํด ๋ชจ๋ธ์ ๋ง์ด๊ทธ๋ ์ด์
ํ๋ ค๊ณ ํ๋ค.
๊ทธ๋ฌ๋ ๋ค์๊ณผ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ๋ณต์ ์ผ๋ก ๋ฐ์ํ์๋ค.
$ alembic revision --autogenerate -m "init" INFO [alembic.runtime.migration] Context impl SQLiteImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL. ERROR [alembic.util.messaging] Target database is not up to date. FAILED: Target database is not up to date.
๋ํ ๊ฐ๋ฐ์๊ฐ DB ํ์ผ์ ์ญ์ ํ ํ์๋ ๋์ผํ ์ค๋ฅ๊ฐ ๋ํ๋ฌ๋ค.
๊ฐ๋ฐ ํ๊ฒฝ์ ์ฃผ์ ํ์ผ ๊ตฌ์กฐ๋ ๋ค์๊ณผ ๊ฐ๋ค.
~/Documents/FastAPI/
โโ alembic.ini
โโ migrations/
โ โโ versions/
โ โ โโ (์ด์ ์ ์์ฑ๋ migration ํ์ผ๋ค ์กด์ฌ)
โโ models.py
โโ database.py
โโ myapi.db
Alembic ์ค์ (env.py)์์๋ ๋ค์๊ณผ ๊ฐ์ด ๋ชจ๋ธ์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๊ณ ์๋ค.
from models import Base target_metadata = Base.metadata
๋ฐ์ดํฐ๋ฒ ์ด์ค URL์ ๋ค์๊ณผ ๊ฐ๋ค.
SQLALCHEMY_DATABASE_URL = "sqlite:///./myapi.db"
๊ฐ๋ฐ์๋ DB ํ์ผ(myapi.db)์ ์ญ์ ํ์ผ๋, Alembic์ ๊ณ์ ๋์ผํ ์ค๋ฅ๋ฅผ ์ถ๋ ฅํ์๋ค.
๋ง์ง๋ง์ผ๋ก ๋ค์ ์ฝ๋๋ก Alembic์ด ์ค์ ๋ก ์ฝ๋ DB ํ์ผ ๊ฒฝ๋ก๋ฅผ ํ์ธํ๋ค.
abs_path = os.path.abspath("./myapi.db")
print(abs_path, os.path.exists(abs_path))
๊ทธ ๊ฒฐ๊ณผ ๋ค์ ์ถ๋ ฅ์ด ๋์๋ค.
[๋ฌธ์ ]
1) ์ ์ํฉ์์ Alembic์ด "Target database is not up to date"๋ฅผ ์ถ๋ ฅํ ์ด์ ๋ฅผ ๊ธฐ์ ํ์์ค.
2) ๊ฐ๋ฐ์๊ฐ ๋จ์ํ DB ํ์ผ์ ์ญ์ ํ์์๋ ๋ถ๊ตฌํ๊ณ Alembic์ด ๊ณ์ ์ค๋ฅ๋ฅผ ์ถ๋ ฅํ ๊ทผ๋ณธ ์์ธ์ ์ค๋ช ํ์์ค.
3) ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๊ฐ๋ฐ์๊ฐ ์ํํด์ผ ํ๋ ์ ํํ ์กฐ์น(๋ช ๋ น์ด ํฌํจ)๋ฅผ ์์ฑํ์์ค.
'FastAPI' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๋ชจ๋ ์น๊ณผ ํ์ด์ฌ (1) | 2025.12.21 |
|---|---|
| ๋ชจ๋ธ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ด๋ฆฌํ๊ธฐ (0) | 2025.11.22 |
| async ๋น๋๊ธฐ, ์ฝ๋ฃจํด ํจ์ (0) | 2025.08.24 |
| ์ฑ๋ด ๊ธฐ๋ฐ ์ฑ ํ ์คํธ (0) | 2025.08.22 |