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

FK column rename error #325

Open
Prof1-web opened this issue Dec 14, 2023 · 0 comments
Open

FK column rename error #325

Prof1-web opened this issue Dec 14, 2023 · 0 comments

Comments

@Prof1-web
Copy link

Prof1-web commented Dec 14, 2023

Aerich 0.7.2, PostgreSQL 15.3

Example:

class User(Model):
    id = fields.BigIntField(pk=True)


class Client(Model):
    id = fields.BigIntField(pk=True)
    user = fields.ForeignKeyField("models.User")

Aerich generate:

CREATE TABLE IF NOT EXISTS "user" (
    "id" BIGSERIAL NOT NULL PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS "client" (
    "id" BIGSERIAL NOT NULL PRIMARY KEY,
    "user_id" BIGINT NOT NULL REFERENCES "user" ("id") ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS "aerich" (
    "id" SERIAL NOT NULL PRIMARY KEY,
    "version" VARCHAR(255) NOT NULL,
    "app" VARCHAR(100) NOT NULL,
    "content" JSONB NOT NULL
);

Then I changed fk_column name and ran migrate:

class Client(Model):
    id = fields.BigIntField(pk=True)
    user_new_name = fields.ForeignKeyField("models.User")

Aerich generate:

ALTER TABLE "client" DROP CONSTRAINT "fk_client_user_f9356948";
ALTER TABLE "client" RENAME COLUMN "user_id" TO "user_new_name_id";
ALTER TABLE "client" ADD CONSTRAINT "fk_client_user_66c1dc3d" FOREIGN KEY ("user_new_name_id") REFERENCES "user" ("id") ON DELETE CASCADE;

And it can't perform this migration. Because CONSTRAINT "fk_client_user_f9356948" does not exist.

And why is aerich removing the constraint? DB can rename it without removing the constraint. Removing and adding can be a problem for large tables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant