seed
I'm trying to create a seed script, but I haven't been able to do it and I get an err that I don't see any sense in, could you help me?
7 Replies
schema
I managed to solve it by sending the uuid, but I don't understand why it fails with the default
Guessing here (I'm not going to set this up to run it) but it looks like you're assigning the same id to every user at runtime: the
.default(crypto.randomUUID())
runs once, and sets a single UUID as the default
That's why you're seeing the "unique constraint failed" error
Hope that helps!But isn't the schema supposed to indicate the template for each insert? Why is the default executed only once? For example, the default in the role or createdAt must work for each instance that I create of my table or does it not work that way?
Yeah: the way you've written your code though doesn't work the way you think.
id: text("id", { length: 36 }).primaryKey().default(crypto.randomUUID())
is equivalent to id: text("id", { length: 36 }).primaryKey().default("ba70af14-4cbc-4cbb-97b4-21d5c5c31b5c")
. You're looking for something like id: text("id", { length: 36 }).primaryKey().$defaultFn(() => crypto.randomUUID())
mm i understand. so this is bad too
but it seems very strange to me, so what is default for?
thanks. It's working as expected now, but I'm a little confused.
for actual default values like in sql, like a config that should default to false if not provided