postgresql 类型ORM:查询失败错误:关系不存在

kdfy810k  于 2022-11-23  发布在  PostgreSQL
关注(0)|答案(6)|浏览(244)

我需要一些迁移方面的帮助。我正在尝试使用迁移来植入数据库。但是我收到错误消息“QueryFailedError:关系“帐户”不存在”。我认为这只是一个典型的新手错误。所以请检查我的代码:

帐户.实体.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);
  }
}

种子数据.迁移.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);
        }
      }),
    );
  }
}

源.实体.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 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'
  ]
}

在另一个迁移中创建表
数据库:PostgreSQL 12.6
节点:14.0.0
类型ORM:0.2.32
@嵌套/类型表单:7.1.5

kupeojn6

kupeojn61#

通过TypeORM创建连接时,如果希望TypeORM为您创建架构,则需要传递synchronize: true,。或者,可以使用CLI的schema:sync命令手动运行sync。
更多信息,例如:

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

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

从文档中
synchronize -指示是否应该在每次应用程序启动时自动创建数据库模式。使用此选项时要小心,不要在生产环境中使用此选项-否则会丢失生产数据。此选项在调试和开发过程中非常有用。作为一种替代方法,您可以使用CLI并运行schema:sync命令。请注意,对于MongoDB数据库,它不会创建模式,因为MongoDB是无模式的。相反,它只通过创建索引来同步。

7rfyedvj

7rfyedvj2#

万一我帮助了别人,我的问题是没有在实体中指定模式

@Entity({ name: 'account', schema: 'companydbo' })
l7wslrjt

l7wslrjt3#

我也在尝试类似的方法。我把数据源[countries]放在JSON文件中,这样就可以了:`

// In a prev migration the countries table was created
// Then in a new migration

export class SeedCountriesTable1616610473154 implements MigrationInterface {
    public async up(queryRunner: QueryRunner): Promise<void> {
        // Trying to commit last transcaction
        await queryRunner.commitTransaction().then(async () => {
            // Seeding database
            // Then try to start another one
            await queryRunner.startTransaction().then(async () => {
                await getRepository('countries').save(countries);
            });
        });
    }

    // eslint-disable-next-line @typescript-eslint/no-empty-function
    public async down(): Promise<void> {}
}

'我希望能帮助你。

xsuvu9jc

xsuvu9jc4#

我得到这个错误是因为我用双引号而不是单引号括住我的值。例如:
错误:

private static async getProducts(): Promise<Product[]> {
  return await getRepository(Product)
    .createQueryBuilder('product')
    .where('product.source = "noobie"') // <---- DOUBLE QUOTES = BAD!!!
    .getMany();
}

右边:

private static async getProducts(): Promise<Product[]> {
  return await getRepository(Product)
    .createQueryBuilder('product')
    .where("product.source = 'noobie'") // <---- SINGLE QUOTES!!!
    .getMany();
}
ss2ws0br

ss2ws0br5#

只需在数据源中添加migrationsTransactionMode: 'each':)

m1m5dgzv

m1m5dgzv6#

我在NestJS中做e2e测试时遇到了这个错误。Jest在不同的文件中同时运行测试。当你对所有测试使用相同的数据库示例时,这是一个问题,因为不同的测试将同时编辑或拆除数据库。
为了解决这个问题,我在package.json文件中的test:e2e脚本的jest命令中附加了--runInBand选项:

"test:e2e": "cross-env NODE_ENV=test jest --config ./test/jest-e2e.json --runInBand"

这将使测试一次连续运行一个文件。使用此选项还意外地减少了完成测试所需的时间。
还有其他方法可以让测试同时运行,但是您必须对多个文件使用多个数据库示例,而且设置过程也比较复杂。

相关问题