Relations using Drizzle and Planetscale

I'm trying to start using Drizzle with Planetscale for my new project, but I have some doubts regarding relations...

I know that Planetscale doesn't allow Foreign key constraints on the DB, meaning you need an application level constraint. For example, from the starter template for T3

export const posts = createTable(
  "post",
  {
    id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
    createdById: varchar("createdById", { length: 255 }).notNull(),
    ...
  },
  (example) => ({
    createdByIdIdx: index("createdById_idx").on(example.createdById),
  }),
);

export const users = createTable("user", {
  id: varchar("id", { length: 255 }).notNull().primaryKey(),
  name: varchar("name", { length: 255 }),
  ...
});

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


As we can see, the Posts has the createdById attribute, but it's not connected to the User table in any way.

My question here are:

  1. Why is there no many() relation to posts like accounts and sessions?
  2. Since we do not have DB level restrictions, we trust that we will not make mistakes in the app and add wrong user ids to posts?
  3. Is it even possible to add wrong author ids to posts?
  4. What to do in case or to prevent 3?
All help is appreciated πŸ˜„
Was this page helpful?