drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
23.63k stars 583 forks source link

[BUG]: Typescript doesn't recognize One-To-One Relation #1869

Open philparzer opened 8 months ago

philparzer commented 8 months ago

What version of drizzle-orm are you using?

^0.29.1

What version of drizzle-kit are you using?

^0.20.13

Describe the Bug

I'm encountering a TypeScript error when trying to access a related object property through Drizzle ORM relations. Although the runtime behavior is as expected and I receive the correct object in response, TypeScript reports an error indicating that the property does not exist.

Drizzle Schema of relevant tables and relation

Screenshot 2024-02-06 at 15 59 02

TRPC Procedure and Correct Console Output for relational Object

Screenshot 2024-02-06 at 15 56 04

Type Inspect

Screenshot 2024-02-06 at 15 56 47

This could also just be a skill-issue due to this being the first time using drizzle. But I read the docs extensively and think my relations and tables should be fine.

Thanks for your help.

Expected behavior

Typescript should recognize that the field "tag" does indeed exist on "sticker" and not throw a type error. Also, I don't get autocomplete when specifying the "with" relation.

Environment & setup

T3 Stack (App Router, Next 14), MySQL on Planetscale

LukaHietala commented 8 months ago

If you're using Planetscale as the database, you cannot use foreign key constraints like you're doing here. Instead, you should just use the emulated relations that are already there. @philparzer

export const tagStickersRelation = relations(stickers, ({ one }) => ({
    tag: one(tags, {
        fields: [stickers.tagId],
        references: [tags.id],
    }),
}));

Maybe this also fixes your issues with Typescript.

More info here: https://planetscale.com/blog/working-with-related-data-using-drizzle-and-planetscale

LukaHietala commented 8 months ago

I also tried your code with mysql2 and sqlite driver, but didn't get any issues with Typescript or what so ever. 😄

philparzer commented 8 months ago

@LukaHietala thanks for the reply. Strange that you didnt get any errors, what setup did you use?

Regarding FK constraints: Planetscale has recently added support for them Screenshot 2024-02-09 at 06 27 03

LukaHietala commented 8 months ago

Do you have any other relations like this in your schema that are working?

philparzer commented 8 months ago

Nah, TS breaks for all relational querys using "with" in the same way.

AndriiSherman commented 8 months ago

@philparzer, can you show the part of the code where you're creating the db object using the drizzle() function? Just to be sure that your setup is 100% correct and it's not a drizzle issue

philparzer commented 7 months ago

@AndriiSherman

Screenshot 2024-02-11 at 04 57 27
Angelelz commented 7 months ago

@philparzer Can you try adding the other side of the relation to your schema as well? Just to check if that solves the issue. Meaning:

export const tagRelations = relations(tags, ({ one }) => ({
  sticker: one(stickers, {
    fields: [tags.id],
    references: [stickers.tagId]
  }
});
philparzer commented 7 months ago

@Angelelz that suggestion didn't solve the problem.

jkbudde commented 2 months ago

I'm encountering the same issue using the pg-core driver. Getting TS errors with one-to-one relations for relational queries using "with," and the suggestions above also did not solve this issue for me.

drizzle-orm@^0.32.0
drizzle-kit@^0.23.0