mysql2 many-to-many
Is there an example of a mysql many-to-many relationship?
I haven't been able to find one and I'm getting a foreign key constraint db error similar to this post https://planetscale.com/blog/working-with-related-data-using-drizzle-and-planetscale. I've created a join table with foreign keys and relations in line with the example here https://orm.drizzle.team/docs/rqb#many-to-many.
Many thanks!
Drizzle Queries - Drizzle ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
4 Replies
The example in the docs will serve you well
For planetscale you just need to delete the
.references
method on the columns. The rest will be the sameThanks @Angelelz for support!
I've tried a direct copy of the example but switching it from
pgTable
to mysqlTable
(see below) and bump into the same error when I try to use db push for drizzle kit 'ALTER TABLE
users_to_groups ADD CONSTRAINT
users_to_groups_user_id_users_id_fk FOREIGN KEY (
user_id) REFERENCES
users(
id) ON DELETE no action ON UPDATE no action;'
.
Using mysql locally at the moment. Is this something to do with .serial
?Like I said, for planetscale, all you need to do is delete the references method in the column definitions:
The
.references
method create a foreign key constraint that is not supported in planetscaleAh my misunderstanding! Thank you 🙏