question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rename Unique Constraint

See original GitHub issue

Migrated 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:closed
  • Created 9 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
sqlalchemy-botcommented, Nov 27, 2018

Adrian (@thiefmaster) wrote:

Relational databases dont generally have RENAME CONSTRAINT functions

At least PostgreSQL supports ALTER TABLE ... RENAME CONSTRAINT.

0reactions
sqlalchemy-botcommented, Nov 27, 2018

Changes by Michael Bayer (@zzzeek):

  • changed status to closed
Read more comments on GitHub >

github_iconTop Results From Across the Web

RENAME CONSTRAINT | CockroachDB Docs
The RENAME CONSTRAINT statement changes the name of a constraint on a column. Note: The RENAME CONSTRAINT statement performs a schema change. For...
Read more >
Rename a constraint in SQL Server? - Stack Overflow
You can rename using sp_rename using @objtype = 'OBJECT' ... If the constraint to be renamed has a period in it (dot), then...
Read more >
How to Use SQL Query to Rename a Constraint?
SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME='INFO';. Output: Step 10: Rename the user- ...
Read more >
Rename a CHECK Constraint in SQL Server using T-SQL
Run this to rename the constraint. sp_rename 'dbo.chkValidEndDate', 'chkEndDate';. So the old (existing) name comes first, followed by the new ...
Read more >
How to Rename a Constraint in Oracle - Dot Net Tutorials
We need to use the following syntax to rename a constraint in oracle. Syntax: ALTER TABLE <TN> RENAME CONSTRAINT < OLD CONSTRAINT NAME>...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found