I try to solve the question in the title. PlanetScale does not support foreign keys yet.
This is my current schema:
export const users = mysqlTable("user", {
id: varchar("id", { length: 256 }).primaryKey(),
email: varchar("email", { length: 256 }).unique(),
username: varchar("username", { length: 256 }).unique(),
firstName: text("first_name"),
lastName: text("last_name"),
});
export const usersRelations = relations(users, ({ many }) => ({
followers: many(following, { relationName: "followers" }),
following: many(following, { relationName: "following" }),
}));
export const following = mysqlTable("usersFollowing", {
userId: varchar("user_id", { length: 255 }).primaryKey(),
followerId: varchar("follower_id", { length: 255 }).notNull(),
});
export const followingRelations = relations(following, ({ one }) => ({
userId: one(users, {
fields: [following.userId],
references: [users.id],
relationName: "user",
}),
followerId: one(users, {
fields: [following.followerId],
references: [users.id],
relationName: "follower",
}),
}));
But it says:
Error: There is not enough information to infer relation "users.followers"
Can anyone help?
I solved my problem, this is the code: