Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 3 additions & 2 deletions packages/drizzle/src/queries/sanitizeQueryValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,13 @@ export const sanitizeQueryValue = ({
}
}

// For hasMany relationship/upload fields, contains should use equals operator
// hasMany relationship/upload/select fields are stored as separate rows in a join table.
// The JOIN already gives us individual rows, so "contains" becomes an equality check on each row's value.
if (
'hasMany' in field &&
field.hasMany &&
operator === 'contains' &&
(field.type === 'relationship' || field.type === 'upload')
(field.type === 'relationship' || field.type === 'upload' || field.type === 'select')
Comment thread
AlessioGr marked this conversation as resolved.
) {
operator = 'equals'
}
Expand Down
11 changes: 11 additions & 0 deletions test/database/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,17 @@ export const getConfig: () => Partial<Config> = () => ({
},
],
},
{
slug: 'select-has-many',
fields: [
{
name: 'roles',
type: 'select',
hasMany: true,
options: ['user', 'admin', 'editor'],
},
],
},
],
globals: [
{
Expand Down
22 changes: 22 additions & 0 deletions test/database/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,28 @@ describe('database', () => {
})
})

it('should query hasMany select field with contains operator', async () => {
const { id } = await payload.create({
collection: 'select-has-many',
data: {
roles: ['admin'],
},
})

const result = await payload.find({
collection: 'select-has-many',
where: {
roles: {
contains: 'admin',
},
},
})

expect(result.docs.some((doc) => doc.id === id)).toBe(true)

await payload.delete({ collection: 'select-has-many', id })
})

describe('allow ID on create', () => {
beforeAll(() => {
payload.db.allowIDOnCreate = true
Expand Down
38 changes: 38 additions & 0 deletions test/database/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface Config {
aliases: Alias;
'blocks-docs': BlocksDoc;
'unique-fields': UniqueField;
'select-has-many': SelectHasMany;
'payload-kv': PayloadKv;
users: User;
'payload-locked-documents': PayloadLockedDocument;
Expand Down Expand Up @@ -119,6 +120,7 @@ export interface Config {
aliases: AliasesSelect<false> | AliasesSelect<true>;
'blocks-docs': BlocksDocsSelect<false> | BlocksDocsSelect<true>;
'unique-fields': UniqueFieldsSelect<false> | UniqueFieldsSelect<true>;
'select-has-many': SelectHasManySelect<false> | SelectHasManySelect<true>;
'payload-kv': PayloadKvSelect<false> | PayloadKvSelect<true>;
users: UsersSelect<false> | UsersSelect<true>;
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
Expand All @@ -144,6 +146,9 @@ export interface Config {
'virtual-relation-global': VirtualRelationGlobalSelect<false> | VirtualRelationGlobalSelect<true>;
};
locale: 'en' | 'es' | 'uk';
widgets: {
collections: CollectionsWidget;
};
user: User;
jobs: {
tasks: unknown;
Expand Down Expand Up @@ -693,6 +698,16 @@ export interface UniqueField {
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "select-has-many".
*/
export interface SelectHasMany {
id: string;
roles?: ('user' | 'admin' | 'editor')[] | null;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-kv".
Expand Down Expand Up @@ -830,6 +845,10 @@ export interface PayloadLockedDocument {
relationTo: 'unique-fields';
value: string | UniqueField;
} | null)
| ({
relationTo: 'select-has-many';
value: string | SelectHasMany;
} | null)
| ({
relationTo: 'users';
value: string | User;
Expand Down Expand Up @@ -1330,6 +1349,15 @@ export interface UniqueFieldsSelect<T extends boolean = true> {
updatedAt?: T;
createdAt?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "select-has-many_select".
*/
export interface SelectHasManySelect<T extends boolean = true> {
roles?: T;
updatedAt?: T;
createdAt?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-kv_select".
Expand Down Expand Up @@ -1540,6 +1568,16 @@ export interface VirtualRelationGlobalSelect<T extends boolean = true> {
createdAt?: T;
globalType?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "collections_widget".
*/
export interface CollectionsWidget {
data?: {
[k: string]: unknown;
};
width: 'full';
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "auth".
Expand Down