Skip to content

Conversation

farhadham
Copy link

This pull request fixes the incorrect example in the Drizzle ORM documentation for defining composite primary keys using the primaryKey operator. The current example mistakenly uses an object with keys pk and pkWithCustomName to define composite primary keys. This syntax does not work as intended and may lead to confusion for developers.

Changes Made

  • Updated the composite primary key example to use the correct syntax:
  • Returned an array of primaryKey definitions directly instead of wrapping them inside an object.
  • Fixed incorrect component import in test files causing tests to fail

Before Change (Incorrect):

export const booksToAuthors = pgTable("books_to_authors", {
  authorId: integer("author_id"),
  bookId: integer("book_id"),
}, (table) => {
  return [{
    pk: primaryKey({ columns: [table.bookId, table.authorId] }),
    pkWithCustomName: primaryKey({ name: 'custom_name', columns: [table.bookId, table.authorId] }),
  }];
});

After Fix:

export const booksToAuthors = pgTable(
  "books_to_authors",
  {
    authorId: integer("author_id"),
    bookId: integer("book_id"),
  },
  (table) => {
    return [
      primaryKey({ columns: [table.bookId, table.authorId] }),
      primaryKey({ name: "custom_name", columns: [table.bookId, table.authorId] }),
    ];
  },
);

Closes #456

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

Successfully merging this pull request may close these issues.

Incorrect Example for Defining Composite Primary Keys
1 participant