[英]Getting error when I save() an entity - Error: Cyclic dependency: "e" - AWS serverless with typeORM
我正在使用带有 Typescript 和TypeORM 的AWS 无服务器Node js 做一个项目,看起来一切正常。 在本地,我可以获得正常或复杂的查询。 但是当我开始在我的应用程序中添加/编辑部分时,我被卡住了。
我正在尝试保存我的完整实体,它也保存了所有关系。 然后我将cascade: true
参数应用于实体属性,它完美地工作。
当我将应用程序成功部署到 aws 时,问题就开始了。 所有查询都有效,但特别是在保存实体 tripLeg 时,我收到此错误:循环依赖:“e”
它仅在部署版本时发生,本地运行完美。 你知道如何解决吗?
实体(注意最后3个属性)
import {
Column,
Entity,
Index,
JoinColumn,
ManyToOne,
OneToMany,
OneToOne,
PrimaryGeneratedColumn
} from 'typeorm';
import { Fbo } from './Fbo';
import { Airport } from './Airport';
import { Trip } from './Trip';
import { Pilot } from './Pilot';
import { TripLegClient } from './TripLegClient';
import { TripLegPassenger } from './TripLegPassenger';
import { TripLegStats } from './TripLegStats';
@Index('idx_unique_trip_legNo', ['legNo', 'tripId'], { unique: true })
@Index('IDX_d1cb2c6e35ce929f806af521c8', ['id'], { unique: true })
@Index('REL_d1cb2c6e35ce929f806af521c8', ['id'], { unique: true })
@Index('id', ['id'], {})
@Index('fk_fromId_airport_idx', ['fromId'], {})
@Index('fk_toId_airport_idx', ['toId'], {})
@Index('fk_fromFboId_fbo_idx', ['fromFboId'], {})
@Index('fk_toFboId_fbo_idx', ['toFboId'], {})
@Index('tripId_idx', ['tripId'], {})
@Index('fk_pic_idx', ['picId'], {})
@Index('fk_sic_idx', ['sicId'], {})
@Entity('trip_leg', { schema: 'schedule' })
export class TripLeg {
@PrimaryGeneratedColumn({ type: 'int', name: 'id' })
id: number;
@Column('int', { name: 'leg_no' })
legNo: number;
@Column('varchar', { name: 'trip_id', length: 10 })
tripId: string;
@Column('varchar', { name: 'from_id', length: 7 })
fromId: string;
@Column('varchar', { name: 'to_id', length: 7 })
toId: string;
@Column('int', { name: 'from_fbo_id' })
fromFboId: number;
@Column('int', { name: 'to_fbo_id' })
toFboId: number;
@Column('int', { name: 'pic_id' })
picId: number;
@Column('int', { name: 'sic_id', nullable: true })
sicId: number | null;
@Column('datetime', { name: 'date_time' })
dateTime: Date;
@ManyToOne(() => Fbo, (fbo) => fbo.tripLegs, {
onDelete: 'NO ACTION',
onUpdate: 'NO ACTION'
})
@JoinColumn([{ name: 'to_fbo_id', referencedColumnName: 'id' }])
toFbo: Fbo;
@ManyToOne(() => Airport, (airport) => airport.tripLegs, {
onDelete: 'NO ACTION',
onUpdate: 'NO ACTION'
})
@JoinColumn([{ name: 'to_id', referencedColumnName: 'id' }])
to: Airport;
@ManyToOne(() => Trip, (trip) => trip.tripLegs, {
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
})
@JoinColumn([{ name: 'trip_id', referencedColumnName: 'id' }])
trip: Trip;
@ManyToOne(() => Pilot, (pilot) => pilot.tripLegs, {
onDelete: 'NO ACTION',
onUpdate: 'NO ACTION'
})
@JoinColumn([{ name: 'sic_id', referencedColumnName: 'id' }])
sic: Pilot;
@ManyToOne(() => Pilot, (pilot) => pilot.tripLegs2, {
onDelete: 'NO ACTION',
onUpdate: 'NO ACTION'
})
@JoinColumn([{ name: 'pic_id', referencedColumnName: 'id' }])
pic: Pilot;
@ManyToOne(() => Airport, (airport) => airport.tripLegs2, {
onDelete: 'NO ACTION',
onUpdate: 'NO ACTION'
})
@JoinColumn([{ name: 'from_id', referencedColumnName: 'id' }])
from: Airport;
@ManyToOne(() => Fbo, (fbo) => fbo.tripLegs2, {
onDelete: 'NO ACTION',
onUpdate: 'NO ACTION'
})
@JoinColumn([{ name: 'from_fbo_id', referencedColumnName: 'id' }])
fromFbo: Fbo;
@OneToMany(() => TripLegClient, (tripLegClient) => tripLegClient.tripLeg, {
cascade: true
})
tripLegClients: TripLegClient[];
@OneToMany(
() => TripLegPassenger,
(tripLegPassenger) => tripLegPassenger.tripLeg,
{ cascade: true }
)
tripLegPassengers: TripLegPassenger[];
@OneToOne(() => TripLegStats, (tripLegStats) => tripLegStats.tripLeg, {
cascade: true
})
tripLegStats: TripLegStats;
}
TypeORM Save() 调用
await conn.getRepository(TripLeg).save(tripLeg);
配置文件
{
"compilerOptions": {
"allowJs" : true,
"allowSyntheticDefaultImports" : true,
"allowUnreachableCode" : true,
"alwaysStrict" : true,
"baseUrl" : "./src/",
"declaration" : true,
"emitDecoratorMetadata" : true,
"esModuleInterop" : true,
"experimentalDecorators" : true,
"forceConsistentCasingInFileNames": true,
"importHelpers" : true,
"inlineSources" : true,
"isolatedModules" : true,
"module" : "CommonJS",
"moduleResolution" : "node",
"noFallthroughCasesInSwitch" : true,
"noImplicitAny" : true,
"noImplicitReturns" : true,
"noImplicitThis" : true,
"noUnusedLocals" : true,
"noUnusedParameters" : true,
"outDir" : "build",
"preserveConstEnums" : true,
"removeComments" : true,
"resolveJsonModule" : true,
"skipLibCheck" : true,
"sourceMap" : true,
"strict" : true,
"strictNullChecks" : true,
"target" : "ESNext",
"types" : ["reflect-metadata", "node"],
"lib" : ["ES5", "ES6", "ES7", "ESNext"],
"strictPropertyInitialization" : false,
"paths" : {
"@constants/*" : ["constants/*"],
"@constants" : ["constants"],
"@controllers/*" : ["controllers/*"],
"@controllers" : ["controllers"],
"@db/*" : ["db/*"],
"@db" : ["db"],
"@handlers/*" : ["handlers/*"],
"@handlers" : ["handlers"],
"@helpers/*" : ["helpers/*"],
"@helpers" : ["helpers"],
"@interfaces/*" : ["interfaces/*"],
"@interfaces" : ["interfaces"],
"@middlewares/*" : ["middlewares/*"],
"@middlewares" : ["middlewares"],
"@providers/*" : ["providers/*"],
"@providers" : ["providers"],
"@services/*" : ["services/*"],
"@services" : ["services"],
"@utils/*" : ["utils/*"],
"@utils" : ["utils"],
"@entities/*" : ["entities/*"],
"@entities" : ["entities"],
"@repositories/*":["repositories/*"],
"@repositories" :["repositories"]
}
},
"exclude": [
".build/**/*",
"build/**/*",
"dist/**/*",
"node_modules/**/*",
".serverless/**/*",
".webpack/**/*",
"_warmup/**/*",
".vscode/**/*",
"../webpack.config.js"
],
"include" : ["**/*.ts"],
"typeRoots": ["node_modules/@types"]
}
我有同样的错误。 问题是最小化器将相同的名称分配给 2 个不同的实体类。
当您保存实体时,TypeORM 会运行一种算法来对实体的关系图进行排序。 如果最小化器为其分配了相同名称的 2 个实体之间存在关系,例如“e”,则将有一个无效的边 ['e', 'e'] 并且算法将抛出错误循环依赖:“e ”。
为了解决这个问题,我在webpack.config.js 中添加了一个配置来保留实体类的名称:
const TerserPlugin = require('terser-webpack-plugin');
optimization: {
minimize: true,
minimizer: [new TerserPlugin({ terserOptions: { keep_classnames: <regex> }})],
}
其中regex是与我所有实体类匹配的正则表达式。 您还可以使用true保留代码的所有类的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.