Cannot read properties of undefined (reading 'referencedTable') for many-to-many

Hi Drizzle folks, I'm new to drizzle and want to create a many-to-many relationship to model user and teams. It more or less the same as the many-to-many example in the documenation: https://orm.drizzle.team/docs/relations#many-to-many . Only difference is that I'm using UUIDs as primary key for both user and team . I've organized my schema into different files, created an index.ts barel file and initialized Drizzle

export { default as user, usersRelations } from "./user";
export { default as team, teamsRelations } from "./team";
export { default as usersToTeams, usersToTeamsRelations } from "./usersToTeams";

export { default as user, usersRelations } from "./user";
export { default as team, teamsRelations } from "./team";
export { default as usersToTeams, usersToTeamsRelations } from "./usersToTeams";
Init of the drizzle config looks like this:
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import env from "../env";
import * as schema from "./schema";

export const connection = postgres(env.DATABASE_URL, {
max: env.DB_MIGRATING || env.DB_SEEDING ? 1 : undefined,
onnotice: env.DB_SEEDING ? () => {} : undefined,
});

export const db = drizzle(connection, {
schema,
logger: true,
});

export type db = typeof db;

export default db;
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import env from "../env";
import * as schema from "./schema";

export const connection = postgres(env.DATABASE_URL, {
max: env.DB_MIGRATING || env.DB_SEEDING ? 1 : undefined,
onnotice: env.DB_SEEDING ? () => {} : undefined,
});

export const db = drizzle(connection, {
schema,
logger: true,
});

export type db = typeof db;

export default db;
When I now do th following query in drizzle studio:

db.query.teams.findMany({
with: {
usersToTeams: {
with: {
users: true
}
}
}
})

db.query.teams.findMany({
with: {
usersToTeams: {
with: {
users: true
}
}
}
})
I get the error:
Cannot read properties of undefined (reading 'referencedTable')
Cannot read properties of undefined (reading 'referencedTable')
However the following works:
db.query.teams.findMany()
db.query.teams.findMany()
And also Drizzle Studio looks fine as far as I can tell. Any idea what's going wrong here?
No description
13 Replies
Tenkes
Tenkesβ€’3w ago
Can you send files where you defined tables and relations? These files to be specific:
export { default as user, usersRelations } from "./user"; // <-
export { default as team, teamsRelations } from "./team"; // <-
export { default as usersToTeams, usersToTeamsRelations } from "./usersToTeams"; // <-
export { default as user, usersRelations } from "./user"; // <-
export { default as team, teamsRelations } from "./team"; // <-
export { default as usersToTeams, usersToTeamsRelations } from "./usersToTeams"; // <-
(ping me when you reply)
Andreas
AndreasOPβ€’3w ago
Hey @Tenkes , sure. In my first version of the post I already had the content in the post but couldn't send it due to length limitations.
Tenkes
Tenkesβ€’3w ago
db.query.teams.findMany({
with: {
usersToTeams: {
with: {
users: true // <- this should be user not users
}
}
}
})
db.query.teams.findMany({
with: {
usersToTeams: {
with: {
users: true // <- this should be user not users
}
}
}
})
ans then when you want to loop through them you would do:
user.usersToTeams.map(({ user }) => console.log(user.id))
user.usersToTeams.map(({ user }) => console.log(user.id))
Andreas
AndreasOPβ€’3w ago
Hey @Tenkes . Tried it but same error 😦
Tenkes
Tenkesβ€’3w ago
Interesting, everything looks good... I'm only skeptical about the way you're exporting from your barrel file and about the fact you're default-exporting your tables when you're already exporting them. Maybe remove export default from all your schema files and then your barrel file should look something like:
export { users, usersRelations } from "./user";
export { teams, teamsRelations } from "./team";
export { usersToTeams, usersToTeamsRelations } from "./usersToTeams";
export { users, usersRelations } from "./user";
export { teams, teamsRelations } from "./team";
export { usersToTeams, usersToTeamsRelations } from "./usersToTeams";
@Andreas Yeah that might be it. Or more precisely it might be because in your barrel file you're renaming default to user instead of users:
// πŸ‘‡
export { default as user, usersRelations } from "./user";
// πŸ‘‡
export { default as user, usersRelations } from "./user";
Same for teams. So either rename it or do what I said in previous message (I think it would be cleaner that way)
Andreas
AndreasOPβ€’3w ago
Ok, I changed my barrel file to this. But the error is still the same 😦
Andreas
AndreasOPβ€’3w ago
Any further recommendations how I could track down the issue (besided form debugging the drizzle code πŸ˜…)
Tenkes
Tenkesβ€’3w ago
Doesn't make sense lol, it should work. Try to run it in drizzle.run and see what you get
Drizzle Run
Drizzle Run
Tenkes
Tenkesβ€’3w ago
Or try to reset stuff, clear cache, generate and migrate db etc...
Andreas
AndreasOPβ€’3w ago
Nice, I'll try that. Let's see if I can find the issue
Andreas
AndreasOPβ€’3w ago
@Tenkes Ok, that's kind of strange. I moved the example (with slight differences) to drizzle.run and it is working as expected https://drizzle.run/tjfaup6m3q9z8iys5o2bgf2k. One of the differences is, that I'm using a single schema file instead of multiple files. I'll try this now on my local machine.
Andreas
AndreasOPβ€’3w ago
Ok, never mind. I think its a bug in Drizzle Studio :(. I created a simple testDB.ts with
/**
* Write your code here
* πŸ’‘Tip: you can use the `$` global variable to access goodies
*/

import { db } from ".";
import {} from "./schema";

const result = await db.query.team.findMany({
with: {
usersToTeams: {
with: {
user: true,
},
},
},
});
console.log(JSON.stringify(result));
/**
* Write your code here
* πŸ’‘Tip: you can use the `$` global variable to access goodies
*/

import { db } from ".";
import {} from "./schema";

const result = await db.query.team.findMany({
with: {
usersToTeams: {
with: {
user: true,
},
},
},
});
console.log(JSON.stringify(result));
And I got the expected result. This is kind of annoying and I lost 3 hours :(.
Tenkes
Tenkesβ€’3w ago
That really sucks, hope you at least figured it out

Did you find this page helpful?