Is it possible to do circular dependencies between tables? #396
Answered
by
AndriiSherman
knight10-ux
asked this question in
Q&A
-
|
In mikro-orm I can import entity as type. How can I do this with drizzle-orm? import { index, integer, pgTable, serial, varchar } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
id: serial('id').primaryKey(),
fullName: varchar('full_name', { length: 256 }),
// FIXME: how can I do this??
authOtpId: integer('auth_otp_id').references(() => authOtps.id),
}, (users) => ({
nameIdx: index('name_idx').on(users.fullName),
}));
export const authOtps = pgTable('auth_otp', {
id: serial('id').primaryKey(),
phone: varchar('phone', { length: 256 }),
userId: integer('user_id').references(() => users.id),
}); |
Beta Was this translation helpful? Give feedback.
Answered by
AndriiSherman
Apr 7, 2023
Replies: 2 comments 4 replies
-
|
@mkieblesz Yes! export const users = pgTable(
"users",
{
id: serial("id").primaryKey(),
fullName: varchar("full_name", { length: 256 }),
authOtpId: integer("auth_otp_id")
},
(users) => ({
nameIdx: index("name_idx").on(users.fullName),
fk: foreignKey({
columns: [users.authOtpId],
foreignColumns: [authOtps.id],
}),
})
);
export const authOtps = pgTable("auth_otp", {
id: serial("id").primaryKey(),
phone: varchar("phone", { length: 256 }),
userId: integer("user_id").references((): AnyPgColumn => users.id),
}); |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
AndriiSherman
-
|
Here is what I came up with import {
pgTable,
serial,
integer,
foreignKey,
AnyPgColumn,
type PgTableExtraConfigValue,
} from "drizzle-orm/pg-core";
const users = pgTable(
"users",
{
id: serial("id").primaryKey(),
authOtp: integer("auth_otp_id"),
},
(columns): PgTableExtraConfigValue[] => [
foreignKey({
columns: [columns.authOtp],
foreignColumns: [authOtps.id],
name: "users_auth_otp_id_fk",
}).onDelete("set null"),
],
);
const authOtps = pgTable(
"auth_otps",
{
id: serial("id").primaryKey(),
user: integer("user_id"),
},
(columns): PgTableExtraConfigValue[] => [
foreignKey({
columns: [columns.user],
foreignColumns: [users.id],
name: "auth_otps_user_id_fk",
}).onDelete("set null"),
],
);The explicit |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mkieblesz Yes!