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]: Switching to (table) => [{}] with array syntax drops indexes #3930

Open
1 task done
dejoma opened this issue Jan 10, 2025 · 2 comments
Open
1 task done

[BUG]: Switching to (table) => [{}] with array syntax drops indexes #3930

dejoma opened this issue Jan 10, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@dejoma
Copy link

dejoma commented Jan 10, 2025

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.38.3

What version of drizzle-kit are you using?

0.30.1

Other packages

No response

Describe the Bug

Table:

export const logs = pgTable(
  'logs',
  {
    id: serial('id').primaryKey().notNull(),
    bookId: integer('book_id').references(() => books.id),
    courseId: integer('course_id').references(() => courses.id),
    metadata: text('metadata')
  },
  (table) => ({
      courseIndex: index('idx_logs_course_id').on(table.courseId),
      bookIndex: index('idx_logs_book_id').on(table.bookId)
    })
);

Then I get a deprecation warning on pgTable, and when I check the docs I see a new syntax for indexes/unique-constraints/etc. namely:

export const logs = pgTable(
'logs',
{...},
(table) => [{
      courseIndex: index('idx_logs_course_id').on(table.courseId),
      bookIndex: index('idx_logs_book_id').on(table.bookId)
    }]
)

Note the difference in the return value of that last argument, it's an array. Then if I run a migration, the SQL deletes indexes/constraints/etc.
SQL migration generated with drizzle-kit generate

DROP INDEX "idx_logs_course_id";
DROP INDEX "idx_logs_book_id";
@dejoma dejoma added the bug Something isn't working label Jan 10, 2025
@UnexDev
Copy link

UnexDev commented Jan 10, 2025

Correct usage is

export const logs = pgTable(
    'logs',
    {...},
    (table) => [
        index('idx_logs_course_id').on(table.courseId),
        index('idx_logs_book_id').on(table.bookId)
    ]
)

@dejoma
Copy link
Author

dejoma commented Jan 10, 2025

Correct usage is

export const logs = pgTable(
    'logs',
    {...},
    (table) => [
        index('idx_logs_course_id').on(table.courseId),
        index('idx_logs_book_id').on(table.bookId)
    ]
)

Thanks, but could you also send me a link to the documentation where that is done?

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