Skip to content

Add support for custom relationships to ReferenceAttribute #117

Description

@austin-beer

Feature request

PostgREST supports defining custom relationships between tables.
See https://docs.postgrest.org/en/stable/references/api/resource_embedding.html#computed-relationships

The field name in the select queries and in the returned JSON is determined by the name of each function. The name may match the table name that's being returned, but it doesn't have to.

I have a situation where we have multiple different relationships between the same two tables. As a result, we have defined multiple computed relationship functions in PostgreSQL. Each function/relationships has a different field name. I need to provide this custom field name to the ReferenceAttribute for each embedded relationship field in the associated C# model so that the C# client uses the correct field name when querying for the embedded relationship and parsing the returned JSON.

ReferenceAttribute already provides a ColumnName property that could be used. However, that property is only used to determine the field name if the ForeignKey property is also set, which isn't applicable in this case.
See

if (!string.IsNullOrEmpty(refAttr.ForeignKey))
{
Columns.Add(UseInnerJoin
? $"{refAttr.ColumnName}:{refAttr.ForeignKey}!inner({string.Join(",", refAttr.Columns.ToArray())})"
: $"{refAttr.ColumnName}:{refAttr.ForeignKey}({string.Join(",", refAttr.Columns.ToArray())})");
}
else
{
Columns.Add(UseInnerJoin
? $"{refAttr.TableName}!inner({string.Join(",", refAttr.Columns.ToArray())})"
: $"{refAttr.TableName}({string.Join(",", refAttr.Columns.ToArray())})");
}

Describe the solution you'd like

A simple solution is to update ReferenceAttribute so that, if ColumnName is set but ForeignKey is not, then ColumnName is used instead of TableName.

System information

Version of Supabase.Postgrest: 4.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions