【发布时间】:2018-08-13 22:40:00
【问题描述】:
在添加与库存实体具有多对一关系的新项目实体后,我在运行测试服务器时遇到了问题。在 PGAdmin4 中,显示的 Item 表在与我的 Inventory 表比较时看起来是正确的,所以我不完全确定 bug splat 的位置,但是当尝试执行我的 get/seed 时,这两个表都没有任何数据输出app.controller.ts.
查看终端报错信息,查找错误码42P01,发现是未定义的表。
提前感谢所有帮助!
Inventory.ts
import { BaseEntity } from "typeorm/repository/BaseEntity";
import {Entity, PrimaryGeneratedColumn, Column, OneToOne, OneToMany} from "typeorm";
import {Player} from "./Player";
import {Item} from "./Item";
@Entity()
export class Inventory{
@PrimaryGeneratedColumn()
id: number;
@Column("varchar", { length: 200 })
name: string;
@OneToOne(type => Player, player => player.inventory)
player: Player;
@OneToMany(type => Item, item => item.inventory)
items: Item[];
}
Item.ts
import { BaseEntity } from "typeorm/repository/BaseEntity";
import {Entity, PrimaryGeneratedColumn, Column, ManyToOne} from "typeorm";
import { Inventory } from "./Inventory";
@Entity()
export class Item{
@PrimaryGeneratedColumn()
id: number;
@Column("varchar", { length: 200 })
name: string;
@ManyToOne(type => Inventory, inventory => inventory.items)
inventory: Inventory;
}
终端错误信息
{ QueryFailedError: relation "public.item_id_seq" does not exist
at new QueryFailedError (C:\Users\JYUN3\Desktop\Projects\NativeScript\mygame\svc\src\error\QueryFailedError.ts:7:9)
at Query.callback (C:\Users\JYUN3\Desktop\Projects\NativeScript\mygame\svc\src\driver\postgres\PostgresQueryRunner.ts:216:26)
at Query.handleError (C:\Users\JYUN3\Desktop\Projects\NativeScript\mygame\svc\node_modules\pg\lib\query.js:143:17)
at Connection.connectedErrorHandler (C:\Users\JYUN3\Desktop\Projects\NativeScript\mygame\svc\node_modules\pg\lib\client.js:132:26)
at Connection.emit (events.js:160:13)
at Socket.<anonymous> (C:\Users\JYUN3\Desktop\Projects\NativeScript\mygame\svc\node_modules\pg\lib\connection.js:117:12)
at Socket.emit (events.js:160:13)
at addChunk (_stream_readable.js:269:12)
at readableAddChunk (_stream_readable.js:256:11)
at Socket.Readable.push (_stream_readable.js:213:10)
message: 'relation "public.item_id_seq" does not exist',
name: 'QueryFailedError',
length: 116,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'namespace.c',
line: '406',
routine: 'RangeVarGetRelidExtended',
query: 'ALTER TABLE "public"."item" ALTER COLUMN "id" SET DEFAULT nextval(\'"public.item_id_seq"\')',
parameters: [] }
app.controller.ts
import { Get, Controller, Param } from '@nestjs/common';
import { Spell } from './entity/Spell';
import { Player } from './entity/Player';
import { Inventory } from './entity/Inventory';
import { Item } from './entity/Item';
import { createConnection } from 'typeorm';
@Get('seed')
seed(): void {
createConnection().then(async connection => {
const player1 = new Player();
player1.name = "TestSubject"
player1.health = 20;
player1.mana = 20;
player1.currentRing = 1;
player1.currentZone = 2;
player1.currentRoom = 2;
const spell1 = new Spell();
spell1.name = "Fire";
spell1.damage = 5;
spell1.mana = 5;
spell1.player = player1;
await connection.manager.save(spell1);
//loading spells into player
player1.spells = [];
player1.spells.push(spell1);
const item1 = new Item();
item1.name = "sword";
await connection.manager.save(item1);
const inventory = new Inventory();
inventory.name = "my items";
inventory.player = player1;
await connection.manager.save(inventory);
player1.inventory.items = [];
player1.inventory.items.push(item1);
await connection.manager.save(player1);
}).catch(error => console.log(error));
}
【问题讨论】:
标签: javascript postgresql typescript typeorm