Problem to Solve
Using MSSQL I'd like to have a column that is unique and accepts nulls.
Issues
Add two rows of data into a column that allows nulls with the unique constraint like in the implementation below gives the following error:
Violation of UNIQUE KEY constraint 'UQ_...'. Cannot insert duplicate key in object 'TABLE'. The duplicate key value is (<NULL>). (2627) (SQLExecDirectW)"
Downgrading the column causes constraint issues tied to the
reference
column. The constraint is automatically uniquely named so its a pain to programmatically remove.
Current Implementation
The alembic operation is:
from alembic import op
import sqlalchemy as sa
#...
def upgrade():
op.add_column(
'TABLE', sa.Column('reference', sa.Integer(), nullable=True, unique=True),
)
def downgrade():
op.drop_column('TABLE', 'reference')