typeorm-seeding icon indicating copy to clipboard operation
typeorm-seeding copied to clipboard

Error when trying to make a connection.

Open abcdefghijklmnop opened this issue 4 years ago • 1 comments

The problem is: Could not save entity. I'm using TypeORM with Postgres.

SEED file:

create-article.seed.ts:

...

export class CreateOpenTournament implements Seeder {
  public async run(factory: Factory): Promise<void> {
    await factory(Article)().create();
  }
}

FACTORY file:

article.factory.ts:

...

define(Article, (faker: typeof Faker) => {
  const article = new Article();

  article.id = faker.random.uuid();
  article.publicedDate = new Date('2012-10-07 12:00');
  article.numberOfViews = 20;
  article.type = ArticleType.BLOG;
  article.status = factory(Status)() as any;
  article.content = factory(ArticleContent)() as any;
  article.contributors = [factory(ArticleMapping)() as any];
  article.category = factory(Category)() as any;
  article.tags = [factory(TagMapping)() as any]; // <-- Error here
  article.ratings = null;

  return article;
});

tagMapping.factory.ts:

...
define(TagMapping, (faker: typeof Faker) => {
  const tagMapping = new TagMapping();
  tagMapping.id = faker.random.uuid();
  tagMapping.isMain = false; //boolean;	<-- here is false but in compile null
  tagMapping.tag = factory(Tag)() as any;

  return tagMapping;
});

Entity file:

article.entity.ts:

...
@Entity()
export class Article extends DefaultEntity {
  ...

  @ApiProperty({ type: [TagMapping], description: 'The Tags belong to this article.' })
  @ManyToMany(() => TagMapping, { cascade: true, eager: true })
  @JoinTable()
  tags: TagMapping[];

  ...
}

This is what I get when running the seeder:

...
❌  Could not save entity
QueryFailedError: null value in column "isMain" violates not-null constraint
  ...
  code: '23502',
  detail: 'Failing row contains (93f39f1d-64b6-41b7-b127-ce9e6f1dcebc, 2021-02-15 11:29:35.901828+01, 2021-02-15 11:29:35.901828+01, null, null, null).',
	...
  query: 'INSERT INTO "tag_mapping"("id", "createDate", "updateDate", "deleteDate", "isMain", "tagId") VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT) RETURNING "id", "createDate", "updateDate", "deleteDate"',
  parameters: []
}
× Could not run the seed CreateOpenTournament!
Error: Could not save entity
	...
npm ERR! code 1
...

Sorry for my bad English.

abcdefghijklmnop avatar Feb 15 '21 12:02 abcdefghijklmnop

Hi! Sorry for late response.

I have to check this deeply, not sure how is the behaviour when working on arrays on factories.

jorgebodega avatar Oct 11 '21 19:10 jorgebodega