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

[BUG]: Check constraint are not being generated for postgres. #3520

Open
1 task done
denispaluca opened this issue Nov 10, 2024 · 1 comment
Open
1 task done

[BUG]: Check constraint are not being generated for postgres. #3520

denispaluca opened this issue Nov 10, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@denispaluca
Copy link

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.36.1

What version of drizzle-kit are you using?

0.28.0

Other packages

No response

Describe the Bug

Check constraints are not being generated when using drizzle with postgress.
Respective docs: https://orm.drizzle.team/docs/indexes-constraints#check

Schema:

export const reports = createTable(
  "report",
  {
    id: varchar("id", { length: 255 })
      .notNull()
      .primaryKey()
      .$defaultFn(() => crypto.randomUUID()),

    gameId: integer("game_id")
      .references(() => games.id)
      .notNull(),

    userId: varchar("user_id", { length: 255 })
      .notNull()
      .references(() => users.id),

    score: integer("score"),
  },
  (table) => ({
    checkConstraint: check(
      "score_check",
      sql`0 < ${table.score} AND ${table.score} < 4`,
    ),
  }),
);

Using npx drizzle-kit generate.
Generated SQL, does not contain score constraint:

CREATE TABLE IF NOT EXISTS "pandorasdb_report" (
	"id" varchar(255) PRIMARY KEY NOT NULL,
	"game_id" integer NOT NULL,
	"user_id" varchar(255) NOT NULL,
	"score" integer
);
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "pandorasdb_report" ADD CONSTRAINT "pandorasdb_report_game_id_pandorasdb_game_id_fk" FOREIGN KEY ("game_id") REFERENCES "public"."pandorasdb_game"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "pandorasdb_report" ADD CONSTRAINT "pandorasdb_report_user_id_pandorasdb_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."pandorasdb_user"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;

(The tables use the prefix "pandorasdb")

@denispaluca denispaluca added the bug Something isn't working label Nov 10, 2024
@valentinbeggi
Copy link

valentinbeggi commented Nov 12, 2024

Hello
Same issue here

export const adaptations = pgTable(
  'adaptations',
  {
    adaptationId: uuid('adaptation_id').primaryKey(),
    name: varchar('name').notNull(),
    targetedRisks: varchar('targeted_risks', { enum: PHYSICAL_RISKS })
      .array()
      .notNull(),
    description: varchar('description').notNull(),
    effectiveness: varchar('effectiveness_level', {
      enum: ADAPTATION_RISK_LEVELS,
    }).notNull(),
    cost: varchar('cost_level', { enum: ADAPTATION_RISK_LEVELS }).notNull(),
    easeOfImplementation: varchar('ease_of_implementation_level', {
      enum: ADAPTATION_RISK_LEVELS,
    }).notNull(),
    additionalInformation: varchar('additional_information'),

    created: timestamp('created_at').defaultNow(),
    modified: timestamp('updated_at').$onUpdate(() => new Date()),

    userId: uuid('user_id').notNull(),
    companyId: uuid('company_id'),
  },
  (table) => ({
    checkConstraint: check(
      'targeted_risks_check',
      sql`cardinality(${table.targetedRisks}) > 0`
    ),
  })
);

targeted_risks_check constraint is not generated

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