mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 12:56:13 +00:00
fix(plugin-cm): fix unique option default value to update (#768)
This commit is contained in:
parent
b6daa9ad69
commit
e6a2dff79a
@ -23,6 +23,36 @@ describe('field indexes', () => {
|
||||
await app.destroy();
|
||||
});
|
||||
|
||||
it('create unique constraint after added dulplicated records', async () => {
|
||||
const tableName = 'test1';
|
||||
// create an field with unique constraint
|
||||
const field = await agent
|
||||
.resource('collections.fields', tableName)
|
||||
.create({
|
||||
values: {
|
||||
name: 'title',
|
||||
type: 'string'
|
||||
},
|
||||
});
|
||||
|
||||
// create a record
|
||||
const response1 = await agent.resource(tableName).create({
|
||||
values: { title: 't1' }
|
||||
});
|
||||
// create another same record
|
||||
const response2 = await agent.resource(tableName).create({
|
||||
values: { title: 't1' }
|
||||
});
|
||||
|
||||
const response3 = await agent.resource('fields').update({
|
||||
filterByTk: field.id,
|
||||
values: {
|
||||
unique: true
|
||||
}
|
||||
});
|
||||
expect(response3.status).toBe(400);
|
||||
});
|
||||
|
||||
it('field value cannot be duplicated with unique index', async () => {
|
||||
const tableName = 'test1';
|
||||
// create an field with unique constraint
|
||||
|
@ -77,9 +77,9 @@ export class CollectionManagerPlugin extends Plugin {
|
||||
|
||||
this.app.db.on('fields.afterUpdate', async (model: FieldModel, { context, transaction }) => {
|
||||
if (context) {
|
||||
const prev = model.previous('options')?.unique;
|
||||
const next = model.get('options')?.unique;
|
||||
if (lodash.isBoolean(prev) && lodash.isBoolean(next) && prev !== next) {
|
||||
const { unique: prev } = model.previous('options');
|
||||
const { unique: next } = model.get('options');
|
||||
if (Boolean(prev) !== Boolean(next)) {
|
||||
await model.migrate({ transaction });
|
||||
}
|
||||
}
|
||||
@ -180,7 +180,9 @@ export class CollectionManagerPlugin extends Plugin {
|
||||
|
||||
const errorHandlerPlugin = <PluginErrorHandler>this.app.getPlugin('@nocobase/plugin-error-handler');
|
||||
errorHandlerPlugin.errorHandler.register(
|
||||
(err) => err instanceof UniqueConstraintError,
|
||||
(err) => {
|
||||
return err instanceof UniqueConstraintError;
|
||||
},
|
||||
(err, ctx) => {
|
||||
return ctx.throw(400, ctx.t(`The value of ${Object.keys(err.fields)} field duplicated`));
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user