mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-08 06:59:26 +08:00
fix(m2m-array): issue where updating m2m array fields in single relation collection (#5820)
* fix(m2m-array): issue where updating m2m array fields in single relation collection * fix: select * test: add test cases
This commit is contained in:
parent
33eda2665c
commit
a0aa3b8663
@ -195,7 +195,7 @@ const InternalFileManager = (props) => {
|
|||||||
const pickerProps = {
|
const pickerProps = {
|
||||||
size: 'small',
|
size: 'small',
|
||||||
fieldNames,
|
fieldNames,
|
||||||
multiple: ['o2m', 'm2m'].includes(collectionField?.interface) && multiple,
|
multiple: ['o2m', 'm2m', 'mbm'].includes(collectionField?.interface) && multiple,
|
||||||
association: {
|
association: {
|
||||||
target: collectionField?.target,
|
target: collectionField?.target,
|
||||||
},
|
},
|
||||||
|
@ -108,7 +108,7 @@ export abstract class SingleRelationRepository extends RelationRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await updateModelByValues(target, options?.values, {
|
await updateModelByValues(target, options?.values, {
|
||||||
...lodash.omit(options, 'values'),
|
...options,
|
||||||
transaction,
|
transaction,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -112,4 +112,108 @@ describe('m2m array api, bigInt targetKey', () => {
|
|||||||
});
|
});
|
||||||
expect(res.status).toBe(200);
|
expect(res.status).toBe(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('update m2m array field in single realtion collection', async () => {
|
||||||
|
await db.getRepository('collections').create({
|
||||||
|
values: {
|
||||||
|
name: 'tags',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'id',
|
||||||
|
type: 'bigInt',
|
||||||
|
autoIncrement: true,
|
||||||
|
primaryKey: true,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'title',
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await db.getRepository('collections').create({
|
||||||
|
values: {
|
||||||
|
name: 'users',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'id',
|
||||||
|
type: 'bigInt',
|
||||||
|
autoIncrement: true,
|
||||||
|
primaryKey: true,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'username',
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'tags',
|
||||||
|
type: 'belongsToArray',
|
||||||
|
foreignKey: 'tag_ids',
|
||||||
|
target: 'tags',
|
||||||
|
targetKey: 'id',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await db.getRepository('collections').create({
|
||||||
|
values: {
|
||||||
|
name: 'projects',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'id',
|
||||||
|
type: 'bigInt',
|
||||||
|
autoIncrement: true,
|
||||||
|
primaryKey: true,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'title',
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'users',
|
||||||
|
type: 'belongsTo',
|
||||||
|
foreignKey: 'user_id',
|
||||||
|
target: 'users',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
// @ts-ignore
|
||||||
|
await db.getRepository('collections').load();
|
||||||
|
await db.sync();
|
||||||
|
await db.getRepository('tags').create({
|
||||||
|
values: [{ title: 'a' }, { title: 'b' }, { title: 'c' }],
|
||||||
|
});
|
||||||
|
await db.getRepository('users').create({
|
||||||
|
values: { id: 1, username: 'a' },
|
||||||
|
});
|
||||||
|
let user = await db.getRepository('users').findOne({
|
||||||
|
filterByTk: 1,
|
||||||
|
});
|
||||||
|
expect(user.tag_ids).toEqual(null);
|
||||||
|
await db.getRepository('projects').create({
|
||||||
|
values: { id: 1, title: 'p1', user_id: 1 },
|
||||||
|
});
|
||||||
|
const res = await agent.resource('projects.users', 1).update({
|
||||||
|
filterByTk: 1,
|
||||||
|
values: {
|
||||||
|
tags: [
|
||||||
|
{ id: 1, title: 'a' },
|
||||||
|
{ id: 2, title: 'b' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
user = await db.getRepository('users').findOne({
|
||||||
|
filterByTk: 1,
|
||||||
|
});
|
||||||
|
if (db.sequelize.getDialect() === 'postgres') {
|
||||||
|
expect(user.tag_ids).toMatchObject(['1', '2']);
|
||||||
|
} else {
|
||||||
|
expect(user.tag_ids).toMatchObject([1, 2]);
|
||||||
|
}
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -256,6 +256,18 @@ describe('m2m array api, bigInt targetKey', () => {
|
|||||||
} else {
|
} else {
|
||||||
expect(user2.tag_ids).toMatchObject([1, 3]);
|
expect(user2.tag_ids).toMatchObject([1, 3]);
|
||||||
}
|
}
|
||||||
|
const user3 = await db.getRepository('users').create({
|
||||||
|
values: {
|
||||||
|
id: 5,
|
||||||
|
username: 'e',
|
||||||
|
tags: { id: 1 },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (db.sequelize.getDialect() === 'postgres') {
|
||||||
|
expect(user3.tag_ids).toMatchObject(['1']);
|
||||||
|
} else {
|
||||||
|
expect(user3.tag_ids).toMatchObject([1]);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create target when creating belongsToArray', async () => {
|
it('should create target when creating belongsToArray', async () => {
|
||||||
|
@ -227,6 +227,14 @@ describe('m2m array api, string targetKey', () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
expect(user2.tag_ids).toMatchObject(['a', 'c']);
|
expect(user2.tag_ids).toMatchObject(['a', 'c']);
|
||||||
|
const user3 = await db.getRepository('users').create({
|
||||||
|
values: {
|
||||||
|
id: 5,
|
||||||
|
username: 'e',
|
||||||
|
tags: { stringCode: 'a' },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(user3.tag_ids).toMatchObject(['a']);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create target when creating belongsToArray', async () => {
|
it('should create target when creating belongsToArray', async () => {
|
||||||
|
@ -25,7 +25,10 @@ export class BelongsToArrayField extends RelationField {
|
|||||||
if (!values || values[name] === undefined) {
|
if (!values || values[name] === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const value: any[] = values[name] || [];
|
let value: any[] = values[name] || [];
|
||||||
|
if (!Array.isArray(value)) {
|
||||||
|
value = [value];
|
||||||
|
}
|
||||||
const tks = [];
|
const tks = [];
|
||||||
const items = [];
|
const items = [];
|
||||||
for (const item of value) {
|
for (const item of value) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user