错误:缺少表的FROM子句条目 - Postgresql函数

时间:2016-09-28 08:30:41

标签: sql postgresql

我正在运行一个postgres查询,该查询调用一个函数来使用新记录更新数据库。我碰巧在这里收到错误:missing FROM-clause entry for table "create_trans"

createTransaction: function(id, sub_id, purchase_date, expiry, is_paid, latest_receipt, success, fail){
    db.query("select create_trans  ( $1, $2, to_timestamp($3), to_timestamp($4), $5, $6 )",
            [id, subscription_id, purchase_date, expiry, is_paid, latest_receipt])
        .then(success, fail)
},

上述查询函数调用postgres create_trans函数。

CREATE FUNCTION create_trans(transaction_id BIGINT, subscription_id BIGINT,purchase_date TIMESTAMPTZ, expiry TIMESTAMPTZ, is_paid BOOLEAN,latest_receipt TEXT ) RETURNS VOID AS
$$
BEGIN
    INSERT INTO transactions ( id, subscription_id, purchase_date, expiry, is_paid, latest_receipt ) VALUES ( create_trans.transaction_id, create_trans.subscription_id, create_trans.original_purchase_date, create_trans.expiry, create_trans.is_paid, create_trans.latest_receipt );
    UPDATE subscriptions SET updated_at = NOW(), expiry = create_trans.expiry, latest_receipt = create_trans.latest_receipt WHERE id = subscription_id;
    RETURN;
END;
$$
LANGUAGE plpgsql;

我最终收到此错误消息:

error: missing FROM-clause entry for table "create_trans"

知道为什么吗?

更新 我更新了create_trans函数的参数,使其与列名不同。

CREATE FUNCTION create_trans(p_transaction_id BIGINT, p_subscription_id BIGINT, p_purchase_date TIMESTAMPTZ, p_expiry TIMESTAMPTZ, p_is_paid BOOLEAN,p_latest_receipt TEXT ) RETURNS VOID AS
$$
BEGIN
    INSERT INTO transactions ( id, subscription_id, purchase_date, expiry, is_paid, latest_receipt ) VALUES ( p_transaction_id, p_subscription_id, p_original_purchase_date, p_expiry, p_is_paid, p_latest_receipt );
    UPDATE subscriptions SET updated_at = NOW(), expiry = p_expiry, latest_receipt = p_latest_receipt WHERE id = p_subscription_id;
    RETURN;
END;
$$
LANGUAGE plpgsql;

我仍然收到此错误消息:

{ error: missing FROM-clause entry for table "create_trans"
    at Connection.parseE (/Users/siddharthan64/pgbear/pgbear-gateway-service/node_modules/pg/lib/connection.js:539:11)
    at Connection.parseMessage (/Users/siddharthan64/pgbear/pgbear-gateway-service/node_modules/pg/lib/connection.js:366:17)
    at Socket.<anonymous> (/Users/siddharthan64/pgbear/pgbear-gateway-service/node_modules/pg/lib/connection.js:105:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at Socket.Readable.push (_stream_readable.js:130:10)
    at TCP.onread (net.js:542:20)
    name: 'error',
    length: 527,
    severity: 'ERROR',
    code: '42P01',
    detail: undefined,
    hint: undefined,
    position: undefined,
    internalPosition: '166',
    internalQuery: 'INSERT INTO transactions ( id, subscription_id, purchase_date, expiry, is_paid, latest_receipt ) VALUES ( create_trans.transaction_id, create_trans.subscription_id, create_trans.original_purchase_date, create_trans.expiry, create_trans.is_paid, create_trans.latest_receipt )',
  where: 'PL/pgSQL function create_trans(bigint,bigint,timestamp with time zone,timestamp with time zone,boolean,text) line 3 at SQL statement',
    schema: undefined,
    table: undefined,
    column: undefined,
    dataType: undefined,
    constraint: undefined,
    file: 'parse_relation.c',
    line: '3036',
    routine: 'errorMissingRTE' }

0 个答案:

没有答案