PostgresError: foreign key constraint "product_categories_category_id_fk" cannot be implemented
Hello Drizzle users,
I'm working on ecommerce store with Next.js, Drizzle ORM and Neon PostgreSQL. I was working on my schema and ran into this problem. Here's some relevant code:
When I try to migrate this schema I get following error:
I haven't really used that much of PostgreSQL so I don't know if this is possible, but when I'm working with MongoDB and mongoose I can just store IDs of rows from another table, and when I want to use data from that row I can just use
.populate()
yeah? But that seems to be an issue here.9 Replies
Here's repo URL: https://github.com/boristenkes/shop.co
GitHub
GitHub - boristenkes/shop.co
Contribute to boristenkes/shop.co development by creating an account on GitHub.
I think you need to define the shape of the id too
I already did, just didn't include it here:
const id = uuid('id').primaryKey().defaultRandom().notNull()
So my schema.ts looks something like:
I would say the issue is the
categories
is being defined as an array
of uuid
's with a relation on it. I'm not entirely that well versed in the Drizzle schema and am having relation issues myself however I would think you would need to go through the relations
API.Yep, that worked! Thanks a bunch, I didn't even know
relations
was a thing lolGreat to hear! I was a bit of a silly billy and had forgotten the otherside of the relation for my issue haha! In the future as well it might be a good idea to make use of https://drizzle.run 🙂
Drizzle Run
Drizzle Run
omg that's just amazing.. THANKS!
I can't add
{ onDelete: 'cascade' }
if I'm using relations?You should be able to add it within your
.references
That being said you should be wary of using cascade
, it may seem like the right thing to do but it can be dangerous in certain scenarios.
If at all possible you should wrap your delete's within a transactionwdym?