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]: drizzle-kit does not respect the order of columns configured in primaryKey() #2326

Open
yodhcn opened this issue May 15, 2024 · 0 comments
Labels
bug Something isn't working drizzle-kit

Comments

@yodhcn
Copy link

yodhcn commented May 15, 2024

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

0.21.1

Describe the Bug

export const worksToCreators = sqliteTable(
  'works_to_creators',
  {
    workId: integer('work_id')
      .notNull()
      .references(() => works.id),
    creatorId: integer('creator_id')
      .notNull()
      .references(() => creators.id),
    classification: text('classification', {
      enum: [
        'voice_by',
        'music_by',
        'other_by',
      ],
    }).notNull(),
  },
  (t) => ({
    pk: primaryKey({
      columns: [t.workId, t.creatorId, t.classification],
    }),
  })
)

desired column order: [t.workId, t.creatorId, t.classification]
actual column order: [t.classification, t.creator_id, t.work_id]

CREATE TABLE `works_to_creators` (
	`work_id` integer NOT NULL,
	`creator_id` integer NOT NULL,
	`classification` text NOT NULL,
	PRIMARY KEY(`classification`, `creator_id`, `work_id`), # wrong order
	FOREIGN KEY (`work_id`) REFERENCES `works`(`id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`creator_id`) REFERENCES `creators`(`id`) ON UPDATE no action ON DELETE no action
);

Expected behavior

export const worksToCreators = sqliteTable(
  'works_to_creators',
  {
    workId: integer('work_id')
      .notNull()
      .references(() => works.id),
    creatorId: integer('creator_id')
      .notNull()
      .references(() => creators.id),
    classification: text('classification', {
      enum: [
        'voice_by',
        'music_by',
        'other_by',
      ],
    }).notNull(),
  },
  (t) => ({
    pk: primaryKey({
      columns: [t.workId, t.creatorId, t.classification],
    }),
  })
)
CREATE TABLE `works_to_creators` (
	`work_id` integer NOT NULL,
	`creator_id` integer NOT NULL,
	`classification` text NOT NULL,
	PRIMARY KEY(`work_id`, `creator_id`, `classification`), # correct order
	FOREIGN KEY (`work_id`) REFERENCES `works`(`id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`creator_id`) REFERENCES `creators`(`id`) ON UPDATE no action ON DELETE no action
);

Environment & setup

import type { Config } from 'drizzle-kit'

export default {
  schema: './src/db/schema/index.ts',
  out: './drizzle',
  dialect: 'sqlite',
  driver: 'd1',
} satisfies Config
bunx drizzle-kit generate
@yodhcn yodhcn added the bug Something isn't working label May 15, 2024
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
Projects
None yet
Development

No branches or pull requests

2 participants