diff --git a/packages/core/database/src/__tests__/operator/array-operator.test.ts b/packages/core/database/src/__tests__/operator/array-operator.test.ts index 537b533969..08f988be93 100644 --- a/packages/core/database/src/__tests__/operator/array-operator.test.ts +++ b/packages/core/database/src/__tests__/operator/array-operator.test.ts @@ -204,6 +204,37 @@ describe('array field operator', function () { expect(filter3[0].get('name')).toEqual(t2.get('name')); }); + test('$anyOf with association with same column name', async () => { + const Tag = db.collection({ + name: 'tags', + fields: [ + { type: 'array', name: 'type' }, + { type: 'string', name: 'name' }, + ], + }); + + const Post = db.collection({ + name: 'posts', + tableName: 'posts_table', + fields: [ + { type: 'array', name: 'type' }, + { + type: 'hasMany', + name: 'tags', + }, + ], + }); + + await db.sync({ force: true }); + + await Post.repository.find({ + filter: { + 'type.$anyOf': ['aa'], + 'tags.name': 't1', + }, + }); + }); + // fix https://nocobase.height.app/T-2803 test('$anyOf with string', async () => { const filter3 = await Test.repository.find({ diff --git a/packages/core/database/src/operators/array.ts b/packages/core/database/src/operators/array.ts index e59ee4fb03..83c9def9bf 100644 --- a/packages/core/database/src/operators/array.ts +++ b/packages/core/database/src/operators/array.ts @@ -33,15 +33,18 @@ const getFieldName = (ctx) => { const model = getModelFromAssociationPath(); + let columnPrefix = model.name; + if (model.rawAttributes[fieldName]) { columnName = model.rawAttributes[fieldName].field || fieldName; } if (associationPath.length > 0) { const association = associationPath.join('->'); - columnName = `${association}.${columnName}`; + columnPrefix = association; } + columnName = `${columnPrefix}.${columnName}`; return columnName; };