errorsmyown
errorsmyown
DTDrizzle Team
Created by errorsmyown on 11/21/2023 in #help
mysql2 many-to-many
Ah my misunderstanding! Thank you 🙏
7 replies
DTDrizzle Team
Created by errorsmyown on 11/21/2023 in #help
mysql2 many-to-many
Thanks @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;'.
import {
mysqlTable,
serial,
text,
int,
primaryKey,
} from "drizzle-orm/mysql-core";
import { relations } from "drizzle-orm";

export const users = mysqlTable("users", {
id: serial("id").primaryKey(),
name: text("name"),
});

export const usersRelations = relations(users, ({ many }) => ({
usersToGroups: many(usersToGroups),
}));

export const groups = mysqlTable("groups", {
id: serial("id").primaryKey(),
name: text("name"),
});

export const groupsRelations = relations(groups, ({ many }) => ({
usersToGroups: many(usersToGroups),
}));

export const usersToGroups = mysqlTable(
"users_to_groups",
{
userId: int("user_id")
.notNull()
.references(() => users.id),
groupId: int("group_id")
.notNull()
.references(() => groups.id),
},
(t) => ({
pk: primaryKey(t.userId, t.groupId),
})
);

export const usersToGroupsRelations = relations(usersToGroups, ({ one }) => ({
group: one(groups, {
fields: [usersToGroups.groupId],
references: [groups.id],
}),
user: one(users, {
fields: [usersToGroups.userId],
references: [users.id],
}),
}));
import {
mysqlTable,
serial,
text,
int,
primaryKey,
} from "drizzle-orm/mysql-core";
import { relations } from "drizzle-orm";

export const users = mysqlTable("users", {
id: serial("id").primaryKey(),
name: text("name"),
});

export const usersRelations = relations(users, ({ many }) => ({
usersToGroups: many(usersToGroups),
}));

export const groups = mysqlTable("groups", {
id: serial("id").primaryKey(),
name: text("name"),
});

export const groupsRelations = relations(groups, ({ many }) => ({
usersToGroups: many(usersToGroups),
}));

export const usersToGroups = mysqlTable(
"users_to_groups",
{
userId: int("user_id")
.notNull()
.references(() => users.id),
groupId: int("group_id")
.notNull()
.references(() => groups.id),
},
(t) => ({
pk: primaryKey(t.userId, t.groupId),
})
);

export const usersToGroupsRelations = relations(usersToGroups, ({ one }) => ({
group: one(groups, {
fields: [usersToGroups.groupId],
references: [groups.id],
}),
user: one(users, {
fields: [usersToGroups.userId],
references: [users.id],
}),
}));
Using mysql locally at the moment. Is this something to do with .serial ?
7 replies