Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/drizzle/src/queries/getTableColumnFromPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,12 @@ export const getTableColumnFromPath = ({
},
table: aliasRelationshipTable,
}
} else if (isPolymorphicRelationship(value)) {
const { relationTo } = value
} else if (
isPolymorphicRelationship(value) ||
(Array.isArray(value) && value.length === 1 && isPolymorphicRelationship(value[0]))
) {
const polymorphicVal = Array.isArray(value) ? value[0] : value
const { relationTo } = polymorphicVal

const relationTableName = adapter.tableNameMap.get(
toSnakeCase(adapter.payload.collections[relationTo].config.slug),
Expand All @@ -760,6 +764,16 @@ export const getTableColumnFromPath = ({
rawColumn: sql.raw(`"${aliasRelationshipTableName}"."${relationTableName}_id"`),
table: aliasRelationshipTable,
}
} else if (
newCollectionPath === '' &&
(value === true || value === false || value === 'true' || value === 'false')
) {
return {
columnName: 'parent',
constraints,
field,
table: aliasRelationshipTable,
}
} else if (value === DistinctSymbol) {
const obj: Record<string, SQL> = {}

Expand Down
14 changes: 9 additions & 5 deletions packages/drizzle/src/queries/sanitizeQueryValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ export const sanitizeQueryValue = ({
}

if (field.type === 'relationship' || field.type === 'upload') {
const polymorphicVal =
Array.isArray(val) && val.length === 1 && isPolymorphicRelationship(val[0]) ? val[0] : val

if (val === 'null') {
formattedValue = null
} else if (!(formattedValue === null || typeof formattedValue === 'boolean')) {
Expand All @@ -151,26 +154,27 @@ export const sanitizeQueryValue = ({
collection: adapter.payload.collections[field.relationTo],
})
} else {
if (isPolymorphicRelationship(val)) {
if (isPolymorphicRelationship(polymorphicVal)) {
if (operator !== 'equals') {
throw new APIError(
`Only 'equals' operator is supported for polymorphic relationship object notation. Given - ${operator}`,
)
}
idType = getCollectionIdType({
adapter,
collection: adapter.payload.collections[val.relationTo],
collection: adapter.payload.collections[polymorphicVal.relationTo],
})

if (isRawConstraint(val.value)) {
if (isRawConstraint(polymorphicVal.value)) {
return {
operator,
value: val.value.value,
value: polymorphicVal.value.value,
}
}
return {
operator,
value: idType === 'number' ? Number(val.value) : String(val.value),
value:
idType === 'number' ? Number(polymorphicVal.value) : String(polymorphicVal.value),
}
}

Expand Down
Loading