Managing database schema changes with Alembic and SQLAlchemy in BackAnt.
BackAnt includes Alembic (alembic~=1.12.0) in requirements.txt for database migrations.
How tables are created
In development, BackAnt takes a code-first approach: Base.metadata.create_all(engine) in init_db() creates any tables that don’t yet exist on every startup.This means for new routes, you don’t need a migration — just run docker-compose up --build and the table is created automatically.
When you need Alembic
Alembic is needed when you make changes to existing tables — adding columns, renaming columns, changing types, adding indexes. create_all() does not modify tables that already exist.
from startup.Alchemy import Basetarget_metadata = Base.metadata
Also ensure all models are imported before Base.metadata is referenced:
import models.Users_modelimport models.Orders_model# ... all your models
Generating a migration
After changing a model (e.g. adding a phone column to Users), generate a migration:
alembic revision --autogenerate -m "add phone to users"
Alembic compares your SQLAlchemy models against the current database schema and generates an upgrade() / downgrade() script in alembic/versions/.
Applying migrations
alembic upgrade head
Rolling back
alembic downgrade -1 # one step backalembic downgrade base # all the way back
CLEAR_DB in development
For rapid iteration during development, CLEAR_DB=True in your .env drops all tables and recreates them from scratch on every restart. This is faster than running Alembic but destroys all data:
CLEAR_DB=True
Never use CLEAR_DB=True in production. Always use Alembic migrations for production schema changes.
Migration checklist
Modify the SQLAlchemy model in api/models/
Run alembic revision --autogenerate -m "description"
Review the generated migration in alembic/versions/
Run alembic upgrade head
Rebuild your Docker image for deployment
{"docsConfig":{"theme":"mint","$schema":"https://mintlify.com/docs.json","name":"BackAnt","description":"Official Documentation of BackAnt- a tool that enables your favorite coding agent to one-shot backends.","colors":{"primary":"#FACC15","light":"#FACC15","dark":"#FACC15"},"logo":"https://mintcdn.com/backantinc-477ea439/U-PHrqyb2Z_wWQC8/logo/dark.svg?fit=max\u0026auto=format\u0026n=U-PHrqyb2Z_wWQC8\u0026q=85\u0026s=69846b0d39be7e7ab7ab5c7bd1196ad5","favicon":"https://mintcdn.com/backantinc-477ea439/ZdLqFDWwS0v1aSs7/favicon.svg?fit=max\u0026auto=format\u0026n=ZdLqFDWwS0v1aSs7\u0026q=85\u0026s=f65be7a0cbc23a41250e1b5ee148ecb1","appearance":{"default":"dark"},"background":{"decoration":"gradient"},"navbar":{"links":[{"href":"mailto:support@backant.io","label":"Support"}],"primary":{"type":"button","label":"Dashboard","href":"https://backant.io/dashboard"}},"footer":{"socials":{"github":"https://github.com/backant"}},"contextual":{"options":["copy","view","chatgpt","claude","perplexity","mcp","cursor","vscode"],"display":"header"},"styling":{"latex":true},"navigation":{"global":{"anchors":[{"anchor":"GitHub","icon":"github","href":"https://github.com/backant"},{"anchor":"Support","icon":"envelope","href":"mailto:support@backant.io"}]},"pages":[]}},"deployment":{"deploymentMetadataValue":{"subdomain":"backantinc-477ea439","hasAudioNarration":false,"actualSubdomain":"backantinc-477ea439","customDomains":["docs.backant.io"],"gitSource":{"type":"github","owner":"backant-io","repo":"docs","deployBranch":"main","contentDirectory":"","isPrivate":false,"mountPath":"","label":""},"entitlements":{},"buildId":"6aa8087851213d7af5acbf1c","clientVersion":"0.0.4143","searchFilterCounts":[{"language":"default","tag":"CLI","count":24},{"language":"default","tag":"Kairos","count":11},{"language":"default","tag":"Getting Started","count":8},{"language":"default","tag":"The Basics","count":8},{"language":"default","tag":"CLI Reference","count":6},{"language":"default","tag":"Database","count":4},{"language":"default","tag":"Get started","count":3},{"language":"default","tag":"Reference","count":3},{"language":"default","tag":"Commands","count":3},{"language":"default","tag":"Security","count":3}]},"assistantConfig":{"enableAskAiButton":false,"enableStarterQuestions":false,"enableAskAiSearchBar":true},"basePath":"","hasChatPermissions":false,"hasWebMcpPermissions":false,"toggles":[{"name":"kill-isr","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-rbac-roles","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-mintlify-slack","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-integrations-settings","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"rbac-expansion","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-pr-grouping","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-prefix","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"analytics-v7","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"user-flows-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"no-publish-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-onboarding","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving-live","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"stripTemperatureMiddleware","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"support-platform-pylon-zendesk","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"instant-rollback","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications-server","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-fable","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fixed-ai-action-pricing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automation-resolve-merge-conflict","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"scoped-publishing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"mintql","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"page-level-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-version-history-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"billing-page-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"full-page-search","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workspace-projects","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"assistant-v5","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fork-pr-previews","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"revalidation-jwt","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-sso-scim","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"hobby-abuse-scan","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"dashboard-inbox","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-signup","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-dual-write","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-enabled","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"approve-review-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-metadata","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"sso-setup-deadline-banner","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-serve","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"live-agents","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"llms-txt-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false}],"googleFontsInlined":false},"customCss":[],"page":{"pagePath":"database/migrations","slug":"/database/migrations","pageMetadata":{"title":"Migrations","description":"Managing database schema changes with Alembic and SQLAlchemy in BackAnt.","href":"/database/migrations"},"description":{"compiledSource":"\"use strict\";\nconst {jsx: _jsx} = arguments[0];\nconst {useMDXComponents: _provideComponents} = arguments[0];\nfunction _createMdxContent(props) {\n const _components = {\n p: \"p\",\n ..._provideComponents(),\n ...props.components\n };\n return _jsx(_components.p, {\n children: \"Managing database schema changes with Alembic and SQLAlchemy in BackAnt.\"\n });\n}\nfunction MDXContent(props = {}) {\n const {wrapper: MDXLayout} = {\n ..._provideComponents(),\n ...props.components\n };\n return MDXLayout ? _jsx(MDXLayout, {\n ...props,\n children: _jsx(_createMdxContent, {\n ...props\n })\n }) : _createMdxContent(props);\n}\nreturn {\n default: MDXContent\n};\n","frontmatter":{},"scope":{}},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{Fragment:_Fragment,jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={a:`a`,code:`code`,li:`li`,ol:`ol`,p:`p`,pre:`pre`,span:`span`,strong:`strong`,..._provideComponents(),...props.components},{CodeBlock,Heading,Warning}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Warning)_missingMdxReference(`Warning`,true);return _jsxs(_Fragment,{children:[_jsxs(_components.p,{children:[`BackAnt includes `,_jsx(_components.a,{href:`https://alembic.sqlalchemy.org/`,children:`Alembic`}),` (`,_jsx(_components.code,{children:`alembic~=1.12.0`}),`) in `,_jsx(_components.code,{children:`requirements.txt`}),` for database migrations.`]}),`\n`,_jsx(Heading,{level:`2`,id:`how-tables-are-created`,children:`How tables are created`}),`\n`,_jsxs(_components.p,{children:[`In development, BackAnt takes a code-first approach: `,_jsx(_components.code,{children:`Base.metadata.create_all(engine)`}),` in `,_jsx(_components.code,{children:`init_db()`}),` creates any tables that don’t yet exist on every startup.`]}),`\n`,_jsxs(_components.p,{children:[`This means for `,_jsx(_components.strong,{children:`new routes`}),`, you don’t need a migration — just run `,_jsx(_components.code,{children:`docker-compose up --build`}),` and the table is created automatically.`]}),`\n`,_jsx(Heading,{level:`2`,id:`when-you-need-alembic`,children:`When you need Alembic`}),`\n`,_jsxs(_components.p,{children:[`Alembic is needed when you make `,_jsx(_components.strong,{children:`changes to existing tables`}),` — adding columns, renaming columns, changing types, adding indexes. `,_jsx(_components.code,{children:`create_all()`}),` does not modify tables that already exist.`]}),`\n`,_jsx(Heading,{level:`2`,id:`setting-up-alembic`,children:`Setting up Alembic`}),`\n`,_jsx(_components.p,{children:`Initialize Alembic from the project root:`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`2`,language:`shellscript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`shellscript`,children:_jsxs(_components.code,{language:`shellscript`,numberOfLines:`2`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`cd`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` my-api`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#DCDCAA`},children:`alembic`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` init`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` alembic`})]}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[`Edit `,_jsx(_components.code,{children:`alembic.ini`}),` to set your database URL:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`1`,language:`ini`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`ini`,children:_jsxs(_components.code,{language:`ini`,numberOfLines:`1`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`sqlalchemy.url`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` = postgresql://postgres:test@localhost/postgres`})]}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[`Edit `,_jsx(_components.code,{children:`alembic/env.py`}),` to point at BackAnt’s `,_jsx(_components.code,{children:`Base`}),`:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`2`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`2`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`from`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` startup.Alchemy `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` Base`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`target_metadata `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` Base.metadata`})]}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[`Also ensure all models are imported before `,_jsx(_components.code,{children:`Base.metadata`}),` is referenced:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`3`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`3`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` models.Users_model`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` models.Orders_model`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# ... all your models`})}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`generating-a-migration`,children:`Generating a migration`}),`\n`,_jsxs(_components.p,{children:[`After changing a model (e.g. adding a `,_jsx(_components.code,{children:`phone`}),` column to `,_jsx(_components.code,{children:`Users`}),`), generate a migration:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`1`,language:`shellscript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`shellscript`,children:_jsxs(_components.code,{language:`shellscript`,numberOfLines:`1`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#DCDCAA`},children:`alembic`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` revision`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#569CD6`},children:` --autogenerate`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#569CD6`},children:` -m`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"add phone to users\"`})]}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[`Alembic compares your SQLAlchemy models against the current database schema and generates an `,_jsx(_components.code,{children:`upgrade()`}),` / `,_jsx(_components.code,{children:`downgrade()`}),` script in `,_jsx(_components.code,{children:`alembic/versions/`}),`.`]}),`\n`,_jsx(Heading,{level:`2`,id:`applying-migrations`,children:`Applying migrations`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`1`,language:`shellscript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`shellscript`,children:_jsxs(_components.code,{language:`shellscript`,numberOfLines:`1`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#DCDCAA`},children:`alembic`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` upgrade`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` head`})]}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`rolling-back`,children:`Rolling back`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`2`,language:`shellscript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`shellscript`,children:_jsxs(_components.code,{language:`shellscript`,numberOfLines:`2`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#DCDCAA`},children:`alembic`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` downgrade`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#569CD6`},children:` -1`}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` # one step back`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#DCDCAA`},children:`alembic`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` downgrade`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` base`}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` # all the way back`})]}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`clear_db-in-development`,children:`CLEAR_DB in development`}),`\n`,_jsxs(_components.p,{children:[`For rapid iteration during development, `,_jsx(_components.code,{children:`CLEAR_DB=True`}),` in your `,_jsx(_components.code,{children:`.env`}),` drops all tables and recreates them from scratch on every restart. This is faster than running Alembic but destroys all data:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`2`,language:`text`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`text`,children:_jsxs(_components.code,{language:`text`,numberOfLines:`2`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{children:`CLEAR_DB=True`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{})})]})})}),`\n`,_jsx(Warning,{children:_jsxs(_components.p,{children:[`Never use `,_jsx(_components.code,{children:`CLEAR_DB=True`}),` in production. Always use Alembic migrations for production schema changes.`]})}),`\n`,_jsx(Heading,{level:`2`,id:`migration-checklist`,children:`Migration checklist`}),`\n`,_jsxs(_components.ol,{children:[`\n`,_jsxs(_components.li,{children:[`Modify the SQLAlchemy model in `,_jsx(_components.code,{children:`api/models/`})]}),`\n`,_jsxs(_components.li,{children:[`Run `,_jsx(_components.code,{children:`alembic revision --autogenerate -m \"description\"`})]}),`\n`,_jsxs(_components.li,{children:[`Review the generated migration in `,_jsx(_components.code,{children:`alembic/versions/`})]}),`\n`,_jsxs(_components.li,{children:[`Run `,_jsx(_components.code,{children:`alembic upgrade head`})]}),`\n`,_jsx(_components.li,{children:`Rebuild your Docker image for deployment`}),`\n`]})]})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"config":{},"pageMetadata":{"title":"Migrations","description":"Managing database schema changes with Alembic and SQLAlchemy in BackAnt.","href":"/database/migrations"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"How tables are created","slug":"how-tables-are-created","depth":2,"children":[]},{"title":"When you need Alembic","slug":"when-you-need-alembic","depth":2,"children":[]},{"title":"Setting up Alembic","slug":"setting-up-alembic","depth":2,"children":[]},{"title":"Generating a migration","slug":"generating-a-migration","depth":2,"children":[]},{"title":"Applying migrations","slug":"applying-migrations","depth":2,"children":[]},{"title":"Rolling back","slug":"rolling-back","depth":2,"children":[]},{"title":"CLEAR_DB in development","slug":"clear_db-in-development","depth":2,"children":[]},{"title":"Migration checklist","slug":"migration-checklist","depth":2,"children":[]}],"codeExamples":{}},"jsLinks":[],"subdomain":"backantinc-477ea439","siteName":"BackAnt","title":"Migrations - BackAnt","navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2026-02-25T21:10:58.950Z","originalFileLocation":"database/migrations.mdx"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"dark\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"description\" content=\"Managing database schema changes with Alembic and SQLAlchemy in BackAnt.\">\u003cmeta data-mint-head name=\"application-name\" content=\"BackAnt\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/backantinc-477ea439/HIBJGyq1rzgD0SkL/_generated/favicon/android-chrome-192x192.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/png\" sizes=\"180x180\" href=\"/mintlify-assets/_mintlify/favicons/backantinc-477ea439/HIBJGyq1rzgD0SkL/_generated/favicon/apple-touch-icon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/backantinc-477ea439/HIBJGyq1rzgD0SkL/_generated/favicon/favicon-16x16.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/backantinc-477ea439/HIBJGyq1rzgD0SkL/_generated/favicon/favicon-32x32.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/backantinc-477ea439/HIBJGyq1rzgD0SkL/_generated/favicon/favicon.ico\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/backantinc-477ea439/HIBJGyq1rzgD0SkL/_generated/favicon-dark/android-chrome-192x192.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/backantinc-477ea439/HIBJGyq1rzgD0SkL/_generated/favicon-dark/favicon-16x16.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/backantinc-477ea439/HIBJGyq1rzgD0SkL/_generated/favicon-dark/favicon-32x32.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/backantinc-477ea439/HIBJGyq1rzgD0SkL/_generated/favicon-dark/favicon.ico\" media=\"(prefers-color-scheme: dark)\">\u003cmeta data-mint-head property=\"og:title\" content=\"Migrations - BackAnt\">\u003cmeta data-mint-head property=\"og:description\" content=\"Managing database schema changes with Alembic and SQLAlchemy in BackAnt.\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"BackAnt\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://docs.backant.io/database/migrations\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://backantinc-477ea439.mintlify.site/_mintlify/api/og/v2?division=Database\u0026amp;title=Migrations\u0026amp;description=Managing+database+schema+changes+with+Alembic+and+SQLAlchemy+in+BackAnt.\u0026amp;theme=992cb8556f776bf78e8c0317\u0026amp;signature=fee58e39f7c25d0e001fce243f29f525c0885cbb576608010ed65eff41dfea7e\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"Migrations - BackAnt\">\u003cmeta data-mint-head name=\"twitter:description\" content=\"Managing database schema changes with Alembic and SQLAlchemy in BackAnt.\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://backantinc-477ea439.mintlify.site/_mintlify/api/og/v2?division=Database\u0026amp;title=Migrations\u0026amp;description=Managing+database+schema+changes+with+Alembic+and+SQLAlchemy+in+BackAnt.\u0026amp;theme=992cb8556f776bf78e8c0317\u0026amp;signature=fee58e39f7c25d0e001fce243f29f525c0885cbb576608010ed65eff41dfea7e\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"/mintlify-assets/_mintlify/favicons/backantinc-477ea439/HIBJGyq1rzgD0SkL/_generated/favicon/browserconfig.xml\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"BackAnt\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#FACC15\">\u003clink data-mint-head rel=\"canonical\" href=\"https://docs.backant.io/database/migrations\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/database/migrations.md\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://docs.backant.io/#organization\",\"name\":\"BackAnt\",\"url\":\"https://docs.backant.io\",\"logo\":{\"@type\":\"ImageObject\",\"url\":\"https://mintcdn.com/backantinc-477ea439/U-PHrqyb2Z_wWQC8/logo/dark.svg?fit=max\u0026auto=format\u0026n=U-PHrqyb2Z_wWQC8\u0026q=85\u0026s=69846b0d39be7e7ab7ab5c7bd1196ad5\"}},{\"@type\":\"WebSite\",\"@id\":\"https://docs.backant.io#website\",\"name\":\"BackAnt\",\"url\":\"https://docs.backant.io\",\"publisher\":{\"@id\":\"https://docs.backant.io/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://docs.backant.io/database/migrations#webpage\",\"url\":\"https://docs.backant.io/database/migrations\",\"name\":\"Migrations\",\"description\":\"Managing database schema changes with Alembic and SQLAlchemy in BackAnt.\",\"dateModified\":\"2026-02-25T21:10:58.950Z\",\"isPartOf\":{\"@id\":\"https://docs.backant.io#website\"},\"breadcrumb\":{\"@id\":\"https://docs.backant.io/database/migrations#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://docs.backant.io/database/migrations#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Database\",\"item\":\"https://docs.backant.io/database/getting-started\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Migrations\",\"item\":\"https://docs.backant.io/database/migrations\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://docs.backant.io/database/migrations#article\",\"headline\":\"Migrations\",\"name\":\"Migrations\",\"description\":\"Managing database schema changes with Alembic and SQLAlchemy in BackAnt.\",\"url\":\"https://docs.backant.io/database/migrations\",\"mainEntityOfPage\":{\"@id\":\"https://docs.backant.io/database/migrations#webpage\"},\"dateModified\":\"2026-02-25T21:10:58.950Z\",\"publisher\":{\"@id\":\"https://docs.backant.io/#organization\"},\"isPartOf\":{\"@id\":\"https://docs.backant.io#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"BackAnt\",\"publisher\":{\"@type\":\"Organization\",\"name\":\"BackAnt\"},\"provider\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","agentPointerHref":"/database/migrations.md","siteName":"BackAnt","nav":{"global":{"anchors":[{"anchor":"GitHub","icon":"github","href":"https://github.com/backant"},{"anchor":"Support","icon":"envelope","href":"mailto:support@backant.io"}]},"tabs":[{"tab":"Getting Started","pages":[{"title":"Getting Started","href":""}]},{"tab":"MCP","pages":[{"title":"MCP","href":"/getting-started/agentic-development"}]},{"tab":"Kairos","pages":[{"title":"Kairos","href":"/kairos/overview"}]},{"tab":"CLI","groups":[{"group":"CLI Reference","pages":[{"title":"CLI Installation","href":"/cli/installation"},{"title":"CLI Overview","href":"/cli/overview"},{"title":"generate api","href":"/cli/generate-api"},{"title":"generate route","href":"/cli/generate-route"},{"title":"generate subroute","href":"/cli/generate-subroute"},{"title":"JSON Spec Format","href":"/cli/json-spec"}]},{"group":"Architecture","pages":[{"title":"Architecture Concepts","href":"/architecture/overview"},{"title":"Directory Structure","href":"/getting-started/directory-structure"}]},{"group":"The Basics","pages":[{"title":"Routing","href":"/the-basics/routing"},{"title":"Middleware","href":"/the-basics/middleware"},{"title":"Controllers","href":"/the-basics/controllers"},{"title":"Requests","href":"/the-basics/requests"},{"title":"Responses","href":"/the-basics/responses"},{"title":"Schemas","href":"/the-basics/schemas"},{"title":"Error Handling","href":"/the-basics/error-handling"},{"title":"Logging","href":"/the-basics/logging"}]},{"group":"Security","pages":[{"title":"Authentication","href":"/security/authentication"},{"title":"Authorization","href":"/security/authorization"},{"title":"Encryption \u0026 Hashing","href":"/security/encryption"}]},{"group":"Database","pages":[{"title":"Database Getting Started","href":"/database/getting-started"},{"title":"Query Builder","description":"Writing database queries with SQLAlchemy and DBSession in BackAnt repositories.","href":"/database/query-builder"},{"title":"Migrations","href":"/database/migrations"},{"title":"Seeding","description":"Replacing mock data with real database logic in BackAnt.","href":"/database/seeding"}]},{"group":"External APIs","pages":[{"title":"AWS","href":"/apis/aws"}]}]}]},"basePath":"","title":"Migrations - BackAnt","buildId":"dpl_Fyv1NbzwP68g2bEH2Roy98uY1zmE","deploymentId":"dpl_Fyv1NbzwP68g2bEH2Roy98uY1zmE","dynamics":[],"chunks":[]}