最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeORM QueryFailedError PostgreSQL - Stack Overflow

programmeradmin5浏览0评论

I'm running into an issue when running a test server after adding a new Item entity that has a ManyToOne relationship with an inventory entity. In PGAdmin4, an Item table is shown that looks correct when comparing it to my Inventory table, so I'm not entirely sure where the bug splat is located, but neither table have any data output when trying to execute get/seed shown in my app.controller.ts.

Looking at the terminal error message and searching up the error code 42P01, I found that it's showing that it's an undefined table.

Thanks in advance for all help!

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;

}

Terminal error message

{ 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));
}

I'm running into an issue when running a test server after adding a new Item entity that has a ManyToOne relationship with an inventory entity. In PGAdmin4, an Item table is shown that looks correct when comparing it to my Inventory table, so I'm not entirely sure where the bug splat is located, but neither table have any data output when trying to execute get/seed shown in my app.controller.ts.

Looking at the terminal error message and searching up the error code 42P01, I found that it's showing that it's an undefined table.

Thanks in advance for all help!

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;

}

Terminal error message

{ 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));
}
Share Improve this question edited Nov 17, 2021 at 6:54 Audwin Oyong 2,5563 gold badges19 silver badges36 bronze badges asked Mar 6, 2018 at 6:27 Jacob YunJacob Yun 1531 gold badge2 silver badges10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 10

You may be getting this error because your schema is not in sync with your entities

Try changing “synchronize: true” in ormconfig.json,
this option is used to keep the schema in sync with the database.

Use this option only in development/debug.

https://github.com/typeorm/typeorm/blob/master/docs/faq.md#how-do-i-update-a-database-schema

for more about connection options: https://typeorm.io/#/connection-options/common-connection-options

I had the same problem. The reason was that I had synchronize: false and didn't make any initial migration, too.

发布评论

评论列表(0)

  1. 暂无评论