Closed lud-hu closed 2 weeks ago
Same issue with latest drizzle-kit and turso driver.
I narrowed down the issue to the .default(sql`CURRENT_TIMESTAMP`)
function.
If a column is added with that default clause/function then the table gets altered, copied, and dropped every time db:push
is run
Reproduction steps:
Expect: drizzle-kit reports no changes
Observed: drizzle-kit wants to rename the table, create a new table with the correct name, copy over all the data from the old table to the new table, and finally drop the renamed table
Update: narrowed the issue down even further to use of sql
function inside the default
function
Update: narrowed the issue down even further, seems to be directly triggered by using CURRENT_TIMESTAMP
inside of the sql template string
timestamp: text("timestamp").default(sql`CURRENT_TIMESTAMP`), ==> TRIGGERS ALTER TABLE BUG
timestamp: text("timestamp").default(sql`(abs(42))`), ==> no alters table bug
You're trying to push date into an integer column, the correct way:
createdAt: integer('created_at', { mode: 'timestamp' })
.notNull()
.default(sql`(unixepoch())`),
Ref: https://orm.drizzle.team/learn/guides/timestamp-default-value#sqlite
Hello guys, this issue occurs due to incorrect usage of CURRENT_TIMESTAMP
function with sql
operator.
When you use functions with sql
operators you should always wrap them into parentheses because SQL might not interpret them correctly. In the database, it should be (CURRENT_TIMESTAMP)
, but if you pass without parentheses Drizzle will think that it is a new default value and will detect the changes.
timestamp: text("timestamp").default(sql`(CURRENT_TIMESTAMP)`) // correct
Hello @lud-hu @blenoski Could you please confirm that these approaches fix your issue? If not, let's discuss it here or in discord
// if you need to store date as string
timestamp: text("timestamp").default(sql`(CURRENT_TIMESTAMP)`)
or
// if you need to store data as number and treat it as Date object in application
timestamp: integer('timestamp', { mode: 'timestamp' }).notNull().default(sql`(unixepoch())`),
What version of
drizzle-orm
are you using?v0.28.6
What version of
drizzle-kit
are you using?v0.19.13
Describe the Bug
I have a fairly simple app where I have trouble getting the db migrations done right. For example there is a simple schema:
I can push it perfectly to a fresh turso db:
bunx drizzle-kit push:sqlite
When I execute the same command exactly after the initial push, it already detects some changes which it shouldn't, should it?
bunx drizzle-kit push:sqlite
The problem popped up after trying to just add one simple column to an existing db. The migration failed due to a foreign key constrain failure (which it shouldn't even have because I just added one column) and then in the next try the
__old...
tables as shown in the unnecessary migration cause problemsExpected behavior
No migration needed after executing the
push:sqlite
command again without changes.Environment & setup
MacOS, bun, drizzle, turso and sqlite.