Skip to content

Instantly share code, notes, and snippets.

@stolinski
Created January 4, 2024 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stolinski/48d74aaae59895c9001128a69448eacc to your computer and use it in GitHub Desktop.
Save stolinski/48d74aaae59895c9001128a69448eacc to your computer and use it in GitHub Desktop.
Error: There is not enough information to infer relation "__public__.habits.checks" at normalizeRelation
// Schema
import { relations } from 'drizzle-orm';
import { serial, text, timestamp, pgTable, date, integer } from 'drizzle-orm/pg-core';
export const habits = pgTable('habits', {
id: serial('id').primaryKey(),
name: text('name'),
days_per_month: integer('days_per_month'),
created_at: timestamp('created_at'),
updated_at: timestamp('updated_at')
});
export const habitRelations = relations(habits, ({ many }) => ({
checks: many(habits)
}));
export const checks = pgTable('checks', {
id: serial('id').primaryKey(),
checked_at: date('date'),
habit_id: integer('habit_id')
});
export const checkRelations = relations(checks, ({ one }) => ({
habit: one(habits, {
fields: [checks.habit_id],
references: [habits.id]
})
}));
// Query
const data = await db.query.habits.findMany({
with: {
checks: true
}
});
// Following https://orm.drizzle.team/docs/rqb#one-to-many
@AryanJ-NYC
Copy link

In:

export const habitRelations = relations(habits, ({ many }) => ({ checks: many(habits)}));

shouldn't it be ({ checks: many(checks)})); ?

I wrote on Twitter but you deleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment