Jac*_*Yun 5 javascript postgresql typescript typeorm
在添加与库存实体具有多对一关系的新项目实体后运行测试服务器时,我遇到了问题。在 PGAdmin4 中,显示的 Item 表在与我的 Inventory 表进行比较时看起来是正确的,所以我不完全确定 bug splat 所在的位置,但是在尝试执行 get/seed 时,这两个表都没有任何数据输出显示在我的app.controller.ts。
查看终端错误消息并搜索错误代码42P01,我发现它显示它是一个未定义的表。
在此先感谢所有帮助!
库存.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[];
}
Run Code Online (Sandbox Code Playgroud)
项目.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;
}
Run Code Online (Sandbox Code Playgroud)
终端错误信息
{ 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: [] }
Run Code Online (Sandbox Code Playgroud)
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));
}
Run Code Online (Sandbox Code Playgroud)
您可能会收到此错误,因为您的架构与您的实体不同步
尝试更改“synchronize: true”in ormconfig.json,
此选项用于使架构与数据库保持同步。
仅在开发/调试中使用此选项。
https://github.com/typeorm/typeorm/blob/master/docs/faq.md#how-do-i-update-a-database-schema
有关连接选项的更多信息:https : //typeorm.io/#/connection-options/common-connection-options
| 归档时间: |
|
| 查看次数: |
11218 次 |
| 最近记录: |