具有嵌套的ManyToOne关系的OneToMany关系返回断开的关系实体

时间:2019-03-06 09:34:46

标签: typeorm

我正在创建一个具有存储库,贡献和用户的结构。

一个存储库有多个由不同用户完成的贡献。知道贡献的计数属性很重要。

但是当我创建一个没有连接的新存储库实体时,我会得到奇怪的行为。

Example Code

我的index.ts

import "reflect-metadata";
import {createConnection, getRepository} from "typeorm";
import {Repository} from "./entity/Repository";

createConnection().then(async connection => {

    const newRepo = new Repository();
    newRepo.name = 'test';
    newRepo.description = 'no contributions made';
    await connection.manager.save(newRepo);

    const RepoRepository = getRepository(Repository);
    const repos = await RepoRepository.find();

    console.log(repos);
    console.log(repos[0].contributions);

    await connection.manager.save(repos);

    connection.close();

}).catch(error => console.log(error));

我的控制台输出:

repos [ Repository {
    name: 'test',
    description: 'no contributions made',
    contributions: [ [Contribution] ] } ]
contributions [ Contribution { count: null, user: null } ]

这很奇怪,因为我从未创建过任何贡献。

然后connection.manager.save(repos);呼叫错误出现在

{ QueryFailedError: ER_BAD_NULL_ERROR: Column 'count' cannot be null
    at new QueryFailedError (D:\GitHub\Issue\src\error\QueryFailedError.ts:9:9)
    at Query.<anonymous> (D:\GitHub\Issue\src\driver\mysql\MysqlQueryRunner.ts:164:37)
    at Query.<anonymous> (D:\GitHub\Issue\node_modules\mysql\lib\Connection.js:502:10)
    at Query._callback (D:\GitHub\Issue\node_modules\mysql\lib\Connection.js:468:16)
    at Query.Sequence.end (D:\GitHub\Issue\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
    at Query.ErrorPacket (D:\GitHub\Issue\node_modules\mysql\lib\protocol\sequences\Query.js:90:8)
    at Protocol._parsePacket (D:\GitHub\Issue\node_modules\mysql\lib\protocol\Protocol.js:278:23)
    at Parser.write (D:\GitHub\Issue\node_modules\mysql\lib\protocol\Parser.js:76:12)
    at Protocol.write (D:\GitHub\Issue\node_modules\mysql\lib\protocol\Protocol.js:38:16)
    at Socket.<anonymous> (D:\GitHub\Issue\node_modules\mysql\lib\Connection.js:91:28)
  message: 'ER_BAD_NULL_ERROR: Column \'count\' cannot be null',
  code: 'ER_BAD_NULL_ERROR',
  errno: 1048,
  sqlMessage: 'Column \'count\' cannot be null',
  sqlState: '23000',
  index: 0,
  sql:
   'INSERT INTO `contribution`(`count`, `repositoryName`, `employeeGithub`) VALUES (NULL, \'test\', NULL)',
  name: 'QueryFailedError',
  query:
   'INSERT INTO `contribution`(`count`, `repositoryName`, `employeeGithub`) VALUES (?, ?, ?)',
  parameters: [ null, 'test', null ] }

我的实体:

@Entity()
export class Repository {

    @PrimaryColumn()
    name: string;

    @Column("text")
    description: string;

    @OneToMany(type => Contribution, contribution => contribution.repository, {eager: true, cascade: true})
    contributions: Contribution[];
}

@Entity()
export class Contribution{

    @ManyToOne(type => Repository, repository => repository.contributions, {primary: true})
    repository: Repository;

    @ManyToOne(type => User, user => user.contributions, {eager: true, cascade: true, primary: true})
    @JoinTable()
    user: User;

    @Column()
    count: number;
}

@Entity()
export class User{

    @PrimaryColumn()
    github: string;

    @OneToMany(type => Contribution, contribution => contribution.user)
    contributions: Contribution[];
}

我尝试过的事情:

从对用户的贡献中删除ManyToOne上的eager可解决此错误,并且我正确地获得了一个空的贡献数组。但这并不是真正有贡献的存储库的解决方案。

主要问题是:我做错了吗?或者这不是在typeorm中有用的东西?

0 个答案:

没有答案