Search code examples
javascriptsqlitenestjstypeorm

'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.

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"

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

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

Messages Module

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

and the ormconfig.json

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

Solution

  • Found it. Weird flex.

    In my entity definition, I let @Entity() empty

    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". Instead of 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.