Drizzle Kit Generate renames columns incorrectly

I am running a drizzle-kit generate. It appears to think I added/renamed a column on all of my tables that does not exist. Any help on how or why this is happening would be appreciated!
No description
No description
26 Replies
jkb
jkbOP2w ago
"drizzle-orm": "^0.36.4",
"drizzle-kit": "^0.28.1",
"drizzle-orm": "^0.36.4",
"drizzle-kit": "^0.28.1",
Mario564
Mario5642w ago
Did you recently update Drizzle Kit? I know these kind of "unnecessary migrations" happen when breaking changes are introduced in Kit
jkb
jkbOP2w ago
@Mario564 I have not updated drizzle kit This is a new repo, and my team has been testing out drizzle, we love the syntax but we are running into so many weird issues with the migration proccess thanks for the help btw ❤️
Mario564
Mario5642w ago
Could you give me the definiton of timestampNotNullDefault and defaultColumns?
jkb
jkbOP2w ago
No description
jkb
jkbOP2w ago
No description
Mario564
Mario5642w ago
Hmm Is the variable startTime present anywhere in your code? If so, where?
jkb
jkbOP2w ago
No description
jkb
jkbOP2w ago
Its in another schema file
Mario564
Mario5642w ago
Ah, I see, I think I might know what's going on
jkb
jkbOP2w ago
Am I doing something wrong?
Mario564
Mario5642w ago
Try making timestampNotNullDefault a function instead of a variable:
const timestampNotNullDefault = () => timestamp(...).notNull();
const timestampNotNullDefault = () => timestamp(...).notNull();
I assume the issue is that timestampNotNullDefault is applied first to startTime in event table, and on subsequent tables, a new reference doesn't get created so it just uses the same one for all other tables Making a function should create a new reference each time it's called and should fix this issue
jkb
jkbOP2w ago
@Mario564 thank you! I am giving it a shot now
Mario564
Mario5642w ago
Alright, let me know if that solves it If it does, I also suggest making defaultColumns and primaryKeyDefaultUUID functions as well, just to avoid future issues with references
jkb
jkbOP2w ago
@Mario564 looks like the issue is persisting
// columnHelpers.ts
export const timestampNotNullDefault = () => timestamp({ precision: 6, withTimezone: true }).notNull()


// event.ts
export const event = pgTable(
"event",
{
id: primaryKeyDefaultUUID,
sportId: integer()
.notNull()
.references(() => sport.id),
seasonId: uuid()
.notNull()
.references(() => sportSeason.id),
eventType: integer()
.notNull()
.references(() => eventType.id),
startTime: timestampNotNullDefault(),
eventStatusId: integer()
.notNull()
.references(() => contestStatus.id),
...defaultColumns,
},
(t) => [index("event_pk_index").on(t.id)],
)
// columnHelpers.ts
export const timestampNotNullDefault = () => timestamp({ precision: 6, withTimezone: true }).notNull()


// event.ts
export const event = pgTable(
"event",
{
id: primaryKeyDefaultUUID,
sportId: integer()
.notNull()
.references(() => sport.id),
seasonId: uuid()
.notNull()
.references(() => sportSeason.id),
eventType: integer()
.notNull()
.references(() => eventType.id),
startTime: timestampNotNullDefault(),
eventStatusId: integer()
.notNull()
.references(() => contestStatus.id),
...defaultColumns,
},
(t) => [index("event_pk_index").on(t.id)],
)
jkb
jkbOP2w ago
No description
jkb
jkbOP2w ago
I assume I shoould just get rid of the helpers and define each type manually there is also no explicit documentation on allowing you to do this, it mentions being able to create objects for commonly used columns, but it also implicitly suggests you can
There are a few tricks you can use with Drizzle ORM. As long as Drizzle is entirely in TypeScript files, you can essentially do anything you would in a simple TypeScript project with your code.
There are a few tricks you can use with Drizzle ORM. As long as Drizzle is entirely in TypeScript files, you can essentially do anything you would in a simple TypeScript project with your code.
Mario564
Mario5642w ago
Do you get prompted to change other columns? This seems to be correct, since the event table is the one with startTime column. This correct behavior unless you get prompted to change more columns on other tables
jkb
jkbOP2w ago
I got prompted to change other columns I even removed all the references and explicitly added the timestamp in each table and it is still happening
Mario564
Mario5642w ago
Is startTime defined anywhere else aside from event table?
jkb
jkbOP2w ago
no
Mario564
Mario5642w ago
Let's test something: will it work if you explicitly define an alias for the column?
timestamp('db_name/alias', { precision: 6, withTimezone: true }).notNull()
timestamp('db_name/alias', { precision: 6, withTimezone: true }).notNull()
jkb
jkbOP2w ago
That seemed to stop it from effecting other tables
Mario564
Mario5642w ago
You might need to use this workaround for the time being, I'm not sure what exactly is going on that causes this behavior Especially since you mention this, this should work pretty much all the time
jkb
jkbOP2w ago
@Mario564 I removed all my previous migrations and started from scratch. Thank you again for the help it seems to be fine for now. At least I know some steps to resolve it 👍
Mario564
Mario5642w ago
Sounds good. I and others will be here to assist if needed
Want results from more Discord servers?
Add your server