Migration issue with PostgresSQL

I'm trying to migrate
export const postLikeTable = pgTable(
"post_like",
{
postId: text("post_id")
.references(() => postTable.id, { onDelete: "cascade" })
.notNull(),
userId: text("user_id")
.references(() => userTable.id, { onDelete: "cascade" })
.notNull(),
},
(self) => ({
pk: primaryKey({ columns: [self.postId, self.userId] }),
})
);
export const postLikeTable = pgTable(
"post_like",
{
postId: text("post_id")
.references(() => postTable.id, { onDelete: "cascade" })
.notNull(),
userId: text("user_id")
.references(() => userTable.id, { onDelete: "cascade" })
.notNull(),
},
(self) => ({
pk: primaryKey({ columns: [self.postId, self.userId] }),
})
);
But it says PostgresError: foreign key constraint "post_like_post_id_post_id_fk" cannot be implemented code: "42804"
1 Reply
Samir
SamirOP13mo ago
My post and user table looks like
export const postTable = pgTable("post", {
id: uuid("id").defaultRandom().primaryKey(),

title: varchar("title", { length: 50 }).unique().notNull(),
content: varchar("content", { length: 1000 }).notNull(),
price: doublePrecision("price").default(0),

createdAt: timestamp("created_at", {
precision: 2,
withTimezone: true,
}).defaultNow(),

userId: text("user_id")
.references(() => userTable.id, { onDelete: "cascade" })
.notNull(),
});
export const postTable = pgTable("post", {
id: uuid("id").defaultRandom().primaryKey(),

title: varchar("title", { length: 50 }).unique().notNull(),
content: varchar("content", { length: 1000 }).notNull(),
price: doublePrecision("price").default(0),

createdAt: timestamp("created_at", {
precision: 2,
withTimezone: true,
}).defaultNow(),

userId: text("user_id")
.references(() => userTable.id, { onDelete: "cascade" })
.notNull(),
});
export const userTable = pgTable("user", {
id: text("id").primaryKey(),
username: varchar("username", { length: 20 }).notNull().unique(),
role: userRoleEnum("role"),
cash: doublePrecision("cash").default(0),
});
export const userTable = pgTable("user", {
id: text("id").primaryKey(),
username: varchar("username", { length: 20 }).notNull().unique(),
role: userRoleEnum("role"),
cash: doublePrecision("cash").default(0),
});
Found the issue. It was happening because of incompatible types: text and uuid
Want results from more Discord servers?
Add your server