Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot drop foreign key constraints that include a column with a serial type #633

Open
kvch opened this issue Jan 28, 2025 · 0 comments
Open
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@kvch
Copy link
Contributor

kvch commented Jan 28, 2025

When we are trying to drop a foreign key constraint (e.g. with drop_multicolumn_constraint) including a column that has serial type, we get an error when we try to complete the migration:

cannot drop column {serial_column} of table {tablename} because other objects depend on it

When we are duplicating a column of serial type, the underlying sequence is not duplicated. See provider_id is duplicated to _pgroll_new_provider_id, but the default value is set to the same sequence:

postgres=# \d phonebook
                                                 Table "public.phonebook"
         Column          |          Type          | Collation | Nullable |                    Default
-------------------------+------------------------+-----------+----------+------------------------------------------------
 id                      | integer                |           | not null | nextval('phonebook_id_seq'::regclass)
 provider_id             | integer                |           | not null | nextval('phonebook_provider_id_seq'::regclass)
 _pgroll_new_provider_id | integer                |           |          | nextval('phonebook_provider_id_seq'::regclass)

Foreign-key constraints:
    "provider_fk" FOREIGN KEY (provider_id) REFERENCES telephone_providers(id) ON UPDATE CASCADE ON DELETE CASCADE

When we try to drop the column as usual (without cascade) we get an error:

postgres=# alter table phonebook drop column provider_id;
ERROR:  cannot drop column provider_id of table phonebook because other objects depend on it
DETAIL:  default value for column _pgroll_new_provider_id of table phonebook depends on sequence phonebook_provider_id_seq
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

But if we drop the column with CASCADE option, the default value of the duplicated column is gone:

postgres=# \d phonebook
                                            Table "public.phonebook"
         Column          |          Type          | Collation | Nullable |                Default
-------------------------+------------------------+-----------+----------+---------------------------------------
 id                      | integer                |           | not null | nextval('phonebook_id_seq'::regclass)
 _pgroll_new_provider_id | integer                |           |          |

We must fix two things:

  • drop the original column (provider_id)
  • preserve the default value for the duplicated column (_pgroll_new_provider_id)
@kvch kvch added bug Something isn't working good first issue Good for newcomers labels Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant