Open philparzer opened 9 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
.references(() => tags.id)
is a foreign key constraintexport 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
I also tried your code with mysql2
and sqlite
driver, but didn't get any issues with Typescript or what so ever. 😄
@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
Do you have any other relations like this in your schema that are working?
Nah, TS breaks for all relational querys using "with" in the same way.
@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
@AndriiSherman
@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]
}
});
@Angelelz that suggestion didn't solve the problem.
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
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
TRPC Procedure and Correct Console Output for relational Object
Type Inspect
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