Skip to content

Commit 6b9847f

Browse files
committed
fix(drizzle): sort by join field on versioned collection with custom text id
When the list view is sorted by a `join` field on a collection that has both `versions` enabled and a custom text id (e.g. nanoid), the query crashes with: operator does not exist: integer = character varying `getTableColumnFromPath` builds the join condition using the versions table's `id` column. On a versions table, `id` is the versions row PK (always integer), not the parent document id. The joined `_rels` / target table column references the parent document id, which is `varchar` for custom text ids, so the types do not match. Use the versions table's `parent` column (mapped from the `parent_id` db column, which is the actual reference to the parent document id) instead of `id` whenever the source table is a versions table. Fixes both branches of `case 'join'` (single-segment `on` with `hasMany`, and the non-`hasMany` path). Made-with: Cursor
1 parent 0ceba02 commit 6b9847f

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

‎packages/drizzle/src/queries/getTableColumnFromPath.ts‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,14 @@ export const getTableColumnFromPath = ({
393393
}
394394

395395
if (!existingTable) {
396+
const rootIDColumn =
397+
adapter.versionsSuffix && rootTableName.endsWith(adapter.versionsSuffix)
398+
? adapter.tables[rootTableName].parent
399+
: adapter.tables[rootTableName].id
396400
addJoinTable({
397401
condition: and(
398402
eq(
399-
adapter.tables[rootTableName].id,
403+
rootIDColumn,
400404
aliasRelationshipTable[
401405
`${(relationshipField.field as RelationshipField).relationTo as string}ID`
402406
],
@@ -490,18 +494,20 @@ export const getTableColumnFromPath = ({
490494
? adapter.tableNameMap.get(`${newTableName}_${toSnakeCase(onSegments[0])}`)
491495
: undefined
492496

497+
const sourceIDColumn = aliasTable
498+
? aliasTable.id
499+
: adapter.versionsSuffix && tableName.endsWith(adapter.versionsSuffix)
500+
? adapter.tables[tableName].parent
501+
: adapter.tables[tableName].id
502+
493503
if (arrayTableName) {
494-
// join from main table to array table
495504
const { newAliasTable: arrayAliasTable } = getTableAlias({
496505
adapter,
497506
tableName: arrayTableName,
498507
})
499508

500509
joins.push({
501-
condition: eq(
502-
arrayAliasTable[onSegments.slice(1).join('_')],
503-
aliasTable ? aliasTable.id : adapter.tables[tableName].id,
504-
),
510+
condition: eq(arrayAliasTable[onSegments.slice(1).join('_')], sourceIDColumn),
505511
queryPath: `${constraintPath}${field.name}._array`,
506512
table: arrayAliasTable,
507513
})
@@ -516,10 +522,7 @@ export const getTableColumnFromPath = ({
516522
})
517523
} else {
518524
joins.push({
519-
condition: eq(
520-
newAliasTable[field.on.replaceAll('.', '_')],
521-
aliasTable ? aliasTable.id : adapter.tables[tableName].id,
522-
),
525+
condition: eq(newAliasTable[field.on.replaceAll('.', '_')], sourceIDColumn),
523526
queryPath: `${constraintPath}${field.name}`,
524527
table: newAliasTable,
525528
})

0 commit comments

Comments
 (0)