Back to Drizzle Orm

0.3.1

changelogs/drizzle-seed/0.3.1.md

0.45.21.1 KB
Original Source

Bug fixes

  • Combining a reference in a table schema (foreign key constraint) with a one-to-many relation for the same two tables defined in the constraint causes the seeder to duplicate these relations and enter an infinite loop.

Example:

ts
// schema.ts
import { integer, pgTable, text } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm/relations";

export const users = pgTable("users", {
  id: integer().primaryKey(),
  name: text(),
  email: text(),
});

export const posts = pgTable("posts", {
  id: integer().primaryKey(),
  content: text(),
  userId: integer().references(() => users.id),
});

export const postsRelation = relations(posts, ({ one }) => ({
  user: one(users, {
    fields: [posts.userId],
    references: [users.id],
  }),
}));

Now, seeding with the schema above will trigger a warning.

You are providing a one-to-many relation between the 'users' and 'posts' tables,
while the 'posts' table object already has foreign key constraint in the schema referencing 'users' table.
In this case, the foreign key constraint will be used.