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

Issue upserting SupabaseModel with association #431

Closed
devj3ns opened this issue Aug 30, 2024 · 2 comments
Closed

Issue upserting SupabaseModel with association #431

devj3ns opened this issue Aug 30, 2024 · 2 comments

Comments

@devj3ns
Copy link
Contributor

devj3ns commented Aug 30, 2024

In my project, I have a model Project and a model Customer:

@ConnectOfflineFirstWithSupabase( supabaseConfig: SupabaseSerializable(tableName: 'projects'))
class Project extends OfflineFirstWithSupabaseModel {

  ...
  
  @Supabase(foreignKey: 'customer')
  @Sqlite(index: true)
  final Customer customer;
}
@ConnectOfflineFirstWithSupabase( supabaseConfig: SupabaseSerializable(tableName: 'customers'))
class Customer extends OfflineFirstWithSupabaseModel {

  ...
  
  @Sqlite(unique: true, index: true)
  @Supabase(unique: true)
  final UUID id;
}

When upserting a Project, the following exception is thrown:

type 'Project' is not a subtype of type 'Customer' of 'input' #0      CustomerAdapter.toSupabase (package:tragwerk_app/brick/adapters/customer_adapter.g.dart:344:52)
#1      SupabaseProvider._upsertByType (package:brick_supabase/src/supabase_provider.dart:107:34)
#2      SupabaseProvider._recursiveAssociationUpsert (package:brick_supabase/src/supabase_provider.dart:149:18)
#3      SupabaseProvider._recursiveAssociationUpsert (package:brick_supabase/src/supabase_provider.dart:142:13)
#4      SupabaseProvider.upsert (package:brick_supabase/src/supabase_provider.dart:90:18)
#5      OfflineFirstRepository.upsert (package:brick_offline_first/src/offline_first_repository.dart:404:28)
<asynchronous suspension>

I checked the debugger and the issue is inside the _recursiveAssociationUpsert method.

When it is first called from the upsert method, the instance is the project data and the type is Project, which is correct. But when the associations are iterated over in the for loop, the _recursiveAssociationUpsert is called again, but this time with the type of Customer which is also correct, but the instance is the project data instead of the nested customer data. This means that the _upsertByType method is called with the project data but the Customer type. For that reason, the exception is thrown inside the toSupabase method, as the project data is passed instead of the customer data.

I guess the fix should be to pass the associated field of the model instead of the original model here:

@tshedor
Copy link
Collaborator

tshedor commented Aug 30, 2024

Great investigation work. Unfortunately, dynamic properties can't be invoked in strong Dart land, so I think the best way to handle this is to convert everything to a map first and then process it. I've made some changes and published the fixes for brick_supabase_generators and brick_supabase in #430

@tshedor tshedor mentioned this issue Aug 30, 2024
29 tasks
@devj3ns
Copy link
Contributor Author

devj3ns commented Sep 2, 2024

Great fix, upserting the Project now works :)

@devj3ns devj3ns closed this as completed Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants