Skip to content

Query association with WherePhrase doesn't work with Supabase #648

@KiddoV

Description

@KiddoV

Issue with WherePhrase and association columns in Brick

Based on the test:

test('OR', () async {
const statement =
'SELECT DISTINCT `DemoModel`.* FROM `DemoModel` INNER JOIN `DemoModelAssoc` ON `DemoModel`.assoc_DemoModelAssoc_brick_id = `DemoModelAssoc`._brick_id WHERE (`DemoModelAssoc`.id = ? OR `DemoModelAssoc`.full_name = ?)';
final sqliteQuery = QuerySqlTransformer<DemoModel>(
modelDictionary: dictionary,
query: Query(
where: [
Where.exact(
'assoc',
WherePhrase([
const Or('id').isExactly(1),
const Or('name').isExactly('Guy'),
]),
),
],
),
);
expect(sqliteQuery.statement, statement);
await db.rawQuery(sqliteQuery.statement, sqliteQuery.values);
sqliteStatementExpectation(statement, [1, 'Guy']);
});

When I apply similar logic in my code, I get an error from Supabase:

Query(
  where: [
    Where.exact("group", Or("creatorId").isExactly(localId)),
    Where.exact("person", WherePhrase([
      const Or("id").isExactly(profileId),
      const Or("cloudId").isExactly(cloudId)
    ]), isRequired: false)
  ],
)

The error I receive:

I/flutter (5044): OfflineFirstRepository: WARNING: #hydrate supabase failure: PostgrestException(message: column _group_members_.cloud_id does not exist, code: 42703, details: Bad Request, hint: Perhaps you meant to reference the column "_group_members_.group_id".)

Observation:
Brick seems to interpret the columns inside the WherePhrase as belonging to the parent table, not the associated table. This causes invalid SQL when querying associations.

Work around

Query without WherePhrase in association query

Query(
      where: [
        Where.exact("group", Or("creatorId").isExactly(localId)),
        Where.exact("person", Or("id").isExactly(profileId)),
        Where.exact("person", Or("cloudId").isExactly(cloudId)),
      ],
    )

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