简体   繁体   English

'SQLite 错误:没有这样的表',在连接的数据库上使用 TypeORM,其他表可访问。 有没有特定的方法来访问带有外键的表?

[英]'SQLite Error: No Such Table' with TypeORM on a connected DB with other tables accessible. Is there a specific way to reach tables with foreign keys?

Good evening all,大家晚上好

I am trying to query on a table called 'MessageEntityXREF' stored on a sqlite DB through TypeORM.我正在尝试通过 TypeORM 查询存储在 sqlite 数据库上的名为“MessageEntityXREF”的表。

The database is connected and other tables are reachable.数据库已连接,其他表可访问。

When I try to Get on this specific table: "QueryFailedError: SQLITE_ERROR: no such table: message_entity_xref"当我尝试获取此特定表时:“QueryFailedError:SQLITE_ERROR:没有这样的表:message_entity_xref”

Any help or advice would be greatly appreciated.任何帮助或建议将不胜感激。

Thanks谢谢

I tried to change the order in the constructor, changed the typing.我试图改变构造函数中的顺序,改变了输入。 However, considering the same method works for other tables, I tend to think there is an issue with this table.但是,考虑到相同的方法适用于其他表,我倾向于认为该表存在问题。 I queried the sqlite db for a list of all tables, it gives a list with the MessageEntityXREF in it.我在 sqlite db 中查询了所有表的列表,它提供了一个包含 MessageEntityXREF 的列表。 I installed DB browser for SQLite and the table is here with the MessageID and EntityId columns.我为 SQLite 安装了 DB 浏览器,这里的表包含 MessageID 和 EntityId 列。

NOTE: there is a table called "Entities" in the database which could lead to confusion with TypeORM tendency to use @Entity etc...注意:数据库中有一个名为“实体”的表,这可能会导致与 TypeORM 使用 @Entity 等的倾向混淆......

With nestJS, I created an entity for this table and I inject the repository in a custom service used then by a function which find in this repository:使用 nestJS,我为此表创建了一个实体,并将存储库注入自定义服务中,然后由在此存储库中找到的函数使用:

Messages Service消息服务

@Entity export class MessageEntityXREF {

@PrimaryColumn() MessageId: number;

 @Column() EntityID: number; } 


custom service

@InjectRepository(MessageEntityXREF)
private readonly messageEntityRepository: Repository<                 MessageEntityXREF>

async getMessagesByEntityId(id: number): Promise<any[]> {
try {
  return await this.messageEntityRepository.find({
    select: ['MessageId','EntityId'],
    where: [
    { EntityId: id}
    ]
 });
} catch (error) {
  throw error;
  }
 }
}

I would expect a list of all MessageId with a given EntityID.我希望有一个包含给定 EntityID 的所有 MessageId 的列表。

When no ID is given, I get an empty array (expected).当没有给出 ID 时,我得到一个空数组(预期)。

When I give an ID, I get a 500 error from the server with "QueryFailedError: SQLITE_ERROR: no such table: message_entity_xref"当我提供 ID 时,我从服务器收到 500 错误,其中包含“QueryFailedError: SQLITE_ERROR: no such table: message_entity_xref”

Definition of entity实体的定义

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class MessageEntityXREF {
  @PrimaryGeneratedColumn()
  MessageId: number;

  @Column()
  EntityId: number;
}

Messages Module消息模块

@Module({
  imports: [
    CacheModule.register(),
    TypeOrmModule.forFeature([Messages, MessageEntityXREF]),
  ],
  providers: [MessagesService, MessagesResolver],
  controllers: [MessagesController],
})
export class MessagesModule {}

and the ormconfig.json和 ormconfig.json

{
  "skip": false,
  "name": "default",
  "type": "sqlite",
  "database": "./sqliteDB/solve.rld",
  "entities": ["src/**/**.entity{.ts,.js}"],
  "logging": false
}

In my case, the cause was different.就我而言,原因是不同的。 In order to make TypeORM work with SQLite I needed to add synchronize: true to TypeORM options:为了使 TypeORM 与 SQLite 一起工作,我需要在 TypeORM 选项中添加synchronize: true

    TypeOrmModule.forRoot({
      type: 'sqlite',
      database: path.resolve(__dirname, '../db.sqlite3'),
      entities: [User, Post],
      logging: true,
      synchronize: true,
    })

Found it. 找到了。 Weird flex. 奇怪的弹性。

In my entity definition, I let @Entity() empty 在我的实体定义中,我让@Entity()为空

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class MessageEntityXREF {
  @PrimaryGeneratedColumn()
  MessageId: number;

  @Column()
  EntityId: number;
}

and then typeORM is requesting the sqlite db for the table "message_entity_xref". 然后typeORM正在向sqlite db请求表“ message_entity_xref”。 Instead of MessageEntityXREF. 而不是MessageEntityXREF。

Forcing it like that: 强迫这样:

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity('MessageEntityXREF')
export class MessageEntityXREF {
  @PrimaryGeneratedColumn()
  MessageId: number;

  @Column()
  EntityId: number;
}

And it works like a charm. 它就像一种魅力。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 TypeORM 和 NodeJS 中连接表 - Join Tables in TypeORM & NodeJS 如何使用 Laravel controller 中的查询从链接到具有主键和外键的另一个表的表中删除一行? - How to delete a row from table linked to another tables with primary and foreign keys with query in Laravel controller? 如何以csv或其他格式从nativescript中导出和导入所选表或从sqlite数据库中完成数据库 - how to export and import selected tables or complete DB from sqlite database in nativescript in csv or some other format 在“ <input> 无法访问。 是否有一种简单的方法使其可访问(JS),还是我必须放弃它? - Using “required” on an <input> isn't accessible. Is there an easy way to make it accessible (JS) or do I have to abandon it? NestJS 与 TypeORM:如何使用 TypeORM 连接数据库并使实例可访问 - NestJS with TypeORM: How to connect a DB using TypeORM and make the instance accessible 更改html表中的一个字段,将更改该表中的特定元素。 不在其他表中 - Change of one field in the html table, changes a specific element inside that table. Not in other tables 在连接的 DynamoDB 表中传播更改的正确方法是什么 - What is the correct way to propogate changes in connected DynamoDB tables 我是否必须在第一批sqlite db中创建所有表? - Do I have to create all tables at first population of sqlite db? 在隐藏其他表的同时切换单个表 - Toggle a single table while hiding other tables 折叠具有特定表ID的表? JavaScript的 - Collapse Tables with specific Table ID? JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM