brick icon indicating copy to clipboard operation
brick copied to clipboard

Function afterSave() is generated wrong

Open hortigado opened this issue 1 year ago • 0 comments

Hi i have it problem with the function afterSave with fields type List<Model>. The problem is that the List<Vaccine> field to be added as a child relationship when saving the parent document; Generating an error because this field does not exist in the database. For now I have to manually delete these fields from the afterSave function of the generated adapter

Field List added in model parent

  @Rest(ignore: true)
  @Sqlite(ignore: true)
  List<Vacccines> vaccines;

Adapter generated with it in the method afterSave

  Future<void> afterSave(instance, {required provider, repository}) async {
    if (instance.primaryKey != null) {
      final balanceadosOldColumns = await provider.rawQuery(
          'SELECT `f_Vaccine_brick_id` FROM `_brick_CerdasModelServer_balanceados` WHERE `l_CerdasModelServer_brick_id` = ?',
          [instance.primaryKey]);
      final balanceadosOldIds = balanceadosOldColumns
          .map((a) => a['f_Vaccine_brick_id']);
      final balanceadosNewIds =
          instance.balanceados.map((s) => s.primaryKey).whereType<int>();
      final balanceadosIdsToDelete =
          balanceadosOldIds.where((id) => !balanceadosNewIds.contains(id));

      await Future.wait<void>(balanceadosIdsToDelete.map((id) async {
        return await provider.rawExecute(
            'DELETE FROM `_brick_CerdasModelServer_balanceados` WHERE `l_CerdasModelServer_brick_id` = ? AND `f_Vaccine_brick_id` = ?',
            [instance.primaryKey, id]).catchError((e) => null);
      }));

      await Future.wait<int?>(instance.balanceados.map((s) async {
        final id = s.primaryKey ??
            await provider.upsert<Vaccine>(s,
                repository: repository);
        return await provider.rawInsert(
            'INSERT OR IGNORE INTO `_brick_CerdasModelServer_balanceados` (`l_CerdasModelServer_brick_id`, `f_Vaccine_brick_id`) VALUES (?, ?)',
            [instance.primaryKey, id]);
      }));
    }

hortigado avatar Mar 16 '24 17:03 hortigado