MontaderX
MontaderX
DTDrizzle Team
Created by MontaderX on 7/10/2024 in #help
there is no unique constraint matching given keys for referenced table "users"
i get this error when run migration
PostgresError: there is no unique constraint matching given keys for referenced table "users"
code: "42830"
PostgresError: there is no unique constraint matching given keys for referenced table "users"
code: "42830"
schema
import { pgTable, text, timestamp } from "drizzle-orm/pg-core";
import { cuid2 } from "drizzle-cuid2/postgres";
import { relations } from "drizzle-orm";

export const users = pgTable("users", {
id: cuid2("id").unique().defaultRandom().primaryKey(),
userId: text("user_id").unique().notNull(),
name: text("name").notNull(),
email: text("email").notNull(),

createdAt: timestamp("created_at", { mode: "date" }).defaultNow(),
updatedAt: timestamp("updated_at", { mode: "date", precision: 3 }).$onUpdate(
() => new Date()
),
});

export const notes = pgTable("notes", {
id: cuid2("id").defaultRandom().primaryKey(),
userId: text("user_id")
.references(() => users.userId, {
onDelete: "cascade",
})
.unique()
.notNull(),
title: text("title").notNull(),
content: text("content"),
color: text("color").default("bg-gray-200"),

createdAt: timestamp("created_at", { mode: "date" }).defaultNow(),
updatedAt: timestamp("updated_at", { mode: "date", precision: 3 }).$onUpdate(
() => new Date()
),
});

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

export const notesRelations = relations(notes, ({ one }) => ({
user: one(users, {
fields: [notes.userId],
references: [users.userId],
}),
}));
import { pgTable, text, timestamp } from "drizzle-orm/pg-core";
import { cuid2 } from "drizzle-cuid2/postgres";
import { relations } from "drizzle-orm";

export const users = pgTable("users", {
id: cuid2("id").unique().defaultRandom().primaryKey(),
userId: text("user_id").unique().notNull(),
name: text("name").notNull(),
email: text("email").notNull(),

createdAt: timestamp("created_at", { mode: "date" }).defaultNow(),
updatedAt: timestamp("updated_at", { mode: "date", precision: 3 }).$onUpdate(
() => new Date()
),
});

export const notes = pgTable("notes", {
id: cuid2("id").defaultRandom().primaryKey(),
userId: text("user_id")
.references(() => users.userId, {
onDelete: "cascade",
})
.unique()
.notNull(),
title: text("title").notNull(),
content: text("content"),
color: text("color").default("bg-gray-200"),

createdAt: timestamp("created_at", { mode: "date" }).defaultNow(),
updatedAt: timestamp("updated_at", { mode: "date", precision: 3 }).$onUpdate(
() => new Date()
),
});

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

export const notesRelations = relations(notes, ({ one }) => ({
user: one(users, {
fields: [notes.userId],
references: [users.userId],
}),
}));
3 replies