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

potential out-of-order operations on table name change #256

Closed
albinkc opened this issue Apr 30, 2024 · 1 comment
Closed

potential out-of-order operations on table name change #256

albinkc opened this issue Apr 30, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@albinkc
Copy link
Contributor

albinkc commented Apr 30, 2024

Migration file: https://github.com/albinkc/helpdesk/blob/main/priv/repo/migrations/20240430205016_rename_representative.exs

changing table name, keeping everything else intact, gives me this error:

albin@Albins-MacBook-Pro helpdesk % mix ash.migrate                      
Getting extensions in current project...
Running migration for AshPostgres.DataLayer...

02:20:46.825 [info] == Running 20240430205016 Helpdesk.Repo.Migrations.RenameRepresentative.up/0 forward

02:20:46.827 [info] drop constraint tickets_representative_id_fkey from table tickets

02:20:46.829 [info] create table representatives

02:20:46.836 [info] alter table tickets
** (Postgrex.Error) ERROR 23503 (foreign_key_violation) insert or update on table "tickets" violates foreign key constraint "tickets_representative_id_fkey"

    table: tickets
    constraint: tickets_representative_id_fkey

Key (representative_id)=(d75edc31-72d0-4e95-b840-2f5cd934aeb5) is not present in table "representatives".
    (ecto_sql 3.11.1) lib/ecto/adapters/sql.ex:1054: Ecto.Adapters.SQL.raise_sql_call_error/1
    (elixir 1.16.2) lib/enum.ex:1700: Enum."-map/2-lists^map/1-1-"/2
    (ecto_sql 3.11.1) lib/ecto/adapters/sql.ex:1161: Ecto.Adapters.SQL.execute_ddl/4
    (ecto_sql 3.11.1) lib/ecto/migration/runner.ex:348: Ecto.Migration.Runner.log_and_execute_ddl/3
    (elixir 1.16.2) lib/enum.ex:1700: Enum."-map/2-lists^map/1-1-"/2
    (elixir 1.16.2) lib/enum.ex:1700: Enum."-map/2-lists^map/1-1-"/2
    (ecto_sql 3.11.1) lib/ecto/migration/runner.ex:311: Ecto.Migration.Runner.perform_operation/3
    (stdlib 5.2) timer.erl:270: :timer.tc/2
@albinkc albinkc added bug Something isn't working needs review labels Apr 30, 2024
@zachdaniel
Copy link
Contributor

Ahhhhhhhhhhh, right. I think this is because the migration generator doesn't currently handle table renaming. It just makes the new table and leaves the old one there. This is naturally not ideal :)

To fix this, we'd need to handle table renames, as part of #224, where if a table is missing, we check to see if you're trying to rename it to some new table being added. Going to close this in favor of a note in that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants