一对一加入自我

时间:2018-08-18 03:25:11

标签: typeorm

是否可以使用OneTonOne关系将实体加入自身?

我尝试过:

@OneToOne(type => Person, spouse => spouse.id)
@JoinColumn()
spouse: Person; 

但是,从可以正常工作的代码中获取此错误:

{ QueryFailedError: SQLITE_CONSTRAINT: FOREIGN KEY constraint failed
    at new QueryFailedError (/Users/jonhaven/Dev/Maverick/mav-typeorm/src/error/QueryFailedError.ts:7:9)
    at Statement.handler (/Users/jonhaven/Dev/Maverick/mav-typeorm/src/driver/sqlite/SqliteQueryRunner.ts:53:26)
    at Statement.replacement (/Users/jonhaven/Dev/Maverick/mav-typeorm/node_modules/sqlite3/lib/trace.js:19:31)
    at Statement.replacement (/Users/jonhaven/Dev/Maverick/mav-typeorm/node_modules/sqlite3/lib/trace.js:19:31)
  message: 'SQLITE_CONSTRAINT: FOREIGN KEY constraint failed',
  errno: 19,
  code: 'SQLITE_CONSTRAINT',
  name: 'QueryFailedError',
  query: 'DROP TABLE "person"',
  parameters: [] }

1 个答案:

答案 0 :(得分:0)

这是TypeORM上SQLite的问题。您应该将TypeORM更新到最新版本,并在关系中添加删除级联选项

@OneToOne(type => Person, spouse => spouse.id, { 
  cascade: true, 
  onDelete: "CASCADE" 
})
@JoinColumn()
spouse: Person;