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.

On table or column name change, rename contstraint names to match new name

See original GitHub issue

Currently Hasura does not update constraint names when changing a table or column name.

Consider having an initial table called clients with an id and email column, id is the primary-key and biginit unique autoincrementing, email is text and unique.

Hasura will show the constraints on this table as the following:

Primary key

  • id - clients_pkey

Unique keys

  • email - clients_email_key

Now rename the table to users and the constraint names will not change.

Perhaps they should be updated to keep semantic value, although it’s not breaking anything so this is probably(?) a minor priority change.

Example SQL to tie into changes:

ALTER TABLE name RENAME CONSTRAINT constraint_name TO new_constraint_name;

Also credit to elitan#8042 for suggestions relating to this issue.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:9
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
shakahlcommented, Feb 6, 2020

Hi @tsujp The constraint names are something that the GraphQL engine entirely depends on Postgres. It is hard to update the constraint names explicitly for renamed tables and columns. You can run ALTER TABLE .. in SQL section of the console and the server auto-updates the required metadata.

If you feel it is good to have an edit option in console UI for constraint names, let us know in the comment. 🙂

An edit button would be great there!

0reactions
Varun-Choudharycommented, Nov 22, 2022

I came into an issue where I renamed a table to better fit the data that is in the table. Then I later realized that I wanted to restructure the data so I added another table (with the name of the table before it was renamed). I then tried adding a primary key on the table but it was saying the key already existed.

So, I was not able to add a key to this table. I then figured out that the key was already in use in the renamed table so I deleted the key and re-added it so it would reflect the table. I was then able to add the key to the newly created table.

Steps I took:

  • Initial creation: items
  • Rename: items -> user_items
  • Re-create: items
  • Add new primary key to items – Throws error

Hey @TheColorRed, thanks for reporting the issue. This issue is there till v2.12.0 but it is resolved now. Initial creation: items, let the constraint_name is items_pkey Rename items -> user_items Re-create: items Add a new primary key to items – this will add a primary with constraint name items_pkey1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rename constraint reference that changed because of table ...
You'll have add a "new" constraint. Internally the database doesn't care about the name of things (much). Rather than using names for things ......
Read more >
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.
Read more >
Script to rename constraints and indexes to conform to a SQL ...
Problem. In many systems, keys, indexes and constraints are given names generated by the system. These system-generated names relate ...
Read more >
Renaming column does not update FK definition - MySQL Bugs
The solution is to iterate over the table fields and check if any of the columns were renamed. If a column was renamed,...
Read more >
Alter Table in SQL – How to Add a Column or Rename a ...
When you rename columns using ALTER TABLE you risk breaking database dependencies. If you use a database refactoring tool to change the 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