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

Rap*_*elD 1 javascript sqlite typeorm nestjs

大家晚上好

我正在尝试通过 TypeORM 查询存储在 sqlite 数据库上的名为“MessageEntityXREF”的表。

数据库已连接,其他表可访问。

When I try to Get on this specific table: "QueryFailedError: SQLITE_ERROR: no such table: 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. I installed DB browser for SQLite and the table is here with the MessageID and EntityId columns.

NOTE: there is a table called "Entities" in the database which could lead to confusion with TypeORM tendency to use @Entity etc...

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:

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;
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

I would expect a list of all MessageId with a given EntityID.

When no ID is given, I get an empty array (expected).

When I give an ID, I get a 500 error from the server with "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;
}
Run Code Online (Sandbox Code Playgroud)

Messages Module

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

and the ormconfig.json

{
  "skip": false,
  "name": "default",
  "type": "sqlite",
  "database": "./sqliteDB/solve.rld",
  "entities": ["src/**/**.entity{.ts,.js}"],
  "logging": false
}
Run Code Online (Sandbox Code Playgroud)

ari*_*pen 8

就我而言,原因是不同的。为了使 TypeORM 与 SQLite 一起工作,我需要添加synchronize: true到 TypeORM 选项:

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