sqlalchemy / sqlalchemy/alembic

bulk insert w/ SQL Server and no-pk table in offline mode should not render SET IDENTITY INSERT

Open
#495 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

execution model feature low priority
Dominant language
Python
Stars
4.4k
Forks
375
PR merge metrics
No merged PRs in 30d

Description

Migrated issue, originally created by Wil Tan

In offline mode, Alembic emits IDENTITY INSERT mode control statements. However, in online mode, it does not do so.

There are a few shortcomings that trips up my use cases:

  • in offline mode, sometimes I bulk_insert into a table with no auto-increment key. The SET IDENTITY INSERT <table> ON statement will result in an error in MSSQL.

  • in online mode, sometimes I do want to insert fixed primary key values for auto-increment fields, but because Alembic does not turn on IDENTITY INSERT mode, MSSQL throws an error.

Would it make sense to change the current behaviour in alembic/ddl/mssql.py such that it emits SET IDENTITY INSERT statements if the rows data contains the table._autoincrement_column, regardless of offline/online mode?

I'm happy to work on a pull request if you agree with the approach.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in alembic/ddl/mssql.py and trace how bulk_insert handles IDENTITY INSERT in offline and online modes. Compare behavior for tables without an auto-increment key and rows containing the table's autoincrement column. Done means offline mode does not emit the statement for no-PK tables, while online mode enables it when fixed identity values are inserted.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.