与PostgreSQL一起使用原始SQL时,TypeORM:QueryFailedError

时间:2019-01-27 20:40:38

标签: postgresql typescript typeorm

我浏览了NPM上的TypeORM快速入门,网址为:https://www.npmjs.com/package/typeorm

我添加了一些代码来尝试原始SQL查询,但我立即收到QueryFailedError。

我很好奇如何在下面的示例中使用原始SQL?

我验证了表和实体是使用TypeORM的connection.manager的find方法创建的,并且我还通过pgAdmin手动检查了表。为了达到良好的效果,我使用pgAdmin生成了初始SQL,然后对其进行了最小程度的修改。

我还验证了ormconfig.json文件中的syncy设置为true。

index.ts

import "reflect-metadata";
import {createConnection} from "typeorm";
import {User} from "./entity/User";

createConnection().then(async connection => {
    const user = new User();
    user.firstName = "Timber";
    user.lastName = "Saw";
    user.age = 25;
    await connection.manager.save(user);

    let data = await connection.query(
        `SELECT firstName FROM public.user;`
    );
}).catch(error => console.log(error));

来自堆栈跟踪的错误和错误信息:

QueryFailedError: column "firstname" does not exist
...
message: 'column "firstname" does not exist',
name: 'QueryFailedError',
length: 108,
severity: 'ERROR',
code: '42703',
detail: undefined,
hint: undefined,
position: '8',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_relation.c',
line: '3293',
routine: 'errorMissingColumn',
query: 'SELECT firstName FROM user;',

0 个答案:

没有答案