SupabaseS
Supabase4y ago
floh

One To Many Relation, Dart Flutter, Database Swagger Codegen

Hey guys,
I am currently working on an project where i have a objectA which consists of many objectB´s. A basic one to many relationship.
Here is some pseudo sql code of the table:
create table objectA ( id )
create table objectB ( id, objectA_id references objectA id)

and i to create those types with the swagger codegen: https://pub.dev/packages/swagger_dart_code_generator (please do not mind syntax. the classes have the json serialization annotation correctly)
class ObjectA() {
  id,
  List<ObjectB> objectBs,
}
class ObjectB() {
  id
}

but the codegen is generating smth like this
class ObjectA() {
  id,
}
class ObjectB() {
  id,
  objectA_ID,
}

which means an postgrest call in dart like this wouldn´t work because objectA.fromJson never reads the value ObjectB:
final response = await _supabaseClient
          .from('objectA')
          .select(
            '''
            *,
            objectB (
              *
            )
            ''',
          )
          .eq('id', id)
          .single()
          .execute();
final data = response.data as Map<String, dynamic>;
return ObjectA.fromJson(data);


any reccomendations or best practices on how to solve this?
Dart packages
Have you been turned into a problem with writing code for Http requests? This package can help. It has all of the Http and model codegen functionality you have been looking for.
Was this page helpful?