Skip to main content

Relationships

Ormed supports common relationship types between models using the @OrmRelation annotation.

Relationship Types

A one-to-one relationship where the related model has the foreign key.

(table: 'users')
class UserWithProfile extends Model<UserWithProfile> {
const UserWithProfile({required this.id, this.profile});

(isPrimaryKey: true)
final int id;

.hasOne(Profile, foreignKey: 'user_id')
final Profile? profile;
}

(table: 'profiles')
class Profile extends Model<Profile> {
const Profile({required this.id, required this.userId, required this.bio});

(isPrimaryKey: true)
final int id;

final int userId;
final String bio;
}

Loading Relations

Eager Loading

Load relations upfront with the query:

Future<void> basicEagerLoading(DataSource dataSource) async {
// Load a single relation
final posts = await dataSource.query<$Post>().with_(['author']).get();

for (final post in posts) {
print(post.author?.name); // Already loaded, no additional query
}
}

Lazy Loading

Load relations on-demand:

Future<void> lazyLoading(DataSource dataSource) async {
final post = await dataSource.query<$Post>().find(1);

if (post != null) {
// Load after fetching
await post.load(['author', 'tags']);

print(post.author?.name);
}
}

Checking Relation Status

Future<void> checkLoadedExample(DataSource dataSource) async {
final post = await dataSource.query<$Post>().find(1);

if (post != null) {
if (post.relationLoaded('author')) {
print(post.author?.name);
} else {
await post.load(['author']);
}
}
}

Relation Manipulation

Setting Relations

Future<void> associateExample(DataSource dataSource) async {
final postRepo = dataSource.repo<$Post>();
final post = await dataSource.query<$Post>().first();
final user = await dataSource.query<$User>().first();

if (post != null && user != null) {
// Set a belongs-to relationship
post.associate('author', user);
await postRepo.update(post);

// Remove a belongs-to relationship
post.dissociate('author');
await postRepo.update(post);
}
}

Many-to-Many Operations

Future<void> attachDetachExample(DataSource dataSource) async {
final post = await dataSource.query<$Post>().first();

if (post != null) {
// Attach related models to a many-to-many relationship
await post.attach('tags', [1, 2]);

// With pivot data
await post.attach('tags', [3], pivot: {'added_by': 1});

// Detach related models
await post.detach('tags', [1]);

// Detach all
await post.detach('tags');
}
}

Aggregate Loading

Load aggregate values without fetching all related models:

Future<void> countAggregateExample(DataSource dataSource) async {
final user = await dataSource.query<$User>().first();

if (user != null) {
await user.loadCount(['posts', 'comments']);
// Access via getAttribute
print('Posts: ${user.getAttribute<int>('posts_count')}');
print('Comments: ${user.getAttribute<int>('comments_count')}');
}
}

Preventing N+1 Queries

Use Model.preventLazyLoading() in development to catch N+1 issues:

void preventNPlusOneExample() {
// Enable lazy loading prevention in development
// void main() {
// if (kDebugMode) {
// Model.preventLazyLoading();
// }
// runApp(MyApp());
// }
//
// This throws an exception when you try to access a relation
// that hasn't been eager-loaded, helping catch N+1 issues.
}

This throws an exception when accessing relations that haven't been eager-loaded, helping you identify performance issues early.