Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My Error Code "There is not enough information to infer relation "__public__.postTable.likes"" #396

Open
novinbooster opened this issue May 12, 2024 · 0 comments

Comments

@novinbooster
Copy link

`
import { relations } from "drizzle-orm"
import {
pgTable,
serial,
text,
timestamp,
unique,
varchar,
} from "drizzle-orm/pg-core"

const userTable = pgTable("users", {
id: text("id").primaryKey(),
name: text("name").notNull(),
email: text("email").unique().notNull(),
image: varchar("image").notNull(),
bio: text("bio").default("").notNull(),
website: varchar("website").default("").notNull(),
username: varchar("username").unique().notNull(),
})

const postTable = pgTable("posts", {
id: serial("id").primaryKey(),
content: text("content").notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
userId: text("user_id")
.notNull()
.references(() => userTable.id),
})

const likeTable = pgTable(
"likes",
{
userId: text("user_id")
.notNull()
.references(() => userTable.id),
postId: serial("post_id")
.notNull()
.references(() => postTable.id),
createdAt: timestamp("created_at").defaultNow().notNull(),
},
(table) => {
return {
unq: unique().on(table.userId, table.postId),
}
},
)

const usersRelations = relations(userTable, ({ many }) => ({
posts: many(postTable),
likes: many(likeTable),
}))

const postsRelations = relations(postTable, ({ one, many }) => ({
user: one(userTable, {
fields: [postTable.userId],
references: [userTable.id],
}),
likes: many(likeTable),
}))

type Post = typeof postTable.$inferSelect & {
user: User
}
type User = typeof userTable.$inferSelect & {
posts: Post[]
}

export { likeTable, postTable, postsRelations, userTable, usersRelations }
export type { Post, User }

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant