【发布时间】:2020-04-30 20:17:41
【问题描述】:
我正在使用带有 TypeORM(Postgres 数据库)的 NestJs,我想定期从数据库中删除我过期的令牌。这是我的令牌实体:
@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)
如果您想查看存储库,这里有
如果您想试用 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
有人知道如何解决这个问题吗?
【问题讨论】:
标签: postgresql typeorm