[英]'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.