fix(database): missing reference when rewrite parent field (#1977)

This commit is contained in:
ChengLei Shao 2023-06-03 00:06:54 +08:00 committed by GitHub
parent 9076a1d4e4
commit d86da18006
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 7 deletions

View File

@ -16,6 +16,60 @@ pgOnly()('collection inherits', () => {
await db.close(); await db.close();
}); });
it('should not remove parent field reference map after child rewrite field', async () => {
const through = db.collection({
name: 'through',
fields: [{ name: 'name', type: 'string' }],
});
const rootCollection = db.collection({
name: 'root',
fields: [
{ name: 'name', type: 'string' },
{
name: 'targets',
type: 'belongsToMany',
target: 'root-target',
through: 'through',
foreignKey: 'rootId',
otherKey: 'targetId',
},
],
});
const rootTarget = db.collection({
name: 'root-target',
fields: [{ name: 'name', type: 'string' }],
});
await db.sync({ force: true });
expect(db.referenceMap.getReferences('root-target')).toHaveLength(1);
const child = db.collection({
name: 'child',
inherits: ['root'],
});
const childTarget = db.collection({
name: 'child-target',
inherits: ['root-target'],
});
await child.setField('targets', {
name: 'targets',
type: 'belongsToMany',
target: 'child-target',
through: 'through',
foreignKey: 'rootId',
otherKey: 'targetId',
});
await db.sync();
expect(db.referenceMap.getReferences('root-target')).toHaveLength(1);
});
it('should list data filtered by child type', async () => { it('should list data filtered by child type', async () => {
const rootCollection = db.collection({ const rootCollection = db.collection({
name: 'root', name: 'root',

View File

@ -5,12 +5,12 @@ import { checkIdentifier } from '../utils';
import { BaseRelationFieldOptions, RelationField } from './relation-field'; import { BaseRelationFieldOptions, RelationField } from './relation-field';
export class BelongsToField extends RelationField { export class BelongsToField extends RelationField {
static type = 'belongsTo';
get dataType() { get dataType() {
return 'BelongsTo'; return 'BelongsTo';
} }
static type = 'belongsTo';
get target() { get target() {
const { target, name } = this.options; const { target, name } = this.options;
return target || Utils.pluralize(name); return target || Utils.pluralize(name);
@ -78,7 +78,9 @@ export class BelongsToField extends RelationField {
this.collection.addIndex([this.options.foreignKey]); this.collection.addIndex([this.options.foreignKey]);
this.database.referenceMap.addReference(this.reference(association)); const reference = this.reference(association);
this.database.referenceMap.addReference(reference);
return true; return true;
} }
@ -103,7 +105,7 @@ export class BelongsToField extends RelationField {
} }
const association = collection.model.associations[this.name]; const association = collection.model.associations[this.name];
if (association) { if (association && !this.options.inherit) {
const reference = this.reference(association); const reference = this.reference(association);
this.database.referenceMap.removeReference(reference); this.database.referenceMap.removeReference(reference);
} }

View File

@ -135,7 +135,8 @@ export class BelongsToManyField extends RelationField {
// 删掉 model 的关联字段 // 删掉 model 的关联字段
const association = collection.model.associations[this.name]; const association = collection.model.associations[this.name];
if (association) {
if (association && !this.options.inherit) {
this.references(association).forEach((reference) => this.database.referenceMap.removeReference(reference)); this.references(association).forEach((reference) => this.database.referenceMap.removeReference(reference));
} }

View File

@ -175,7 +175,7 @@ export class HasManyField extends RelationField {
const association = collection.model.associations[this.name]; const association = collection.model.associations[this.name];
if (association) { if (association && !this.options.inherit) {
this.database.referenceMap.removeReference(this.reference(association)); this.database.referenceMap.removeReference(this.reference(association));
} }

View File

@ -181,7 +181,7 @@ export class HasOneField extends RelationField {
const association = collection.model.associations[this.name]; const association = collection.model.associations[this.name];
if (association) { if (association && !this.options.inherit) {
this.database.referenceMap.removeReference(this.reference(association)); this.database.referenceMap.removeReference(this.reference(association));
} }