Bugfix relation with same field name (#4414)
* Bugfix relation with same field name * Fix concurrency issue
This commit is contained in:
@ -9,6 +9,7 @@ import { InjectRepository } from '@nestjs/typeorm';
|
|||||||
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
|
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
|
||||||
import { FindOneOptions, In, Repository } from 'typeorm';
|
import { FindOneOptions, In, Repository } from 'typeorm';
|
||||||
import camelCase from 'lodash.camelcase';
|
import camelCase from 'lodash.camelcase';
|
||||||
|
import { v4 as uuidV4 } from 'uuid';
|
||||||
|
|
||||||
import { ObjectMetadataService } from 'src/metadata/object-metadata/object-metadata.service';
|
import { ObjectMetadataService } from 'src/metadata/object-metadata/object-metadata.service';
|
||||||
import { FieldMetadataService } from 'src/metadata/field-metadata/field-metadata.service';
|
import { FieldMetadataService } from 'src/metadata/field-metadata/field-metadata.service';
|
||||||
@ -62,16 +63,21 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
|||||||
isCustom,
|
isCustom,
|
||||||
);
|
);
|
||||||
|
|
||||||
const createdFields = await this.fieldMetadataService.createMany([
|
const fromId = uuidV4();
|
||||||
|
const toId = uuidV4();
|
||||||
|
|
||||||
|
await this.fieldMetadataService.createMany([
|
||||||
this.createFieldMetadataForRelationMetadata(
|
this.createFieldMetadataForRelationMetadata(
|
||||||
relationMetadataInput,
|
relationMetadataInput,
|
||||||
'from',
|
'from',
|
||||||
isCustom,
|
isCustom,
|
||||||
|
fromId,
|
||||||
),
|
),
|
||||||
this.createFieldMetadataForRelationMetadata(
|
this.createFieldMetadataForRelationMetadata(
|
||||||
relationMetadataInput,
|
relationMetadataInput,
|
||||||
'to',
|
'to',
|
||||||
isCustom,
|
isCustom,
|
||||||
|
toId,
|
||||||
),
|
),
|
||||||
this.createForeignKeyFieldMetadata(
|
this.createForeignKeyFieldMetadata(
|
||||||
relationMetadataInput,
|
relationMetadataInput,
|
||||||
@ -80,18 +86,10 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
|||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const createdFieldMap = createdFields.reduce((acc, fieldMetadata) => {
|
|
||||||
if (fieldMetadata.type === FieldMetadataType.RELATION) {
|
|
||||||
acc[fieldMetadata.name] = fieldMetadata;
|
|
||||||
}
|
|
||||||
|
|
||||||
return acc;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
const createdRelationMetadata = await super.createOne({
|
const createdRelationMetadata = await super.createOne({
|
||||||
...relationMetadataInput,
|
...relationMetadataInput,
|
||||||
fromFieldMetadataId: createdFieldMap[relationMetadataInput.fromName].id,
|
fromFieldMetadataId: fromId,
|
||||||
toFieldMetadataId: createdFieldMap[relationMetadataInput.toName].id,
|
toFieldMetadataId: toId,
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.createWorkspaceCustomMigration(
|
await this.createWorkspaceCustomMigration(
|
||||||
@ -222,8 +220,10 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
|||||||
relationMetadataInput: CreateRelationInput,
|
relationMetadataInput: CreateRelationInput,
|
||||||
relationDirection: 'from' | 'to',
|
relationDirection: 'from' | 'to',
|
||||||
isCustom: boolean,
|
isCustom: boolean,
|
||||||
|
id?: string,
|
||||||
) {
|
) {
|
||||||
return {
|
return {
|
||||||
|
...(id && { id: id }),
|
||||||
name: relationMetadataInput[`${relationDirection}Name`],
|
name: relationMetadataInput[`${relationDirection}Name`],
|
||||||
label: relationMetadataInput[`${relationDirection}Label`],
|
label: relationMetadataInput[`${relationDirection}Label`],
|
||||||
description: relationMetadataInput[`${relationDirection}Description`],
|
description: relationMetadataInput[`${relationDirection}Description`],
|
||||||
|
|||||||
Reference in New Issue
Block a user