Skip to content

Mise Relationships

SteffenBlake edited this page Sep 28, 2026 · 1 revision

Relationships

[Relationship] links a mapped source column to a mapped target column. Mise uses it to generate SQL join fragments containing the target table and an ON predicate.

Declare a relationship

The course mapping in Table mappings puts this attribute on CampusId:

[Column("campus_id")]
[Relationship(CampusTbl.CampusIdCol)]
private static int? CampusId { get; }

CampusTbl is defined in Aliases. Both tables use the PostgreSQL engine. The target reference must identify a mapped column on a table using the same engine.

The generated CourseTbl.CampusIdJoin contains a fragment equivalent to:

"campuses" ON "courses"."campus_id" = "campuses"."campus_id"

It does not include INNER JOIN or LEFT JOIN; choose that operation in the query builder.

Join through aliases

var query = new PostgreSqlQueryBuilder()
    .Select($"{CourseTbl.Course.CourseIdCol:raw}")
    .Select($"{CampusTbl.Campus.NameCol:raw} AS campus_name")
    .From($"{CourseTbl.Course.Table:raw}")
    .LeftJoin($"{CourseTbl.Course.CampusIdJoinCampus:raw}");

The source alias Course determines the left side of the predicate. The Campus suffix selects the target alias. A left join retains courses without a campus and yields SQL NULL for their campus name.

Generated names

Reference Fragment
CourseTbl.CampusIdJoin Unaliased courses to unaliased campuses
CourseTbl.CampusIdJoinCampus Unaliased courses to the Campus alias
CourseTbl.Course.CampusIdJoinCampus The Course alias to the Campus alias
CourseTbl.CampusIdJoinReverseCampus Join unaliased courses from an existing Campus alias

Reverse fragments live on the table that declares the relationship. For example, start from CampusTbl.Campus.Table, then join CourseTbl.CampusIdJoinReverseCampus to bring in its courses. Reverse fragments are generated for target aliases.

Relationships describe join conditions. They do not create foreign keys, load child objects, or decide the cardinality of a query. Multiple matching children can produce multiple SQL rows.

For a condition involving extra predicates, append them to the generated fragment with ordinary parameter interpolation. See Joins.

Clone this wiki locally