[#2029] enable any id aliased column in a view query to be detected as relation field

This commit is contained in:
Gani Georgiev 2023-03-23 19:51:06 +02:00
parent 67ecebe935
commit 7a2360d785
3 changed files with 5 additions and 6 deletions

View File

@ -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)). - 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. - Optimized single relation lookups.
- Normalized record values on `maxSelect` field option change (`select`, `file`, `relation`). - Normalized record values on `maxSelect` field option change (`select`, `file`, `relation`).

View File

@ -279,16 +279,13 @@ func (dao *Dao) parseQueryToFields(selectQuery string) (map[string]*queryField,
var fieldName string var fieldName string
var collection *models.Collection var collection *models.Collection
var isMainTableField bool
if len(parts) == 2 { if len(parts) == 2 {
fieldName = parts[1] fieldName = parts[1]
collection = collections[parts[0]] collection = collections[parts[0]]
isMainTableField = parts[0] == mainTable.alias
} else { } else {
fieldName = parts[0] fieldName = parts[0]
collection = collections[mainTable.alias] collection = collections[mainTable.alias]
isMainTableField = true
} }
// fallback to the default field if the found column is not from a collection schema // 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 continue
} }
if fieldName == schema.FieldNameId && !isMainTableField { if fieldName == schema.FieldNameId {
// convert to relation since it is a direct id reference to non-maintable collection // convert to relation since it is a direct id reference
result[col.alias] = &queryField{ result[col.alias] = &queryField{
field: &schema.SchemaField{ field: &schema.SchemaField{
Name: col.alias, Name: col.alias,

View File

@ -399,7 +399,7 @@ func TestCreateViewSchema(t *testing.T) {
`, `,
false, false,
map[string]string{ map[string]string{
"id2": schema.FieldTypeJson, "id2": schema.FieldTypeRelation,
"text_alias": schema.FieldTypeText, "text_alias": schema.FieldTypeText,
"url_alias": schema.FieldTypeUrl, "url_alias": schema.FieldTypeUrl,
"bool_alias": schema.FieldTypeBool, "bool_alias": schema.FieldTypeBool,