Rename Unique Constraint
See original GitHub issueMigrated issue, originally created by nickretallack (@nickretallack)
I created a model with a UniqueConstraint like so:
class Person(db.Model):
__tablename__ = 'person'
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String, nullable=False)
last_name = db.Column(db.String, nullable=False)
__table_args__ = (
db.UniqueConstraint(first_name, last_name),
)
Then I changed the constraint’s name.
class Person(db.Model):
__tablename__ = 'person'
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String, nullable=False)
last_name = db.Column(db.String, nullable=False)
__table_args__ = (
db.UniqueConstraint(first_name, last_name, name="person_name"),
)
The migration it generated looked like this:
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.create_unique_constraint('person_name', 'person', ['first_name', 'last_name'])
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('person_name', 'person')
### end Alembic commands ###
If I run this, I end up with two identical constraints in the database. Unfortunately, there doesn’t seem to be a rename_constraint function anywhere. I suppose it can be handled by dropping the existing constraint and then recreating it? Alembic should detect and drop the existing constraint, right? But since it isn’t named in the source code, I suppose Alembic can’t find it? Either way, it could look at the existing constraints and realize that there isn’t a corresponding one in the source code, and detect that the constraint must be renamed or dropped because of that. Unless you want to support people creating constraints on the database that don’t exist in the code.
Issue Analytics
- State:
- Created 9 years ago
- Comments:9
Adrian (@thiefmaster) wrote:
At least PostgreSQL supports
ALTER TABLE ... RENAME CONSTRAINT
.Changes by Michael Bayer (@zzzeek):