【发布时间】:2021-06-28 18:27:52
【问题描述】:
我需要一些关于迁移的帮助。我正在尝试使用迁移来播种数据库。但我收到错误“QueryFailedError:关系“帐户”不存在”。我认为这只是典型的新手错误。所以请检查我的代码:
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 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
@nestjs/typeorm:7.1.5
【问题讨论】:
-
你如何初始化你的数据库?
-
@shusson 通过 TypeOrmModule.forRootAsync
-
你能显示更多代码吗? TypeORM 可以为您创建架构,但这取决于您如何初始化 TypeORM 连接。
-
@shusson 检查此gist
-
您需要仔细检查您的实体列表是否包含帐户实体。 gist.github.com/qWici/…
标签: typeorm