db.query error with relation
I have created a schema.ts, with two tables, with a one-to-one relationship. I have also create the "relation".
In my index file, I get an error with the following code
saying "Property 'matches' does not exist on type '{}'"
39 Replies
If you can show whole schema file and file where you creating drizzle() I can help
As far as i can tell, im following what is in the docs.
I tried using "import * as schema from './schema';" in my index.ts but it broke more of the code :/
ok, so I see, that you didn't specify all data for
one
relationas you can see you need to specify fields that will be connected for
one
relationOK, so there is no info on what "fields" or "references" should be :/
what fields from tableA should be connected to field in tableB
in the above example invitedBy is connected to id
how do i include all fields?
gives me an error now
Type 'MySqlVarChar<{ tableName: "_matchDetails"; enumValues: [string, ...string[]]; name: "matchID"; data: string; driverParam: string | number; hasDefault: false; notNull: true; }>' is not assignable to type 'AnyColumn<{ tableName: "matches"; }>'.
The types of '.config.tableName' are incompatible between these types.
Type '"_matchDetails"' is not assignable to type '"_matches"'.ts(2322)
this simply doesn't work, no matter what i put in the "fields" and"references" array
If i do
I get another error for "references"
Type 'MySqlVarChar<{ tableName: "_matches"; enumValues: [string, ...string[]]; name: "matchID"; data: string; driverParam: string | number; hasDefault: false; notNull: true; }>' is not assignable to type 'AnyColumn<{ tableName: "matchDetails"; }>'. The types of '.config.tableName' are incompatible between these types. Type '"_matches"' is not assignable to type '"_matchDetails"'.ts(2322)I even just tried the exact code from the docs, and i get the same error
(apologies for the questions, but i really need this to work, as it's imperative to our project, as all other ORMs have failed me so far)
OK, I think i had it the wrong way around, this now just gives the one error still though on the "fields"
So, i can get rid of the error in the relation, but using "fields: [matches.matchID]," but no idea if thats correct at all? However, I still have the original error in my index.ts, "Property 'matches' does not exist on type '{}'"
make sure you provide relations object to drizzle()
would suggest to do just import all, so you won't forget anything
and then if you want to get any table from that import
and use it in queries
ok, but i still have that error "Property 'matches' does not exist on type '{}'"
and no idea if my relations schema thing is correct 😦
even when you provided relations to drizzle()?
yea
see my index.ts file
in your index ts
you didn't do it
that's why I'm asking
i have "const db = drizzle(connection, { schema: { ...matches, ...matchesDetails } });"
:/
and you need to provide
matchesRelations
as well😩
I would suggest to use this
as it states in docs you need to provide both tables and relations
basically whole schema you have
but now i have an error on my migrate command
Argument of type 'MySql2Database<typeof import("/Users/paulcanning/Programming/Work/rds-test/db/schema")>' is not assignable to parameter of type 'MySql2Database'. The types of '_.schema' are incompatible between these types. Type 'ExtractTablesWithRelations<typeof import("/Users/paulcanning/Programming/Work/rds-test/db/schema")> | undefined' is not assignable to type 'ExtractTablesWithRelations<Record<string, never>> | undefined'. Type 'ExtractTablesWithRelations<typeof import("/Users/paulcanning/Programming/Work/rds-test/db/schema")>' is not assignable to type 'ExtractTablesWithRelations<Record<string, never>>'.ts(2345)
this one is known issue. Already have a PR for that: https://github.com/drizzle-team/drizzle-orm/pull/601
Let me check this PR now and push to beta, so it will unblock you
GitHub
Make migrators accept databases drizzle'd with schemas by mastondzn...
Fixes case where you specify your schemas when you use drizzle(), and migrate() wont accept the database type.
Screenshot of current behaviour:
thank you
Should be there soon
but without migrate, query should work now
appreciate your help on this. I was screwed if i couldnt get this working. Just hope I've done this relation schema correct!
does this look right for a one-to-one?
"matches" is the main table, "matchesDetails" is the 2nd table, which has a foreign key on it
should be like this
you need to connect 1 table to another
just changed to
matchesDetails
in referencesthat errors for me
Type 'MySqlVarChar<{ tableName: "_matchDetails"; name: "matchID"; data: string; driverParam: string | number; enumValues: [string, ...string[]]; notNull: false; hasDefault: false; }>' is not assignable to type 'AnyColumn<{ tableName: "matches"; }>'. The types of '.config.tableName' are incompatible between these types. Type '"_matchDetails"' is not assignable to type '"_matches"'.ts(2322)
I guess you need to make your schema design in a bit other way, let me send you it
👍
wouldn't surprise me if i got it wrong
hmm, i tried the code from the docs again
and this gives me that same kind of error on the relation :/
oh I see now
docs are wrong for exact this example
try this one
we will fix the docs and explain this part more
thanks a lot for your feedback and the issue you've found
same for you example
you just need to do
this one won't give any errors
But I still would suggest to make your schema something like this(by adding ids fields)
and relations
Yea, I originally wanted to use the matchesDetails primary key as the foreign key. I read that you can do that, but I might just use an extra field like you say
the matchID's are known IDs, as they come from another system, hence no auto increment either and being varchars 🙂
quick one, do you know how i stop these TS errors when I run
tsc
? I have excluded node_modules in my tsconfig but it doesnt do anything, i also tried skipLibCheck
BTW that relation now works great and brings back the data successfully, thank you!!
Great to hear!
I also merged a PR to
drizzle-orm@beta
with migrator fixes
you can install this tag and try it out🙏