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]:Composite primary key not added to postgres schema when using array return in pgTable #3596

Closed
1 task done
hghayebi opened this issue Nov 22, 2024 · 7 comments
Closed
1 task done
Labels
bug Something isn't working db/postgres drizzle/kit priority Will be worked on next

Comments

@hghayebi
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.3

What version of drizzle-kit are you using?

0.28.1

Other packages

No response

Describe the Bug

What is the undesired behavior?
When I return an array from pgTable callback, composite primary key not adds to my schema in postgres. but without array it works.

steps to reproduce

the code below not create primary key in postgress:
export const postOnCategories = pgTable(
"post_categories",
{
postId: serial("post_id")
.notNull()
.references(() => posts.id),
categoryId: serial("category_id")
.notNull()
.references(() => categories.id),
},
(table) => [
{
pk: primaryKey({ columns: [table.postId, table.categoryId] }),
},
]
);

result:
image

but old way of returning an object works properly:
export const postOnCategories = pgTable(
"post_categories",
{
postId: serial("post_id")
.notNull()
.references(() => posts.id),
categoryId: serial("category_id")
.notNull()
.references(() => categories.id),
},
(table) => ({
pk: primaryKey({ columns: [table.postId, table.categoryId] }),
})
);

image
@hghayebi hghayebi added the bug Something isn't working label Nov 22, 2024
@hghayebi hghayebi changed the title [BUG]:Composite primary key not work with array return in pgTable [BUG]:Composite primary key not create on postgres when using array return in pgTable Nov 22, 2024
@hghayebi hghayebi changed the title [BUG]:Composite primary key not create on postgres when using array return in pgTable [BUG]:Composite primary key not added to postgres schema when using array return in pgTable Nov 22, 2024
@tobi-or-not-tobi
Copy link

I can confirm this bug, I was trying to get ride of deprecation warnings based on https://github.com/drizzle-team/drizzle-orm/releases/tag/0.36.0, but noticed the composed PK is not added to my pg db.

@nhamilton1
Copy link

I just ran into this as well, seems to also happen with unique constraints. Going to revert back to (t) => ({...}) in the meantime.

@rogerhlg
Copy link

also happing with mysql

@AdamAkiva
Copy link

TLDR
Change this:

(table) => [
    {
      pk: primaryKey({ columns: [table.postId, table.categoryId] }),
    },
  ],

to:

(table) => [primaryKey({ columns: [table.postId, table.categoryId] })],

Don't know whether it is postgres specific or not, but the solution I've given here works for this case as well. (Tested locally with the given drizzle & drizzle-kit versions): #3873 (comment)

@AntonOfTheWoods
Copy link

Still present and above hack still works with 0.30.1

@jasongerbes
Copy link

jasongerbes commented Jan 4, 2025

This appears to be an issue with the docs: https://github.com/drizzle-team/drizzle-orm-docs/pull/4530

The 0.36.0 release notes and the pgTable function signature deprecation message (below) support the solution proposed by @AdamAkiva as the correct approach.

/**
 * @deprecated The third parameter of pgTable is changing and will only accept an array instead of an object
 *
 * @example
 * Deprecated version:
 * ```ts
 * export const users = pgTable("users", {
 * 	id: integer(),
 * }, (t) => ({
 * 	idx: index('custom_name').on(t.id)
 * }));
 * ```
 *
 * New API:
 * ```ts
 * export const users = pgTable("users", {
 * 	id: integer(),
 * }, (t) => [
 * 	index('custom_name').on(t.id)
 * ]);
 * ```
 */

The bug here seems to be a type issue with the new function signature, as pgTable allows an arbitrary array of invalid values to be returned by the third parameter:

import { pgTable, text } from 'drizzle-orm/pg-core'

export const test = pgTable(
  'test',
  {
    id: text('id').primaryKey().notNull(),
  },
  (t) => [{ one: 2 }, false, 1, new Set()],
)

@L-Mario564
Copy link
Collaborator

The docs have been updated to reflect the new syntax for declaring constraints.

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

No branches or pull requests

8 participants