sqlalchemy / sqlalchemy/alembic
batch_op removes sqlite_autoincrement
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.4k
- Forks
- 375
- PR merge metrics
- No merged PRs in 30d
Description
Migrated issue, originally created by berend (@berend)
Given a sqlite3 table with __tableargs__ = {'sqlite_autoincrement': True} and a batch_op migration touching that table:
Model. initial migration
def upgrade():
op.create_table('person',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('firstname', sa.String(), nullable=False),
sa.Column('lastname', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sqlite_autoincrement=True
)
simple migration, making firstname nullable:
def upgrade():
with op.batch_alter_table('person') as batch_op:
batch_op.alter_column(
'firstname',
existing_type=sa.TEXT(),
server_default=None,
nullable=True)
With echo-sql on I can see, that the first version of the person table has autoincrement on the id column. During the second migration, alembic creates a temp table, because sqlite does not support alter column operations. That temp table does not have the autoincrement on the id column. After renaming it to person, the autoincrement is gone
Manually adding table kwargs to the batch_op is a workaround.
def upgrade():
with op.batch_alter_table('person', table_kwargs={'sqlite_autoincrement': True}) as batch_op:
batch_op.alter_column(
'firstname',
existing_type=sa.TEXT(),
server_default=None,
nullable=True)
Do I have to always use table_kwargs to let alembic know? Or is there a bug when reading the table to create the temp table?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the migration using a SQLite table with sqlite_autoincrement and the shown batch_alter_table call, with echo SQL enabled. Start at batch_op table recreation and compare the temporary table definition with the original; done means autoincrement is preserved without requiring table_kwargs, or the required workaround is clearly documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, sqlalchemy, sqlite
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100