Relations
Relations are pure metadata that reference models by name as strings.
BelongsTo
Section titled “BelongsTo”relations: { role: r.belongsTo("role", { localKey: "roleId" }),}HasOne
Section titled “HasOne”relations: { profile: r.hasOne("profile"),}HasMany
Section titled “HasMany”relations: { posts: r.hasMany("post"),}Many-to-Many
Section titled “Many-to-Many”relations: { roles: r.manyToMany("role", { through: "user_roles", localForeignKey: "userId", foreignForeignKey: "roleId", }),}With extra columns in pivot table
Section titled “With extra columns in pivot table”relations: { permissions: r.manyToMany("permission", { through: "user_permissions", localForeignKey: "userId", foreignForeignKey: "permissionId", columns: { active: t.boolean().default(true), grantedAt: t.dateTime().autoCreate(), }, }),}Naming convention
Section titled “Naming convention”Omit options to use naming convention (user_role, userId, roleId):
relations: { roles: r.manyToMany("role"), // defaults computed at runtime}- Always use strings to reference models
- Never use cross imports
- Junction tables are created with
ON DELETE CASCADE - Relations are used in
selectas nested objects
