使用 CURRENT_TIMESTAMP 时很容易回答“表达式多于列”

问题描述 投票:0回答:1

我的打字稿代码正在尝试将记录插入 RDS 集群,它给了我 | 错误保存 mytable 记录时出错:错误:

error: INSERT has more expressions than target columns 

我相信我数得正确。为了重现问题,这大致是我正在运行的 SQL:


      const insertResult = await client.query<{
        my_id: number;
        last_updated_timestamp: Date;
      }>(
        `INSERT INTO mytable (
              f1,
              f2,
              f3,
              f4,
              f5,
              f6,
              f7,
              f8,
              f9,
              f10,
              f11,
              f12,
              f13,
              f14,
              f15,
              f16,
              f17,
              f18,
              f19,
              f20,
              last_updated_timestamp
            ) VALUES (
              $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, CURRENT_TIMESTAMP
            ) RETURNING my_id, last_updated_timestamp`,
        [
          'fvalue01',
          'fvalue02',
          'fvalue03',
          'fvalue04',
          'fvalue05,
          'fvalue06,
          'fvalue07,
          'fvalue08,
          'fvalue09,
          'fvalue10,
          'fvalue11,
          'fvalue12,
          'fvalue13,
          'fvalue14,
          'fvalue15,
          'fvalue16,
          'fvalue17,
          'fvalue18,
          'fvalue19,
          'fvalue20,
        ]

表 mytable 存在,列 my_id、f1-f20 和 last_updated_timestamp 存在。 my_id 是一个序列化的自动生成字段。我只想返回它在我的结果集中生成的任何数字。

我不确定编写这个库的人是否坚持所有字段都必须完全定义为 $ 位置值。类似但较小的 INSERT 正在工作。

我的 package.json 有以下依赖项片段:

  "devDependencies": {
    "@types/aws-lambda": "^8.10.146",
    "@types/jest": "^29.5.14",
    "@types/node": "^22.10.2",
    "@types/pg": "^8.11.10",
    "@typescript-eslint/eslint-plugin": "^7.1.1",
    "@typescript-eslint/parser": "^7.1.1",
    "aws-cdk": "^2.173.2",
    "esbuild": "^0.24.0",
    "eslint": "^8.57.0",
    "eslint-config-standard": "^17.1.0",
    "eslint-import-resolver-typescript": "^3.6.1",
    "eslint-plugin-import": "^2.29.1",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^6.1.1",
    "gulp": "^5.0.0",
    "gulp-typescript": "^6.0.0-alpha.1",
    "jest": "^29.7.0",
    "ts-jest": "^29.2.5",
    "ts-node": "^10.9.2",
    "tsc-watch": "^6.2.1",
    "typescript": "^5.7.2"
  },
  "dependencies": {
    "@aws-sdk/client-eventbridge": "^3.709.0",
    "@aws-sdk/client-ssm": "^3.709.0",
    "aws-cdk-lib": "^2.173.2",
    "aws-lambda": "^1.0.7",
    "constructs": "^10.4.2",
    "pg": "^8.13.1",
    "source-map-support": "^0.5.21",
    "zod": "^3.24.1"
  }
}

我的节点版本是:

$node --version
v20.17.0

我接到命令不要生成 Javascript 时间戳,而是依赖 Postgres/AWS/RDS 生成的时间戳,我认为这是导致所有问题的原因。请建议一种插入记录并返回 my_id 和 last_updated_timestamp 的方法。

node.js database postgresql amazon-web-services amazon-rds
1个回答
0
投票

我认为node.js可以将

CURRENT_TIMESTAMP
转换为字符串而不是sql变量。为了避免这种情况,您可以在查询中使用
current_timestamp()
now()
函数调用,这类似于
CURRENT_TIMESTAMP

const insertResult = await client.query<{
        my_id: number;
        last_updated_timestamp: Date;
      }>(
        `INSERT INTO mytable (
              f1,
              f2,
              f3,
              f4,
              f5,
              f6,
              f7,
              f8,
              f9,
              f10,
              f11,
              f12,
              f13,
              f14,
              f15,
              f16,
              f17,
              f18,
              f19,
              f20,
              last_updated_timestamp
            ) VALUES (
              $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, current_timestamp()
            ) RETURNING my_id, last_updated_timestamp`,
        [
          'fvalue01',
          'fvalue02',
          'fvalue03',
          'fvalue04',
          'fvalue05',
          'fvalue06',
          'fvalue07',
          'fvalue08',
          'fvalue09',
          'fvalue10',
          'fvalue11',
          'fvalue12',
          'fvalue13',
          'fvalue14',
          'fvalue15',
          'fvalue16',
          'fvalue17',
          'fvalue18',
          'fvalue19',
          'fvalue20',
        ]
© www.soinside.com 2019 - 2024. All rights reserved.