【问题标题】:TypeORM-seeding runs through but no new entries are addedTypeORM-seeding 运行,但没有添加新条目
【发布时间】:2022-01-26 09:43:23
【问题描述】:

我正在尝试使用 typeorm-seeding 将一些数据添加到我的数据库中。运行seed:run 并说它执行我的种子,但是,数据库中没有添加新条目。我的种子的路径是正确的,有什么建议可能是错的吗? :)
我有以下基本的.env 文件

# Database Config
IDEA_DB_HOST=localhost
IDEA_DB_PORT=5432
IDEA_DB_USER=postgres
IDEA_DB_PASS=postgres
IDEA_DB_NAME=idea_db

还有以下ormconfig.js

const path = require('path'); // eslint-disable-line

module.exports = {
  host: process.env.IDEA_DB_HOST,
  username: process.env.IDEA_DB_USER,
  password: process.env.IDEA_DB_PASS,
  port: process.env.IDEA_DB_PORT,
  name: process.env.IDEA_DB_NAME,
  seeds: ['src/database/seeds/*.ts'],
  factories: ['src/database/factories/*.ts'],
  type: 'postgres',
  entities: ['src/**/entities/*.entity{.ts,.js}'],
  synchronize: true,
  logging: false,
};

我的种子很基础

import { Factory, Seeder } from 'typeorm-seeding';
import { Connection } from 'typeorm';
import {
  Exercise,
  ExerciseType,
} from '../../exercises/entities/exercise.entity';

export default class CreateExercises implements Seeder {
  public async run(factory: Factory, connection: Connection): Promise<any> {
    await connection
      .createQueryBuilder()
      .insert()
      .into(Exercise)
      .values([
        {
          id: 1,
          createdAt: new Date(),
          updatedAt: new Date(),
          content: 'some demo content',
          solution: 'some demo content',
          labels: ['Winter'],
          exerciseType: ExerciseType.MATHE,
          skills: ['skill 1'],
          minDifficulty: 1,
          maxDifficulty: 2,
          verified: true,
          authorId: '65ca2dd2-6649-11ec-90d6-0242ac120003',
        },
      ])
      .execute();
  }
}

当我运行种子时,我得到以下输出:运行

$ ts-node ./node_modules/typeorm-seeding/dist/cli.js seed
????  TypeORM Seeding v1.6.1
√ ORM Config loaded
√ Factories are imported
√ Seeders are imported
√ Database connected
√ Seeder CreateExercises executed
????  Finished Seeding
Done in 4.58s.

【问题讨论】:

    标签: typescript postgresql typeorm seeding


    【解决方案1】:

    看来你没有保存数据!

    我的意见是这样工作

    import { Factory, Seeder } from 'typeorm-seeding';
    import { Connection } from 'typeorm';
    import {
      Exercise,
      ExerciseType,
    } from '../../exercises/entities/exercise.entity';
    
    export default class CreateExercises implements Seeder {
      private data = [
        {
          id: 1,
          createdAt: new Date(),
          updatedAt: new Date(),
          content: 'some demo content',
          solution: 'some demo content',
          labels: ['Winter'],
          exerciseType: ExerciseType.MATHE,
          skills: ['skill 1'],
          minDifficulty: 1,
          maxDifficulty: 2,
          verified: true,
          authorId: '65ca2dd2-6649-11ec-90d6-0242ac120003',
        },
      ];
      public async run(factory: Factory, connection: Connection): Promise<any> {
        await Promise.all(
          this.data.map((x) =>
            connection.getRepository<Exercise>('exercise').create(x).save(),
          ),
        );
      }
    }
    

    【讨论】:

    • 谢谢你的回答,但它似乎不起作用,首先语法create(x).save()似乎不起作用,我尝试只用save(x)来做,但它也不起作用
    • 怎么了?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2020-12-21
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多