I am confused on how the new relational queries works

In the docs I see the following
import { pgTable, serial, text, integer, boolean } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';

export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name'),
invitedBy: integer('invited_by'),
});

export const usersRelations = relations(users, ({ one, many }) => ({
invitee: one(users, {
fields: [users.invitedBy],
references: [users.id],
}),
}));
import { pgTable, serial, text, integer, boolean } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';

export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name'),
invitedBy: integer('invited_by'),
});

export const usersRelations = relations(users, ({ one, many }) => ({
invitee: one(users, {
fields: [users.invitedBy],
references: [users.id],
}),
}));
What does the usersRelations do? in the actual query it is not used Is it just to add the tables without specifying unique constraints on the id fields? does it add additional fields to the actual table? and is this required to use the with keyword in a query?
18 Replies
hachoter
hachoterOP2y ago
If I manage to understand it I will try to make time to push a pr to clarify the docs (unless it's clear and I am just being stupid)
Kairu
Kairu2y ago
it basically just registers the relations within the query module
hachoter
hachoterOP2y ago
But how does the drizzle instance know about it? if it's not used in the query I am assuming it's only used to modify the tables, so my question is in the above example what ends up happening in the database?
Kairu
Kairu2y ago
the drizzle instance knows about it because you must include all relations alongside your schema i split my schema from my relations like so
import * as relations from "./relations"
import * as schema from "./schema"
export const db = drizzle(connection, {
schema: {
...schema,
...relations,
},
})
import * as relations from "./relations"
import * as schema from "./schema"
export const db = drizzle(connection, {
schema: {
...schema,
...relations,
},
})
relations dont modify tables or the database, it just tells the relational query module what your relations are
hachoter
hachoterOP2y ago
So I still need to add the mapping tables (e.g. usersToGroups ) for many to many?
Kairu
Kairu2y ago
pivot/join tables yeah
hachoter
hachoterOP2y ago
And specify unique constraints for one ot many?
Kairu
Kairu2y ago
constraints are separate to this
hachoter
hachoterOP2y ago
oh I see
Kairu
Kairu2y ago
the relational queries dont require constraints, meaning it works on planetscale
hachoter
hachoterOP2y ago
I didn't find anything in the docs about constraints either I meant unique constraints
Kairu
Kairu2y ago
thats cause constraints are part of the database schema, not the relations
hachoter
hachoterOP2y ago
So unrelated question how can I specify a unique constraint on a single field within the schema?
Kairu
Kairu2y ago
the docs say unique isnt currently implemented https://orm.drizzle.team/docs/indexes-constraints#unique
hachoter
hachoterOP2y ago
Oh bummer, seems like a basic feature, I guess as long as it doesn't change my types I can always modify the migrations Thanks for your help
Kairu
Kairu2y ago
actually not sure, there seems to be a few unique exports from core, maybe one of the devs can chime in on that one np
Dan
Dan2y ago
yes, you can add it to the migration manually won't change the types
runonce
runonce2y ago
This is mentioned on the Foreign keys section of the docs:
relations are a higher level abstraction, they are used to define relations between tables on the application level only. They do not affect the database schema in any way and do not create foreign keys implicitly.
https://orm.drizzle.team/docs/rqb#foreign-keys
Want results from more Discord servers?
Add your server