diff --git a/CHANGELOG.md b/CHANGELOG.md index 49268ac7..09e8a974 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ - Removed the `COALESCE` wrapping from some of the generated filter conditions to make better use of the indexes ([#1939](https://github.com/pocketbase/pocketbase/issues/1939)). +- Detect `id` aliased view columns as single `relation` fields ([#2029](https://github.com/pocketbase/pocketbase/discussions/2029)). + - Optimized single relation lookups. - Normalized record values on `maxSelect` field option change (`select`, `file`, `relation`). diff --git a/daos/view.go b/daos/view.go index 5e7bdbcf..0a409e27 100644 --- a/daos/view.go +++ b/daos/view.go @@ -279,16 +279,13 @@ func (dao *Dao) parseQueryToFields(selectQuery string) (map[string]*queryField, var fieldName string var collection *models.Collection - var isMainTableField bool if len(parts) == 2 { fieldName = parts[1] collection = collections[parts[0]] - isMainTableField = parts[0] == mainTable.alias } else { fieldName = parts[0] collection = collections[mainTable.alias] - isMainTableField = true } // fallback to the default field if the found column is not from a collection schema @@ -323,8 +320,8 @@ func (dao *Dao) parseQueryToFields(selectQuery string) (map[string]*queryField, continue } - if fieldName == schema.FieldNameId && !isMainTableField { - // convert to relation since it is a direct id reference to non-maintable collection + if fieldName == schema.FieldNameId { + // convert to relation since it is a direct id reference result[col.alias] = &queryField{ field: &schema.SchemaField{ Name: col.alias, diff --git a/daos/view_test.go b/daos/view_test.go index fecdbbb0..d6db879a 100644 --- a/daos/view_test.go +++ b/daos/view_test.go @@ -399,7 +399,7 @@ func TestCreateViewSchema(t *testing.T) { `, false, map[string]string{ - "id2": schema.FieldTypeJson, + "id2": schema.FieldTypeRelation, "text_alias": schema.FieldTypeText, "url_alias": schema.FieldTypeUrl, "bool_alias": schema.FieldTypeBool,