gpt4 book ai didi

javascript - 类型 ORM QueryFailedError PostgreSQL

转载 作者:行者123 更新时间:2023-11-29 13:45:26 24 4
gpt4 key购买 nike

在添加与库存实体具有 ManyToOne 关系的新 Item 实体后运行测试服务器时,我遇到了问题。在 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[];
}

项目.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: [] }

应用程序 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));
}

最佳答案

您可能会收到此错误,因为您的模式与您的实体不同步

尝试改变 ormconfig.json 中的 “synchronize: true”
此选项用于使模式与数据库保持同步。

仅在开发/调试中使用此选项。

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

关于javascript - 类型 ORM QueryFailedError PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49124626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com