How to add to the database where the table has a many relationship to another table

I have a users table, which has a many relationship to to a posts table. I want to add a new user to the table, which I can do, but how do I go about adding new posts to that user? Does it add a full post object to the user? Or just an id to the new post which will be in the post table? export const users = pgTable('users', { id: uuid('id').primaryKey().defaultRandom().unique().notNull(), username: varchar('username', { length: 512 }).unique().notNull(), }); export const usersRelations = relations(users, ({ many }) => ({ posts: many(posts), }));
2 Replies
Angelelz
Angelelz17mo ago
The canonical way of doing a one to many relation is by adding an userId foreign key to the posts table. If you need all the post by an user, you just select the post with that userId If you need the user that created a post, just select the user whose id = userId
cobite
cobiteOP17mo ago
Hey thanks. Yup that's how I do it, I guess i'm just overthinking how drizzle is used!
Want results from more Discord servers?
Add your server