Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM: QueryFailedError: relation does not exist

I need a little help with migration. I'm trying to seed DB using migration. But I get an error "QueryFailedError: relation "account" does not exist". I think it's just typical newbie mistake. So please check my code:

account.entity.ts

import { BeforeInsert, Column, Entity, OneToMany } from 'typeorm';
import { AbstractEntity } from '../../common/abstract.entity';
import { SourceEntity } from '../source/source.entity';
import { UtilsService } from '../../shared/services/utils.service';

@Entity({ name: 'account' })
export class AccountEntity extends AbstractEntity {
  @Column({ unique: true })
  username: string;

  @Column({ nullable: true })
  password: string;

  @OneToMany(() => SourceEntity, (source) => source.account, {
    cascade: true,
  })
  sources: SourceEntity[];

  @BeforeInsert()
  async setPassword() {
    this.password = UtilsService.generateHash(this.password);
  }
}

seed-data.migration.ts

import { getCustomRepository, MigrationInterface, QueryRunner } from 'typeorm';
import { AccountRepository } from '../modules/account/account.repository';
import { SourceRepository } from '../modules/source/source.repository';

type DataType = {
  username: string;
  password: string;
  sources: { username: string }[];
};

export class SeedData1617243952346 implements MigrationInterface {
  private data: DataType[] = [
    {
      username: 'test',
      password: 'password',
      sources: [
        { username: 'some_test' },
        { username: 'okey_test' },
        { username: 'super_test' },
      ],
    },
    {
      username: 'account',
      password: 'password',
      sources: [
        { username: 'some_account' },
        { username: 'okey_account' },
        { username: 'super_account' },
      ],
    },
  ];

  public async up(): Promise<void> {
    await Promise.all(
      this.data.map(async (item) => {
        const accountRepository = getCustomRepository(AccountRepository);
        const accountEntity = accountRepository.create();
        accountEntity.username = item.username;
        accountEntity.password = item.password;

        const sourceRepository = getCustomRepository(SourceRepository);
        const sources = [];

        await Promise.all(
          item.sources.map(async (sourceItem) => {
            const sourceEntity = sourceRepository.create();
            sourceEntity.username = sourceItem.username;

            sources.push(sourceEntity);
          }),
        );

        accountEntity.sources = sources;
        const account = await accountRepository.save(accountEntity);
        console.log('Account created:', account.id);
      }),
    );
  }

  public async down(): Promise<void> {
    await Promise.all(
      this.data.map(async (item) => {
        const sourceRepository = getCustomRepository(SourceRepository);
        const accountRepository = getCustomRepository(AccountRepository);

        const account = await accountRepository.findOne({
          where: { username: item.username },
        });
        if (account) {
          await Promise.all(
            item.sources.map(async (src) => {
              const source = await sourceRepository.findOne({
                where: { username: src.username },
              });
              if (source) {
                await sourceRepository.delete(source);
              }
            }),
          );

          await accountRepository.delete(account);
        }
      }),
    );
  }
}

source.entity.ts

import { Column, Entity, ManyToOne } from 'typeorm';
import { AbstractEntity } from '../../common/abstract.entity';
import { AccountEntity } from '../account/account.entity';

@Entity({ name: 'source' })
export class SourceEntity extends AbstractEntity {
  @Column({ unique: true })
  username: string;

  @Column({ default: true })
  overrideCaption: boolean;

  @ManyToOne(() => AccountEntity, (account) => account.sources)
  account: AccountEntity;
}

Error:

Error during migration run:
QueryFailedError: relation "account" does not exist
    at new QueryFailedError (/home/wiha/dev/own/instahub/src/error/QueryFailedError.ts:9:9)
    at PostgresQueryRunner.<anonymous> (/home/wiha/dev/own/instahub/src/driver/postgres/PostgresQueryRunner.ts:228:19)
    at step (/home/wiha/dev/own/instahub/node_modules/tslib/tslib.js:143:27)
    at Object.throw (/home/wiha/dev/own/instahub/node_modules/tslib/tslib.js:124:57)
    at rejected (/home/wiha/dev/own/instahub/node_modules/tslib/tslib.js:115:69)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  length: 106,
  severity: 'ERROR',
  code: '42P01',
  detail: undefined,
  hint: undefined,
  position: '13',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'parse_relation.c',
  line: '1191',
  routine: 'parserOpenTable',
  query: 'INSERT INTO "account"("id", "created_at", "updated_at", "username", "password") VALUES (DEFAULT, DEFAULT, DEFAULT, $1, $2) RETURNING "id", "created_at", "updated_at"',
  parameters: [
    'test',
    '$2b$10$iB6yb3D8e6iGmKoVAJ7eYeYfoItclw5lcVXqauPf9VH94DlDrbuSa'
  ]
}

Tables are created in another migration

DB: PostgreSQL 12.6
Node: 14.0.0
TypeORM: 0.2.32
@nestjs/typeorm: 7.1.5

like image 718
Vania Kycher Avatar asked Nov 27 '25 07:11

Vania Kycher


2 Answers

When creating a connection through TypeORM you need to pass synchronize: true, if you want TypeORM to create schema for you. Alternativly you can manually run sync using the CLI with schema:sync command.

More e.g :


createConnection({
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "root",
    password: "admin",
    database: "test",
    entities: [
        Photo
    ],

// ---
    synchronize: true,
// ---
    logging: false

From the docs

synchronize - Indicates if database schema should be auto created on every application launch. Be careful with this option and don't use this in production - otherwise you can lose production data. This option is useful during debug and development. As an alternative to it, you can use CLI and run schema:sync command. Note that for MongoDB database it does not create schema, because MongoDB is schemaless. Instead, it syncs just by creating indices.

like image 128
shusson Avatar answered Nov 29 '25 21:11

shusson


Just in case I help someone, my issue was not specifying the schema in the Entity

@Entity({ name: 'account', schema: 'companydbo' })
like image 45
Ari Waisberg Avatar answered Nov 29 '25 20:11

Ari Waisberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!