fix: column not exists error after destory relation field (#1465)

* test: field not exists

* fix: create association field after other association field destroyed
This commit is contained in:
ChengLei Shao 2023-02-19 11:55:25 +08:00 committed by GitHub
parent 9dd749ef9a
commit 24fb25754f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 182 additions and 6 deletions

View File

@ -488,7 +488,13 @@ export class Collection<
// @ts-ignore
this.model._indexes = lodash.uniqBy(
indexes.map((item) => {
indexes
.filter((item) => {
return item.fields.every((field) =>
Object.values(this.model.rawAttributes).find((fieldVal) => fieldVal.field === field),
);
})
.map((item) => {
if (this.options.underscored) {
item.fields = item.fields.map((field) => snakeCase(field));
}

View File

@ -343,4 +343,172 @@ describe('collections repository', () => {
const tableInfo4 = await getTableInfo();
expect(tableInfo4['test_field']).not.toBeDefined();
});
it('should destroy relation field after foreignKey destroyed', async () => {
await Collection.repository.create({
context: {},
values: {
name: 'c1Z',
fields: [
{
type: 'string',
name: 'a',
},
],
},
});
await Collection.repository.create({
context: {},
values: {
name: 'c2Z',
},
});
await Field.repository.create({
values: {
name: 'g1',
foreignKey: 'a',
target: 'c2Z',
type: 'belongsTo',
collectionName: 'c1Z',
interface: 'm2o',
},
context: {},
});
const foreignKey = await Field.repository.findOne({
filter: {
collectionName: 'c1Z',
name: 'a',
},
});
expect(foreignKey.get('isForeignKey')).toBeTruthy();
await Field.repository.destroy({
filter: {
name: 'a',
},
context: {},
});
expect(await Field.repository.findOne({ filter: { name: 'g1' } })).toBeFalsy();
});
it('should create field normal when repeat create and destroy', async () => {
const c1Z = await Collection.repository.create({
values: {
name: 'c1Z',
},
context: {},
});
const c2Z = await Collection.repository.create({
values: {
name: 'c2Z',
},
context: {},
});
await Field.repository.create({
values: {
name: 'a',
type: 'string',
collectionName: 'c1Z',
},
context: {},
});
await Field.repository.create({
values: {
name: 'b',
type: 'integer',
collectionName: 'c1Z',
},
context: {},
});
await Field.repository.create({
values: {
name: 'g1',
type: 'belongsTo',
target: 'c2Z',
foreignKey: 'a',
collectionName: 'c1Z',
},
context: {},
});
await Field.repository.create({
values: {
name: 'g2',
type: 'belongsTo',
target: 'c2Z',
foreignKey: 'b',
collectionName: 'c1Z',
},
context: {},
});
await Field.repository.create({
values: {
name: 'g3',
type: 'belongsTo',
target: 'c2Z',
foreignKey: 'a',
collectionName: 'c1Z',
interface: 'm2o',
},
context: {},
});
await Field.repository.create({
values: {
name: 'g1',
type: 'hasMany',
target: 'c1Z',
foreignKey: 'a',
collectionName: 'c2Z',
interface: 'o2m',
},
context: {},
});
await Field.repository.create({
values: {
name: 'g2',
type: 'hasMany',
target: 'c1Z',
foreignKey: 'b',
collectionName: 'c2Z',
interface: 'o2m',
},
context: {},
});
await Field.repository.destroy({
filter: {
collectionName: 'c1Z',
name: ['a', 'b'],
},
context: {},
});
expect(await Field.repository.count()).toBe(0);
await Field.repository.create({
values: {
name: 'g5',
type: 'belongsTo',
target: 'c2Z',
foreignKey: 'a1',
collectionName: 'c1Z',
interface: 'm2o',
},
context: {},
});
expect(await Field.repository.count()).toBe(2);
});
});

View File

@ -11,9 +11,11 @@ export function afterCreateForForeignKeyField(db: Database) {
const M = collection.model;
const attr = M.rawAttributes[foreignKey];
if (!attr) {
throw new Error(`${collectionName}.${foreignKey} does not exists`);
}
return attribute2field(attr);
}