drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: There is not enough information to infer relation // SQLite one to one

Open anthonyivol opened this issue 11 months ago • 5 comments

What version of drizzle-orm are you using?

0.30.2

What version of drizzle-kit are you using?

0.20.14

Describe the Bug

I am trying to implement a one to one relation based on this example : https://orm.drizzle.team/docs/rqb#one-to-one, but with SQLite. I get an error while querying and launching drizzle-kit studio.

This is my schema :

import { text, integer, sqliteTable } from "drizzle-orm/sqlite-core";
import { relations } from 'drizzle-orm';

export const users = sqliteTable("users", {
  id: integer("id", { mode: 'number' }).primaryKey({ autoIncrement: true }),
  name: text('name'),
});

export const usersRelations = relations(users, ({ one }) => ({
  profileInfo: one(profileInfo),
}));

export const profileInfo = sqliteTable('profile_info', {
  id: integer("id", { mode: 'number' }).primaryKey({ autoIncrement: true }),
  userId: integer('user_id').references(() => users.id),
  metadata: text('metadata'),
});

This is the error while launching studio: Error: There is not enough information to infer relation "__public__.users.profileInfo"

If I comment the usersRelations function i can successfully launch drizzle studio.

If I manually create one user record and one profileInfo related to that user : image image

And query this from an API endpoint like so :

let users = await db.query.users.findMany({
    with : {
      profileInfo: true
    }
  })

I also get an error : Error: There is not enough information to infer relation "users.profileInfo"

By adding a onDelete cascade to the reference like this :

export const profileInfo = sqliteTable('profile_info', {
  id: integer("id", { mode: 'number' }).primaryKey({ autoIncrement: true }),
  userId: integer('user_id').references(() => users.id, { onDelete: 'cascade' }),
  metadata: text('metadata'),
});

And deleting the user record the related profileInfo is also deleted, so I guess the relation exists.

I am not very experimented with database so I may missed something, but I feel like I've followed the example and it is not working.

Expected behavior

The query return the User with profileInfo and drizzle studio launch successfully.

Environment & setup

MacOS 13.5 Node v21.4.0

anthonyivol avatar Mar 17 '24 17:03 anthonyivol