【发布时间】:2021-07-02 12:30:03
【问题描述】:
当使用 typeorm 存储库函数 findOne 或 QueryBuilder 时:
constructor (
@InjectRepository(EntityName)
private entityNameRepository: Repository<EntityName>,
) {}
...
const qb = this.entityNameRepository.createQueryBuilder('e');
qb.where('e.originKey=:origin_key', {
origin_key: originKey,
});
const entityName: EntityName = await qb.getOne();
// or
const entityName: EntityName = await this.entityNameRepository.findOne({
originKey: originKey,
});
我收到以下错误消息:
QueryFailedError: Unknown column 'originKey' in 'where clause'
at new QueryFailedError (/Users/{project}/node_modules/typeorm/error/QueryFailedError.js:11:28)
at Query.onResult (/Users/{project}/node_modules/typeorm/driver/mysql/MysqlQueryRunner.js:216:45)
at Query.execute (/Users/{project}/node_modules/mysql2/lib/commands/command.js:30:14)
at PoolConnection.handlePacket (/Users/{project}/node_modules/mysql2/lib/connection.js:425:32)
at PacketParser.Connection.packetParser.p [as onPacket] (/Users/{project}/node_modules/mysql2/lib/connection.js:75:12)
at PacketParser.executeStart (/Users/{project}/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.Connection.stream.on.data (/Users/{project}/node_modules/mysql2/lib/connection.js:82:25)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:287:12)
at readableAddChunk (_stream_readable.js:268:11)
我的实体看起来像:
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { UuidGenerator } from '../adapter/uuid-generator';
@Entity()
export class EntityName extends BaseEntity {
@PrimaryGeneratedColumn({ name: 'id' })
id: number;
@Column({ name: 'uuid', unique: true, nullable: false })
uuid: string;
@Column({ name: 'is_active', default: true })
isActive: boolean;
@Column({ name: 'origin_key', unique: true, nullable: true })
originKey: string;
}
而MySQL中的列名为origin_key
当我强制使用属性名称 origin_key 进行查询时,它也会失败,并显示一个不同的错误,指出实体中没有给出该字段,由于命名,这是有道理的。
【问题讨论】:
-
你试过
e.origin_key吗? -
@MicaelLevi 是的,然后它也会失败,并显示一条不同的错误消息,告诉
origin_key不是我实体的已知属性