w3tecch / typeorm-seeding

🌱 A delightful way to seed test data into your database.
https://www.npmjs.com/package/typeorm-seeding
MIT License
887 stars 132 forks source link

seed:run completes successfully but there is no added rows. #96

Closed rtman closed 3 years ago

rtman commented 4 years ago

Hey,

I'm migrating my seeding from sequelize (which currently works) to this package. I'm a bit confused here, I'm running the following seeder on a docker migration container. There is another container that also connects to typeorm which works fine, this is the main connection where all the app work is done. I assume you can have multiple typeorm connections active at once, anyways so the seed:run completes without any indication of failure but there are no rows added.

Screen Shot 2020-10-04 at 2 21 31 PM Screen Shot 2020-10-04 at 2 22 14 PM

Here's my ormconfig.js

module.exports = {
  type: 'postgres',
  port: 5432,
  database: process.env.DATABASE_DB,
  username: process.env.DATABASE_USER,
  password: process.env.DATABASE_PASSWORD,
  host: process.env.DATABASE_HOST,
  synchronize: true,
  logging: false,
  entities: ['../graphql/src/orm/models/artist.ts'],
  // "migrations": [
  //    "src/migration/**/*.ts"
  // ],
  // "subscribers": [
  //    "src/subscriber/**/*.ts"
  // ]
  seeds: ['src/seeds/*{.ts,.js}'],
  // "factories": ['src/factories/**/*{.ts,.js}'],
};

Heres the seeder I'm running.

import { Factory, Seeder } from 'typeorm-seeding';
import { Connection } from 'typeorm';
import { Artist } from '../../graphql/src/orm/models';

export default class CreateUsers implements Seeder {
  public async run(factory: Factory, connection: Connection): Promise<any> {
    await connection
      .createQueryBuilder()
      .insert()
      .into(Artist)
      .values([
        {
          id: '0b600e0a-96d0-4ec0-bc94-2587a6b3507a',
          name: 'Various Artists',
          description: '',
          profileImageStoragePathLarge:
            'test',
          profileImageStoragePathSmall:
            'test',
          profileImageStoragePathThumb:
            'test',
          profileImageUrlLarge:
            'test',
          profileImageUrlSmall:
            'test',
          profileImageUrlThumb:
            'test',
          createdAt: new Date(),
          updatedAt: new Date(),
          followers: 0,
        },
      ])
      .execute();
  }
}

How can the seed complete sucessfully but there be no rows?

AdstonOliveira commented 3 years ago

here it is like this

rtman commented 3 years ago

here it is like this

not sure what you mean

rtman commented 3 years ago

Do I need to provide more information, would be great to get some guidance here.

loonskai commented 3 years ago

@rtman I thing you have a typo in your ormconfig.js. Check seeds property, you should have something like this:

seeds: ['src/seeds/**/*{.ts,.js}'],
JulienLeal commented 3 years ago

I have the same issue :(

rtman commented 3 years ago

@SiarheiLunski I believe I tried that initially and it didn't work. But also if the orm config is incorrect, how is it importing seeds as per the image I posted?

SaveYourTime commented 3 years ago
  1. Make sure your seeds and factories are both in the correct format.
  2. Make sure those factory files should be in the src/database/factories folder and suffixed with .factory like src/database/factories/user.factory.ts

Here's my ormconfig.js

module.exports = {
  type: 'mysql',
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  username: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
  entities: [__dirname + '/../**/*.entity.{js,ts}'],
  seeds: [__dirname + '/../database/seeds/**/*{.ts,.js}'],
  factories: [__dirname + '/../database/factories/**/*{.ts,.js}'],
  namingStrategy: new SnakeNamingStrategy(),
  legacySpatialSupport: false,
};
elenahongo commented 3 years ago

same issue here

malteneuss commented 3 years ago

I had a the same problem and debugged it by using the default folder structure src/seeds/.. and src/factories/.. with ormconfig.js being

...
   seeds: ['src/seeds/**/*{.ts,.js}'],
  factories: ['src/factories/**/*{.ts,.js}'],
...

as in the guides. There i saw the CLI actually working: When the CLI recognizes seed files like add-users.seed.ts, you see

✔ Database connected
✔ Seeder AddUsers executed
👍  Finished Seeding

in the log. Turns out i had a typo in my custom seed path property in my ormconfig.js file.

albertcito commented 3 years ago

I had the same issue. With this code:

export default class CreateColors implements Seeder {
  public async run(): Promise<void> {
    return Transaction.run(
      () => this.save(),
    );
  }

  private async save() {
    const promises: Promise<LangColor>[] = [];
    colors.forEach((color) => {
      promises.push(this.saveColor(color));
    });
     Promise.all(promises);
  }
private async saveColor(color: ColorsProperties) {
  ....
}

The issue was in the line Promise.all(promises);. I added await Promise.all(promises); and it works properly.

metalcamp commented 3 years ago

Closing, since it seems to be misconfig issue. If you have any further issues regarding this topic please feel free to reopen.

rtman commented 3 years ago

Just fyi that @malteneuss is correct, but this package should say if it cannot locate any seed files instead of displaying Finished Seeding when it didn't find anything to seed:

Screen Shot 2021-09-14 at 9 18 26 AM

One might think that it was able to locate the files and just didn't log them individually.

JoaquinPiersigilli commented 2 years ago

Same error here, I have my seeds in another directory outside src and even though I changed the ormconfig accordingly and "✔ Seeders are imported" appears, it doesn't actually find anything, but when I change to the default route it works...

The error happen when trying to execute a single Seed by class name