Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing remote relationship after offline retry #267

Open
alexrothm opened this issue Apr 15, 2024 · 2 comments
Open

Missing remote relationship after offline retry #267

alexrothm opened this issue Apr 15, 2024 · 2 comments

Comments

@alexrothm
Copy link

Hi, when creating objects with relationships directly one after the other, or in offline mode, the remote server does not have the relationship after posting data via the retry offlineOperation.

I have two models. Crop and Cultivar. Crops can have multiple Cultivars and Cultivars may be assigned to a Crop.

@JsonSerializable()
@DataRepository([JsonServerAdapter])
class Crop extends DataModel<Crop> {
  @override
  final int? id;
  final String name;
  final HasMany<Cultivar>? cultivars;

  Crop({this.id, required this.name, HasMany<Cultivar>? cultivars})
      : cultivars = cultivars ?? HasMany();

  int getCultivarsCount() {
    return cultivars!.length > 0 ? cultivars!.length : 0;
  }
}
@JsonSerializable(fieldRename: FieldRename.snake)
@DataRepository([JsonServerAdapter])
class Cultivar extends DataModel<Cultivar> {
  @override
  final int? id;
  final String name;
  final String ripeningTime;
  late final BelongsTo<Crop> crop;
  final String cropName;

  Cultivar(
      {this.id,
      required this.name,
      required this.ripeningTime,
      BelongsTo<Crop>? crop,
      required this.cropName})
      : crop = crop ?? BelongsTo();
}

When I do the following only the local repository has the relationship saved. The remote one does not save any id for the corresponding crop:

OutlinedButton(
    onPressed: () {
      Crop newCrop = Crop(name: 'MyCrop');
      newCrop.save();
    
      Cultivar newCultivar = Cultivar(name: 'MyCultivar',
          ripeningTime: Cultivar.ripeningTimeEarly, crop: BelongsTo(newCrop), cropName: newCrop.name);
      newCultivar.save();
    }...),

When a change the function to async and add await before each save the relation is stored correctly.

But, when I shut down the remote server and the offline functionality retries both posts. The crop id is empty for the newCultivar.

Summary:
The desiged objects should look like this:

Crop: {"id":15,"name":"MyCrop"}
Cultivar: {"id":15, "name":"MyCultivar","ripening_time":"RIPENING_TIME_EARLY","crop":15,"crop_name":"MyCrop"}

but in offline mode or without using async + await I get this instead:

Crop: {"id":15,"name":"MyCrop"}
Cultivar: {"id":15, "name":"MyCultivar","ripening_time":"RIPENING_TIME_EARLY","crop":null,"crop_name":"MyCrop"}

I appreciate any help.

@frank06
Copy link
Contributor

frank06 commented Apr 24, 2024

Makes sense as retries will attempt to send the exact same original request. Changes after that are not captured. I'm not sure, maybe we should re-serialize the updated model

@frank06
Copy link
Contributor

frank06 commented Sep 19, 2024

Need to decide whether to re-serialize the updated model

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants