Skip to content

Embedded Model Relationship Not Being Stored #298

Description

@yadferhad

In 1.x, sending a parent record that contains fully embedded child objects (rather than just foreign-key IDs) automatically persisted those children, so BelongsTo.value / HasMany.value were immediately available.

In 2.x, the same payload only saves the parent; the relationships remain null (or empty) until I manually insert the children. Is this the intended behaviour, or is there a recommended way to restore automatic persistence of embedded resources?

When we query the restaurant model, the primaryCurrency is available, but when we then set the restaurant in a ValueNotifier, it loses the relationships

Code:

@JsonSerializable()
@DataAdapter([BaseAdapter, RestaurantAdapter])
class Restaurant extends DataModel<Restaurant> {
  final int? id;

  @JsonKey(name: 'primary_currency')
  final BelongsTo<Currency> primaryCurrency;

  @JsonKey(name: 'staffs')
  final HasMany<Staff> staffs;
}
class Currency extends DataModel<Currency> {
  @override
  final int? id;
  final String name;
  final String code;

  Currency({
    this.id,
    required this.name,
    required this.code,
  });
}

API Response:

{
  "id": 1,
  "name": "Some Kitchen",
  "primary_currency": { "id": 105, "name": "US Dollar", "code": "USD" },
  "staffs": [
    { "id": 19, "name": "Some Guy" },
    { "id": 5,  "name": "Test Staff" }
  ]
}

When queried:

Restaurant? c = await ref.restaurants.findOne(
                  activeRestaurant.value!.id!,
                  remote: false,
                );

 print(c?.primaryCurrency.value?.name); // US Dollar

When stored in a ValueNotifier:

print(activeRestaurant.value!.primaryCurrency.value); // null

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions