drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.61k stars 649 forks source link

[BUG]: Pushing a table with a fixed array will fail on certain conditions (drizzle-orm/pg-core) #3582

Open vignedev opened 2 days ago

vignedev commented 2 days ago

Report hasn't been filed before.

What version of drizzle-orm are you using?

^0.36.3

What version of drizzle-kit are you using?

^0.28.1

Other packages

tsx@4.19.2

Describe the Bug

Apolgies if this has been reported, as I couldn't find it. Using PostgreSQL, and node-postgres with drizzle. Specifying the schema as such:

import { pgTable, serial, real } from 'drizzle-orm/pg-core'

export const testTable = pgTable('test_table', {
  testId: serial('test_id').primaryKey().notNull(),
  bbox: real('bbox').array(4) // <-- desired fixed size
})

...and pushing it with npx drizzle-kit push will succeed. However, if I insert anything with the following snippet:

import { drizzle } from 'drizzle-orm/node-postgres'
import * as schema from './schema'

const db = drizzle(process.env['POSTGRESQL_DB'], { schema })
db.insert(schema.testTable).values({
  bbox: [0, 1, 2, 3]
}).then(console.log)

...and later try npx drizzle-kit push, I will get the following:

$ npx drizzle-kit push

No config path provided, using default 'drizzle.config.ts'
Reading config file '/home/vignette/Projects/drizzle-bugreport/drizzle.config.ts'
Using 'pg' driver for database querying
[✓] Pulling schema from database...
 Warning  Found data-loss statements:
· You're about to change bbox column type from real[] to real[4] with 1 items

THIS ACTION WILL CAUSE DATA LOSS AND CANNOT BE REVERTED

Do you still want to push changes?
[x] All changes were aborted

More over, the pulled schema does not contain the array size restrictions. Here's the output of npx drizzle-kit pull's schema.ts:

import { pgTable, serial, real } from "drizzle-orm/pg-core"
import { sql } from "drizzle-orm"

export const testTable = pgTable("test_table", {
    testId: serial("test_id").primaryKey().notNull(),
    bbox: real().array(),
});