Default values not working for insert
what is going on here? shouldn't the value be filled in by default?
10 Replies
To my understanding, you can't use default like that. I'm surprised it's not screaming at you with an error in the schema declaration.
The
default
method receives a SQL function, that the database is supposed to execute, not the ORM.
Meaning, It receives SQL in the form of this code gives me the same error, as does just a plain string
if you want to generate unique id for your id column. You can use
uuid()
instead of text()
something like this
yeah, but i am trying to piecemeal migrate a prisma schema, and it will break the prisma side of things if i switch to UUID so for now i need to have text. i dont understand why my approach in original post wouldnt work? how else are defaults supposed to work for text fields
i think this is a bug. at the very least my schema ought to have a type error https://github.com/drizzle-team/drizzle-orm/issues/1035
GitHub
[BUG]: Default text field values do not work · Issue #1035 · drizzl...
What version of drizzle-orm are you using? 0.28.2 What version of drizzle-kit are you using? 0.19.12 Describe the Bug await db .insert(Drizzle.User) .values({ email, password: Utils.getHash(passwor...
According to the doc, a default has to be a constant like
sql
'test'::string``.
https://orm.drizzle.team/docs/indexes-constraints#defaultIndexes & Constraints – DrizzleORM
Drizzle ORM | %s
The default function is only used by Drizzle-kit during the migration. see https://github.com/drizzle-team/drizzle-orm/issues/304
GitHub
[Feature Request] Pass a function to
default()
· Issue #304 · dri...What version of drizzle-orm are you using? 0.22.0 Describe the Feature Request I'm trying to port something from Prisma that uses a cuid type for the identifier column. Because the underlying d...
I think I’m running into the same issue.
id
is set to uuid.defaultRandom.primaryKey
Is there some special “default” syntax for insert operations?You can't currently pass a JS function to
default()
only a sql function, and you have to run a migration in order for it to pick up.So, I fixed it by deleting the table and running
push:pg
again. Didn't even change a line of code in the schema.ts