I am confused on how the new relational queries works

Hhachoter5/21/2023
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],
    }),
}));

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?
Hhachoter5/21/2023
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)
KKairu5/21/2023
it basically just registers the relations within the query module
Hhachoter5/21/2023
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?
KKairu5/21/2023
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,
  },
})
KKairu5/21/2023
relations dont modify tables or the database, it just tells the relational query module what your relations are
Hhachoter5/21/2023
So I still need to add the mapping tables (e.g. usersToGroups ) for many to many?
KKairu5/21/2023
pivot/join tables yeah
Hhachoter5/21/2023
And specify unique constraints for one ot many?
KKairu5/21/2023
constraints are separate to this
Hhachoter5/21/2023
oh I see
KKairu5/21/2023
the relational queries dont require constraints, meaning it works on planetscale
Hhachoter5/21/2023
I didn't find anything in the docs about constraints either
Hhachoter5/21/2023
I meant unique constraints
KKairu5/21/2023
thats cause constraints are part of the database schema, not the relations
Hhachoter5/21/2023
So unrelated question how can I specify a unique constraint on a single field within the schema?
KKairu5/21/2023
the docs say unique isnt currently implemented https://orm.drizzle.team/docs/indexes-constraints#unique
Hhachoter5/21/2023
Oh bummer, seems like a basic feature, I guess as long as it doesn't change my types I can always modify the migrations
Hhachoter5/21/2023
Thanks for your help
KKairu5/21/2023
actually not sure, there seems to be a few unique exports from core, maybe one of the devs can chime in on that one
KKairu5/21/2023
np
Bbloberenober5/21/2023
yes, you can add it to the migration manually
Bbloberenober5/21/2023
won't change the types
RRuno5/22/2023
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