Is this a bug with arrays?

In postgres I have a column defined like this:
someArrayField: text('someArrayField')
.array()
.default(sql`ARRAY[]::text[]`)
.notNull()
someArrayField: text('someArrayField')
.array()
.default(sql`ARRAY[]::text[]`)
.notNull()
For insertions, drizzle says I don't need to define someArrayField since it has a default. Yet when I try to insert a record, I get this error:
error: null value in column "someArrayField" of relation "SomeTable" violates not-null constraint
error: null value in column "someArrayField" of relation "SomeTable" violates not-null constraint
Am I doing this incorrectly? interestingly, on localhost, this has no issues when I use import { drizzle } from 'drizzle-orm/postgres-js'; This only has issues when deployed when I use import { drizzle } from 'drizzle-orm/neon-serverless'; I wanted to just do .default([]) but then drizzle kit tells me:
No config path provided, using default path
Reading config file '/Users/jakeleventhal/Code/rip-technologies/packages/ecominate/database/drizzle.config.ts'
error: syntax error at or near ";"
at /Users/jakeleventhal/Code/rip-technologies/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:24462:21
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async PgPostgres.query (/Users/jakeleventhal/Code/rip-technologies/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:25423:21)
at async Command.<anonymous> (/Users/jakeleventhal/Code/rip-technologies/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:63244:9) {
No config path provided, using default path
Reading config file '/Users/jakeleventhal/Code/rip-technologies/packages/ecominate/database/drizzle.config.ts'
error: syntax error at or near ";"
at /Users/jakeleventhal/Code/rip-technologies/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:24462:21
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async PgPostgres.query (/Users/jakeleventhal/Code/rip-technologies/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:25423:21)
at async Command.<anonymous> (/Users/jakeleventhal/Code/rip-technologies/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:63244:9) {
17 Replies
Angelelz
Angelelz12mo ago
Can you try changing the the column to:
someArrayField: text('someArrayField')
.array()
.$default([])
.notNull()
someArrayField: text('someArrayField')
.array()
.$default([])
.notNull()
The question I'd have is, did you run push/migrate after you defined the the default?
jakeleventhal
jakeleventhalOP12mo ago
when i defined it like this, yes i pushed it
.default(sql`ARRAY[]::text[]`)
.default(sql`ARRAY[]::text[]`)
jakeleventhal
jakeleventhalOP12mo ago
No description
jakeleventhal
jakeleventhalOP12mo ago
$default([]) doesnt work using .$default(() => []) in my code seems to work well, once a record exists in the db, if i try to change from ARRAY[]::text[] to () => [] it says
· You're about to remove default value from someOtherFieldWithData not-null column with 60 items
· You're about to remove default value from someOtherFieldWithData not-null column with 60 items
Angelelz
Angelelz12mo ago
My bad, this is the correct code You don't have to run migrations for this. The $default function run purely in js
Angelelz
Angelelz12mo ago
Also, you might have better luck with the following syntax. See here https://stackoverflow.com/questions/30933266/empty-array-as-postgresql-array-column-default-value
Stack Overflow
Empty array as PostgreSQL array column default value
I have a defined an array field in postgresql 9.4 database: character varying(64)[] Can I have an empty array e.g. {} for default value of that field? What will be the syntax for setting so? I'm
Angelelz
Angelelz12mo ago
someArrayField: text('someArrayField')
.array()
.default(sql`'{}'`)
.notNull()
someArrayField: text('someArrayField')
.array()
.default(sql`'{}'`)
.notNull()
jakeleventhal
jakeleventhalOP12mo ago
.default(sql`'{}'`)
.default(sql`'{}'`)
this syntax works why wouldnt the original ARRAY[]::text[] work?
Angelelz
Angelelz12mo ago
No idea
jakeleventhal
jakeleventhalOP12mo ago
seems like two: 1. ARRAY[] stynax default doesnt work for neon (probably a bug in neon?) 2. drizzle does not handle default arrays properly: .default([]) yeilds:
No config path provided, using default path
Reading config file '/Users/jakeleventhal/Code/rip-technologies/packages/ecominate/database/drizzle.config.ts'
error: syntax error at or near ";"
at /Users/jakeleventhal/Code/rip-technologies/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:24462:21
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async PgPostgres.query (/Users/jakeleventhal/Code/rip-technologies/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:25423:21)
at async Command.<anonymous> (/Users/jakeleventhal/Code/rip-technologies/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:63244:9) {
No config path provided, using default path
Reading config file '/Users/jakeleventhal/Code/rip-technologies/packages/ecominate/database/drizzle.config.ts'
error: syntax error at or near ";"
at /Users/jakeleventhal/Code/rip-technologies/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:24462:21
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async PgPostgres.query (/Users/jakeleventhal/Code/rip-technologies/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:25423:21)
at async Command.<anonymous> (/Users/jakeleventhal/Code/rip-technologies/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/bin.cjs:63244:9) {
Angelelz
Angelelz12mo ago
The default() is designed to receive sql. Although unfortunately the interfaces doesn't convey that.
Angelelz
Angelelz12mo ago
GitHub
[BUG]: .default() method accepts Date object but should only accept...
What version of drizzle-orm are you using? 0.29.1 What version of drizzle-kit are you using? 0.20.6 Describe the Bug This relates to #1105 (comment). When using an integer column in timestamp mode,...
jakeleventhal
jakeleventhalOP12mo ago
weird, seems like [] is neither SQL<unknown> or Date
Angelelz
Angelelz12mo ago
It's definitely not. But it's the same bug.
jakeleventhal
jakeleventhalOP12mo ago
for me, the interface is SQL<unknown> | string[] (why it doesnt throw an error for me)
Angelelz
Angelelz12mo ago
Yes, what I'm trying to say is that the interface is not correct. If you are using a Date or an array, drizzle is showing you the wrong interface for the .default() method
jakeleventhal
jakeleventhalOP12mo ago
oh, nvm, its based on the column type. understood
Want results from more Discord servers?
Add your server