表“令牌”的缺少FROM子句条目

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了表“令牌”的缺少FROM子句条目相关的知识,希望对你有一定的参考价值。

我正在将NestJs与TypeORM(Postgres数据库)一起使用,我希望定期从数据库中删除过期的令牌。这是我的令牌实体:

@Entity('Token')
export class Token extends BaseEntity 
  @PrimaryColumn()
  public encoding: string;

  @Column()
  public expiresOn: Date;

我想从数据库中删除已过期的每个令牌。因此,expiresOn返回的日期比当前日期早。

当我打电话时

    await this.tokensRepository
      .createQueryBuilder('token')
      .delete()
      .from(Token) // I also tried it with 'token'
      .where('token.expiresOn <= :currentDate',  currentDate: new Date() )
      .execute();

我收到以下错误:

query: DELETE FROM "Token" WHERE token.expiresOn <= $1 -- PARAMETERS: ["2020-01-11T16:37:21.964Z"]
query failed: DELETE FROM "Token" WHERE token.expiresOn <= $1 -- PARAMETERS: ["2020-01-11T16:37:21.964Z"]
error: error: missing FROM-clause entry for table "token"
    at Connection.parseE (/home/matthias/Projects/nest-api/node_modules/pg/lib/connection.js:604:13)
    at Connection.parseMessage (/home/matthias/Projects/nest-api/node_modules/pg/lib/connection.js:403:19)
    at Socket.<anonymous> (/home/matthias/Projects/nest-api/node_modules/pg/lib/connection.js:123:22)
    at Socket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:182:23) 
  name: 'error',
  length: 116,
  severity: 'ERROR',
  code: '42P01',
  detail: undefined,
  hint: undefined,
  position: '27',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'parse_relation.c',
  line: '3240',
  routine: 'errorMissingRTE'

(node:18572) UnhandledPromiseRejectionWarning: QueryFailedError: missing FROM-clause entry for table "token"
    at new QueryFailedError (/home/matthias/Projects/nest-api/node_modules/typeorm/error/QueryFailedError.js:11:28)
    at Query.callback (/home/matthias/Projects/nest-api/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:176:38)
    at Query.handleError (/home/matthias/Projects/nest-api/node_modules/pg/lib/query.js:145:17)
    at Connection.connectedErrorMessageHandler (/home/matthias/Projects/nest-api/node_modules/pg/lib/client.js:214:17)
    at Connection.emit (events.js:210:5)
    at Socket.<anonymous> (/home/matthias/Projects/nest-api/node_modules/pg/lib/connection.js:128:12)
    at Socket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at Socket.Readable.push (_stream_readable.js:224:10)

如果您想看一下存储库,那里有

https://github.com/matthiashermsen/nest-api/blob/master/src/authentications/expired-tokens-removal.service.ts#L60

如果您想尝试NestJs API,这是我的.env文件配置

SERVER_PORT = 3000

DATABASE_TYPE = postgres
DATABASE_HOST = localhost
DATABASE_PORT = 5432
DATABASE_USERNAME = postgres
DATABASE_PASSWORD = postgres
DATABASE_NAME = api
DATABASE_LOGGING = true
DATABASE_SYNCHRONIZE = true

NODE_ENV = development

AUTHENTICATION_SALT_ROUNDS = 12
TOKEN_SECRET = secret
TOKEN_EXPIRATION_IN_SECONDS = 36000
EXPIRED_TOKEN_REMOVAL_IN_MILLISECONDS = 5000

有人知道如何解决此问题吗?

答案

如果将标识符括在SQL中的引号中,则必须完全按照存储的方式编写标识符。名称"Token"不等于token。您可能应该将实体声明更改为@Entity('token')

另一答案

我通过从令牌实体中的实体装饰器中删除名称来解决它。

@Entity()
export class Token extends BaseEntity 
  @PrimaryColumn()
  public encoding: string;

  @Column()
  public expiresOn: Date;

接下来,我从文档https://github.com/typeorm/typeorm/blob/master/docs/delete-query-builder.md#delete-using-query-builder中获取了示例,并创建了此删除查询,这对我来说似乎很好用

await this.tokensRepository
  .createQueryBuilder()
  .delete()
  .from(Token)
  .where('expiresOn <= :currentDate',  currentDate: new Date() )
  .execute();

以上是关于表“令牌”的缺少FROM子句条目的主要内容,如果未能解决你的问题,请参考以下文章

如何修复“缺少表的 FROM 子句条目”?

Postgres:缺少表的 FROM 子句条目

错误:缺少表“用户”的 FROM- 子句条目

查询中的错误 - 缺少表的 FROM 子句条目 - SQL

表 »rowtype« 缺少 FROM 子句条目

返回导致错误:缺少表的 FROM 子句条目