3b00d
3b00d
DTDrizzle Team
Created by 3b00d on 8/8/2023 in #help
How to avoid Inserting invalid values into a table with relations with mysql?
Using planetscale so I cant reference other columns My schema:
export const mainTable = mysqlTable("main",{
id: serial("id").primaryKey().notNull(),
name: varchar("name", {length:32}).unique().notNull()
})

export const subTable = mysqlTable("sub",{
id: serial("id").primaryKey().notNull(),
name: varchar("name", {length: 32}).unique().notNull(),
mainName: varchar("main", {length:32}).notNull()
})

export const mainRelationsTable = relations(mainTable,({many})=>({
sub:many(subTable),
}))

export const subRelationsTable = relations(subTable,({one})=>({
main:one(mainTable,{
fields:[subTable.mainName],
references:[mainTable.name]
})
}))
export const mainTable = mysqlTable("main",{
id: serial("id").primaryKey().notNull(),
name: varchar("name", {length:32}).unique().notNull()
})

export const subTable = mysqlTable("sub",{
id: serial("id").primaryKey().notNull(),
name: varchar("name", {length: 32}).unique().notNull(),
mainName: varchar("main", {length:32}).notNull()
})

export const mainRelationsTable = relations(mainTable,({many})=>({
sub:many(subTable),
}))

export const subRelationsTable = relations(subTable,({one})=>({
main:one(mainTable,{
fields:[subTable.mainName],
references:[mainTable.name]
})
}))
this query runs without problems:
await dbClient.insert(subTable).values({name: "something", mainName:"value that doesnt exist in main table"})
await dbClient.insert(subTable).values({name: "something", mainName:"value that doesnt exist in main table"})
how do I avoid this? can I make the query automatically return error without manually checking if mainName exists in mainTable first?
1 replies
DTDrizzle Team
Created by 3b00d on 7/12/2023 in #help
supabase workflow
Hey I'm trying to set up a sveltekit app with supabase + drizzle and currently what I got going is a schema file in /src/lib/db and a
drizzle-kit generate:pg
drizzle-kit generate:pg
script in package.json. Currently I am generating migration whenever I change schema and then copying the migration generated into the SQL Editor in supabase's website to change my db. Is there a command for letting drizzle handle the pushing for me? I know
drizzle-kit push
drizzle-kit push
doesnt support postgres yet so that one is out the window.
1 replies