tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
804 stars 90 forks source link

FK column unique not working #326

Open Prof1-web opened 6 months ago

Prof1-web commented 6 months ago

My models:

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

class Client(Model):
    id = fields.BigIntField(pk=True)
    user = fields.ForeignKeyField("models.User", on_delete=fields.OnDelete.NO_ACTION, unique=True)

For this models aerich generate:

CREATE TABLE IF NOT EXISTS "users" (
    "id" BIGSERIAL NOT NULL PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS "clients" (
    "id" BIGSERIAL NOT NULL PRIMARY KEY,
    "user_id" BIGINT NOT NULL REFERENCES "users" ("id") ON DELETE NO ACTION
);
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
);

I am expecting unique constraint on the user_id column, but aerich is ignoring it