Column Unique name appears to be incorrect when spreading common fields to multiple tables

Came across this issue. Not sure if it's a big deal but i am spreading a common set of timestamp fields around to most of my tables and it looks like the unique name is incorrect. It looks like all my tables have this 'organizations_created_at_unique' naming convention on them with the unique field
[ 'created_at', 'PgTimestamp', 'organizations_created_at_unique' ],
[ 'updated_at', 'PgTimestamp', 'organizations_updated_at_unique' ],
[ 'deleted_at', 'PgTimestamp', 'organizations_deleted_at_unique' ]
[ 'created_at', 'PgTimestamp', 'organizations_created_at_unique' ],
[ 'updated_at', 'PgTimestamp', 'organizations_updated_at_unique' ],
[ 'deleted_at', 'PgTimestamp', 'organizations_deleted_at_unique' ]
import { contacts } from '@/db/schema'

const { columns } =
await getTableConfig(contacts)

console.log(columns.map(c => [c.name, c.columnType, c.uniqueName]))
import { contacts } from '@/db/schema'

const { columns } =
await getTableConfig(contacts)

console.log(columns.map(c => [c.name, c.columnType, c.uniqueName]))
This is passed
// Pass this to all necessary tables
export const createdAndUpdatedAtFields = {
createdAt: timestamp('created_at').defaultNow().notNull(), // autogenerate when record created
updatedAt: timestamp('updated_at'), // manually update
deletedAt: timestamp('deleted_at'), // for soft deletes
}
// Pass this to all necessary tables
export const createdAndUpdatedAtFields = {
createdAt: timestamp('created_at').defaultNow().notNull(), // autogenerate when record created
updatedAt: timestamp('updated_at'), // manually update
deletedAt: timestamp('deleted_at'), // for soft deletes
}
export const contacts = pgTable('contacts', {
contactId: uuid('contact_id')
.default(sql`uuid_generate_v7()`)
.primaryKey(),
firstName: varchar('first_name', { length: 100 }),
lastName: varchar('last_name', { length: 100 }),
...createdAndUpdatedAtFields,
})
export const contacts = pgTable('contacts', {
contactId: uuid('contact_id')
.default(sql`uuid_generate_v7()`)
.primaryKey(),
firstName: varchar('first_name', { length: 100 }),
lastName: varchar('last_name', { length: 100 }),
...createdAndUpdatedAtFields,
})
No description
11 Replies
Angelelz
Angelelz17mo ago
Interesting, not sure if that'll have any impact... Can you try creating the table like this:
export const contacts = pgTable('contacts', {
contactId: uuid('contact_id')
.default(sql`uuid_generate_v7()`)
.primaryKey(),
firstName: varchar('first_name', { length: 100 }),
lastName: varchar('last_name', { length: 100 }),
...{ ...createdAndUpdatedAtFields },
})
export const contacts = pgTable('contacts', {
contactId: uuid('contact_id')
.default(sql`uuid_generate_v7()`)
.primaryKey(),
firstName: varchar('first_name', { length: 100 }),
lastName: varchar('last_name', { length: 100 }),
...{ ...createdAndUpdatedAtFields },
})
I just wonder if creating a brand new object per table, would create an unique name Pinging @Andrew Sherman for his insight
Andrii Sherman
Andrii Sherman17mo ago
so the problem is that the name is the same across different tables? Oh, or that you don't have unique and still see the unique name?
Angelelz
Angelelz17mo ago
Who call build on the ColumnBuilder classes? Is it drizzle-kit or the drizzle object when you pass in the schema? And what is uniqueName used for anyway?
Andrii Sherman
Andrii Sherman17mo ago
drizzle-kit just calls getTableConfig() https://orm.drizzle.team/docs/goodies#get-table-information and gets all the info about table uniqueName is used by drizzle-kit to drop unique by it's name, and to create with a specific name(user can specify a custom unique constraint name)
Goodies – DrizzleORM
Drizzle ORM | %s
Andrii Sherman
Andrii Sherman17mo ago
so uniqueName is not used in drizzle-orm at all it's just a metadata for getTableConfig() and for anyone using it either it's drizzle-kit or some other library that depends on drizzle-orm schema metadata it's generated here by default, didn't see any problems on generating it for a column. But now I think I can generate it only when .unique() functions is used
DiamondDragon
DiamondDragonOP17mo ago
doesn't seem to change the result
No description
Angelelz
Angelelz17mo ago
You would need to create an unique name yourself if that's an issue.
Andrii Sherman
Andrii Sherman17mo ago
Just to be sure, uniqueName is about name of a unique constraint that may be generated for this column My bad to generate it always, even if unique constraint us not set up It won’t cause any issue now Just internal metadata I guess we will need to improve it a bit
DiamondDragon
DiamondDragonOP17mo ago
Does this get fed to the migrations? That’s the only place I could see it cause issues perhaps if you create constraints and it’s with the wrong unique name that gets pushed to the db
Angelelz
Angelelz17mo ago
Yeah but you have the option to put whatever name you want in the object you pass to .unique()
DiamondDragon
DiamondDragonOP17mo ago
Ah - makes sense. Got it
Want results from more Discord servers?
Add your server