fix(m2m-array): updating a m2m (array) field throws an error when the updatedBy field is present (#7089)

This commit is contained in:
YANG QIA 2025-06-17 23:54:48 +08:00 committed by GitHub
parent b5711b68b3
commit 11edb003ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 0 deletions

View File

@ -30,6 +30,7 @@ export class BelongsToArrayAssociation {
targetKey: string;
identifierField: string;
as: string;
options: any;
constructor(options: {
db: Database;
@ -40,6 +41,7 @@ export class BelongsToArrayAssociation {
targetKey: string;
}) {
const { db, source, as, foreignKey, target, targetKey } = options;
this.options = options;
this.associationType = 'BelongsToArray';
this.db = db;
this.source = source;

View File

@ -421,3 +421,61 @@ describe('issues', () => {
).resolves.toBeTruthy();
});
});
describe('issues with users', () => {
let app: MockServer;
let db: MockDatabase;
let agent;
beforeEach(async () => {
app = await createMockServer({
plugins: ['field-m2m-array', 'data-source-manager', 'data-source-main', 'error-handler', 'field-sort', 'users'],
});
db = app.db;
agent = app.agent();
});
afterEach(async () => {
await db.clean({ drop: true });
await app.destroy();
});
test('update m2m array field when the updatedBy field is present', async () => {
await db.getRepository('collections').create({
values: {
name: 'test',
fields: [
{
name: 'id',
type: 'bigInt',
autoIncrement: true,
primaryKey: true,
allowNull: false,
},
{
name: 'users',
type: 'belongsToArray',
foreignKey: 'user_ids',
target: 'users',
targetKey: 'username',
},
],
},
});
// @ts-ignore
await db.getRepository('collections').load();
await db.sync();
const record = await db.getRepository('test').create({
values: [{ users: ['nocobase'] }],
});
const values = await agent.resource('test').get({
filterByTk: record[0].id,
appends: ['users'],
});
const res = await agent.resource('test').update({
filterByTk: record[0].id,
values: values.body.data,
});
expect(res.status).toBe(200);
});
});