supabase 函数无法访问一张表

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

我在supabase中有一个函数可以更新两个表(消息和游戏),我现在添加了第三个表(用户),由于某种原因它无法访问它,我添加了一个debug_logs表来弄清楚发生了什么(第 31 行)查询

INSERT INTO debug_logs (log_message) VALUES ('TEST ' || (SELECT SUM(score) FROM "Users"));

它返回 NULL,但如果我只是自己运行它(不在函数中),它会重新运行该值

真的不知道为什么,但似乎只有该函数无法访问用户表...

附上代码:

CREATE OR REPLACE FUNCTION resolve_message_b(world_id INT, guess TEXT, user_id UUID)
RETURNS INT AS $$
DECLARE
    message RECORD;
    total_words INT;
BEGIN
    -- Fetch the message based on the world_id
    SELECT * INTO message
    FROM "Messages"
    WHERE id = world_id;

    -- Increment resolveAttempts by 1
    UPDATE "Messages"
    SET "resolveAttempts" = COALESCE("resolveAttempts", 0) + 1
    WHERE id = world_id;

    -- Check if the guess is correct (case-insensitive)
    IF LOWER(message.content) = LOWER(guess) THEN
        -- Update isResolved to true
        UPDATE "Messages"
        SET "isResolved" = TRUE
        WHERE id = world_id;

        -- Increment game score by 100 and decrement totalWords by 1
        UPDATE "Games"
        SET score = score + 100,
            "totalWords" = "totalWords" - 1
        WHERE id = message."gameId";

        -- test into log_message
        INSERT INTO debug_logs (log_message) VALUES ('TEST ' || (SELECT SUM(score) FROM "Users"));

        -- Increment user score by 100
        UPDATE "Users"
        SET score = score + 100
        WHERE id = user_id;

        INSERT INTO debug_logs (log_message) VALUES ('Updated user score for user_id: ' || user_id);

        -- Fetch the updated totalWords
        SELECT "totalWords" INTO total_words
        FROM "Games"
        WHERE id = message."gameId";

        RETURN total_words;
    ELSE
        -- Fetch the current totalWords
        SELECT "totalWords" INTO total_words
        FROM "Games"
        WHERE id = message."gameId";

        RETURN total_words;
    END IF;

    EXCEPTION
    WHEN OTHERS THEN
        RAISE EXCEPTION 'Error occurred: %', SQLERRM;

END;
$$ LANGUAGE plpgsql;

运行查询。 - 它按预期工作

更改 RLS 以允许任何人进行所有更改,包括(公开的和匿名的)- 没有进行任何更改

非常感谢任何帮助!

postgresql plpgsql supabase sql-function supabase-database
1个回答
0
投票

我已经弄清楚了。我缺少用户表的权限...简单

© www.soinside.com 2019 - 2024. All rights reserved.