X
Xata6mo ago
Storm

Drizzle ORM schema conflict with xata_createdat and xata_updatedat

This is my drizzle schema:
export const users = pgTable('users', {
id: text('id').primaryKey(),
email: text('email').unique(),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at', {
mode: 'date',
precision: 3,
}).$onUpdate(() => new Date()),
})
export const users = pgTable('users', {
id: text('id').primaryKey(),
email: text('email').unique(),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at', {
mode: 'date',
precision: 3,
}).$onUpdate(() => new Date()),
})
When I push my schema using npx drizzle-kit push:pg I get a popup from Xata telling to add xata_createdat and xata_updatedat I do add them but then later when I change my drizzle schema (let's say I added a firstName column) and push it, Drizzle warns me that I'm going to drop xata_createdat and xata_updatedat What am I supposed to in this case? Thank you
6 Replies
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
kostas
kostas6mo ago
Hi, after pushing your initial schema (npx drizzle-kit push:pg), the Xata UI will prompt you to adapt your table. This adds extra metadata and internal triggers to the table, which enable it to work with added Xata's features which aren't available with "vanilla" Postgres, such as text search, file attachments etc. After you've adapted your table, you can run introspect (npx drizzle-kit introspect:pg) which will read the schema from Xata and write it to drizzle/schema.ts, including the extra columns and settings introduced by the "adaptation". You can copy those to your schema file. When you make changes, such as adding new fields to your schema, Drizzle will be aware of them and not try to drop them. As noted here: https://xata.io/docs/integrations/drizzle#limitations there is a temporary bug with the xata_id column. When copying it to your schema file you'll also need to wrap the default with backticks like this:
xataId: text("xata_id").default`(('rec_'::text || (xata_private.xid())::text))`.notNull(),
xataId: text("xata_id").default`(('rec_'::text || (xata_private.xid())::text))`.notNull(),
Drizzle push may show that it needs to alter it, it's a no-op and can be ignored.
Drizzle with Xata
Use the Drizzle integration as a TypeScript ORM
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
kostas
kostas6mo ago
Thanks for the reports! Indeed Drizzle push always adds an alter statement for xata_id (see my comment earlier about it: "Drizzle push may show that it needs to alter it, it's a no-op and can be ignored.") - we're not sure why yet but we'll look into it. As for adapt, indeed it is only possible to adapt "fully". If only some of the xata internal columns are missing or are altered, adapt doesn't fix that. Regarding the issue with creating data, perhaps you're hitting this: https://github.com/xataio/client-ts/issues/1427. In the presense of both "id" and "xata_id", you currently need to set both in order to create a record. We'll work on that.
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
kostas
kostas6mo ago
Yes, the issue should be resolved using the @next version of the xata client (install @xata.io/client@next). The issue was moved to an internal repo to make sure the Playground also uses that version.
Want results from more Discord servers?
Add your server