theodor
theodor
Explore posts from servers
DTDrizzle Team
Created by theodor on 11/28/2023 in #help
Index not being created in table
Hi! I'm using drizzle-kit push:pg to update my local dev database. When I use this command, indices with shared names (e.g. updated_at_idx) only get created on the first table in the schema.ts. For example, if I have:
export const users = pgTable(
'user',
{
id: uuid('id').primaryKey().defaultRandom().notNull(),
name: text('name'),
email: text('email'),
createdAt: timestamp('created_at', { precision: 3 }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { precision: 3 }).notNull().defaultNow(),
},
(table) => ({
emailIdx: index('email_idx').on(table.email),
nameIdx: index('name_idx').on(table.name),
createdAtIdx: index('created_at_idx').on(table.createdAt),
updatedAtIdx: index('updated_at_idx').on(table.updatedAt),
}),
);
export const users = pgTable(
'user',
{
id: uuid('id').primaryKey().defaultRandom().notNull(),
name: text('name'),
email: text('email'),
createdAt: timestamp('created_at', { precision: 3 }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { precision: 3 }).notNull().defaultNow(),
},
(table) => ({
emailIdx: index('email_idx').on(table.email),
nameIdx: index('name_idx').on(table.name),
createdAtIdx: index('created_at_idx').on(table.createdAt),
updatedAtIdx: index('updated_at_idx').on(table.updatedAt),
}),
);
and:
export const conversations = pgTable(
'conversation',
{
id: uuid('id').primaryKey().defaultRandom().notNull(),
name: text('name'),
userId: uuid('user_id')
.notNull()
.references(() => users.id),
createdAt: timestamp('created_at', { precision: 3 }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { precision: 3 }).notNull().defaultNow(),
},
(table) => ({
userIdIdx: index('user_id_idx').on(table.userId),
createdAtIdx: index('created_at_idx').on(table.createdAt),
updatedAtIdx: index('updated_at_idx').on(table.updatedAt),
}),
);
export const conversations = pgTable(
'conversation',
{
id: uuid('id').primaryKey().defaultRandom().notNull(),
name: text('name'),
userId: uuid('user_id')
.notNull()
.references(() => users.id),
createdAt: timestamp('created_at', { precision: 3 }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { precision: 3 }).notNull().defaultNow(),
},
(table) => ({
userIdIdx: index('user_id_idx').on(table.userId),
createdAtIdx: index('created_at_idx').on(table.createdAt),
updatedAtIdx: index('updated_at_idx').on(table.updatedAt),
}),
);
The created_at and updated_at indices on conversations will NOT be created. I noticed that the SQL gets generated correctly if I generate the migration.
1 replies