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]: unclear how to correctly define mutual foreign keys (introspection creates invalid schema) #2993

Open
fnimick opened this issue Sep 20, 2024 · 1 comment
Labels
bug Something isn't working drizzle/kit priority Will be worked on next

Comments

@fnimick
Copy link

fnimick commented Sep 20, 2024

What version of drizzle-orm are you using?

0.33.0

What version of drizzle-kit are you using?

0.24.2

Describe the Bug

Defining tables with foreign keys to each other causes drizzle-kit to output schema that does not type-check.

The following postgres schema:

create schema drizzle_test;

create table drizzle_test.child (
    id uuid primary key default uuid_generate_v4(),
    other_id uuid not null
);

create table drizzle_test.parent (
    id uuid primary key default uuid_generate_v4(),
    other_id uuid not null,
    child_id uuid unique references drizzle_test.child (id) on delete restrict,
    unique (other_id, child_id)
);

alter table drizzle_test.child add constraint test_key
foreign key (other_id, id)
references drizzle_test.parent (other_id, child_id);

generates the following schema from introspection:

import { sql } from "drizzle-orm";
import { foreignKey, pgSchema, pgTable, unique, uuid, type AnyPgColumn } from "drizzle-orm/pg-core";

export const drizzleTest = pgSchema("drizzle_test");

export const childInDrizzleTest = drizzleTest.table(
  "child",
  {
    id: uuid("id")
      .default(sql`uuid_generate_v4()`)
      .primaryKey()
      .notNull(),
    parentId: uuid("parent_id").notNull(),
  },
  (table) => {
    return {
      testKey: foreignKey({
        columns: [table.parentId],
        foreignColumns: [parentInDrizzleTest.id],
        name: "test_key",
      }),
    };
  },
);

export const parentInDrizzleTest = drizzleTest.table(
  "parent",
  {
    id: uuid("id")
      .default(sql`uuid_generate_v4()`)
      .primaryKey()
      .notNull(),
    childId: uuid("child_id"),
  },
  (table) => {
    return {
      parentChildIdFkey: foreignKey({
        columns: [table.childId],
        foreignColumns: [childInDrizzleTest.id],
        name: "parent_child_id_fkey",
      }),
      parentChildIdKey: unique("parent_child_id_key").on(table.childId),
    };
  },
);

This fails to type-check due to the table definitions referencing each other in the extraConfig function.

Expected behavior

Either introspect generates correct schema, or introspect does not generate foreign keys that would cause reference loops.

In addition, documentation on how to handle this when defining schema would be welcome.

Environment & setup

  System:
    OS: macOS 15.0
    CPU: (10) arm64 Apple M1 Pro
    Memory: 117.55 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.13.1 - ~/.asdf/installs/nodejs/20.13.1/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 10.5.2 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 9.7.0 - ~/.asdf/shims/pnpm
  npmPackages:
    drizzle-kit: ^0.24.2 => 0.24.2 
    drizzle-orm: ^0.33.0 => 0.33.0 
@fnimick fnimick added the bug Something isn't working label Sep 20, 2024
@L-Mario564 L-Mario564 added drizzle/kit priority Will be worked on next labels Oct 24, 2024
@raikusy
Copy link

raikusy commented Oct 25, 2024

Facing same issue! It was working on previous versions... :'(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working drizzle/kit priority Will be worked on next
Projects
None yet
Development

No branches or pull requests

3 participants