DT
Join ServerDrizzle Team
help
I am confused on how the new relational queries works
In the docs I see the following
What does the
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
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 usedIs 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?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)
it basically just registers the relations within the query module
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?
the drizzle instance knows about it because you must include all relations alongside your schema
i split my schema from my relations like so
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,
},
})
relations dont modify tables or the database, it just tells the relational query module what your relations are
So I still need to add the mapping tables (e.g.
usersToGroups
) for many to many?pivot/join tables yeah
And specify unique constraints for one ot many?
constraints are separate to this
oh I see
the relational queries dont require constraints, meaning it works on planetscale
I didn't find anything in the docs about constraints either
I meant unique constraints
thats cause constraints are part of the database schema, not the relations
So unrelated question how can I specify a unique constraint on a single field within the schema?
the docs say unique isnt currently implemented https://orm.drizzle.team/docs/indexes-constraints#unique
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
actually not sure, there seems to be a few
unique
exports from core, maybe one of the devs can chime in on that onenp
yes, you can add it to the migration manually
won't change the types
This is mentioned on the Foreign keys section of the docs:
https://orm.drizzle.team/docs/rqb#foreign-keys
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