用户定义的功能在Better-Sqlite中不起作用3

发布于 2025-02-04 08:54:23 字数 2413 浏览 5 评论 0 原文

我有一个 node.js 服务器,可以连接并初始化 sqlite 数据库。我正在使用 Better-Sqlite3 库。

但是服务器从未启动。它给出以下错误:

“在此处输入图像描述”

架构:

CREATE TABLE IF NOT EXISTS users (

    user_id       text        NOT NULL DEFAULT gen_random_uuid() ,
    email         text UNIQUE NOT NULL                           ,
    full_name     text        NOT NULL                           ,
    profile_photo text                 DEFAULT ''                ,    
    passhash      text        NOT NULL                           ,
    created_time  int         NOT NULL DEFAULT now()             ,
    settings      text        NOT NULL DEFAULT '{}'              ,       

    CHECK ( length ( user_id ) = 36 ) ,
    CHECK ( length ( email     ) >= 3  AND length ( email     ) <= 100 ) ,
    CHECK ( length ( full_name ) >= 1  AND length ( full_name ) <= 100 ) ,
    CHECK ( length ( passhash  ) >= 8  AND length ( passhash  ) <= 100 ) ,
    CHECK ( length ( profile_photo  ) >= 0  AND length ( profile_photo  ) <= 1000 ) , 

    PRIMARY KEY ( user_id )

) STRICT;

的一部分

function setup (config) {
    db = new Database('colube.db', { verbose: console.log }); 
    db.pragma('journal_mode = WAL');

    if (!isTablesCreated(db)) {
        addFns(db);

        let path      = getPath('db/migrations/V0001__sqlite3_initial_version.sql');
        let migration = fs.readFileSync(path, 'utf8');

        db.exec(migration);
    }
}

node.js 服务器代码

function addFns (db) {

    db.function('gen_random_uuid', () => {
        return v4();
    }); 
    
    db.function('now', () => {
        return Date.now();
    }); 
}   

表格 sqlite 迁移文件。删除使用 gen_random_uuid 现在(用户定义的函数)时,迁移工作正常。

由于 better-sqlite3 的文档清楚地表明用户定义的功能,我不知道如何解决此问题。

I have a node.js server that connects and initializes a sqlite database. I am using the better-sqlite3 library.

But the server never gets started. It gives below error:

enter image description here

Tables schema:

CREATE TABLE IF NOT EXISTS users (

    user_id       text        NOT NULL DEFAULT gen_random_uuid() ,
    email         text UNIQUE NOT NULL                           ,
    full_name     text        NOT NULL                           ,
    profile_photo text                 DEFAULT ''                ,    
    passhash      text        NOT NULL                           ,
    created_time  int         NOT NULL DEFAULT now()             ,
    settings      text        NOT NULL DEFAULT '{}'              ,       

    CHECK ( length ( user_id ) = 36 ) ,
    CHECK ( length ( email     ) >= 3  AND length ( email     ) <= 100 ) ,
    CHECK ( length ( full_name ) >= 1  AND length ( full_name ) <= 100 ) ,
    CHECK ( length ( passhash  ) >= 8  AND length ( passhash  ) <= 100 ) ,
    CHECK ( length ( profile_photo  ) >= 0  AND length ( profile_photo  ) <= 1000 ) , 

    PRIMARY KEY ( user_id )

) STRICT;

Part of node.js server code:

function setup (config) {
    db = new Database('colube.db', { verbose: console.log }); 
    db.pragma('journal_mode = WAL');

    if (!isTablesCreated(db)) {
        addFns(db);

        let path      = getPath('db/migrations/V0001__sqlite3_initial_version.sql');
        let migration = fs.readFileSync(path, 'utf8');

        db.exec(migration);
    }
}

My user-defined functions:

function addFns (db) {

    db.function('gen_random_uuid', () => {
        return v4();
    }); 
    
    db.function('now', () => {
        return Date.now();
    }); 
}   

The error is thrown when executing sqlite migration file . Migration works fine when removing the use of gen_random_uuid and now(user-defined functions).

Since the document of better-sqlite3 states clear that it supports user-defined functions, I don't know how to solve this problem.

better-sqlite3 document: user defined function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深白境迁sunset 2025-02-11 08:54:23

我从 better-sqlite3 github上得到了答复,该链接解决了我的问题,答复的链接:帮助:语法错误。以下是答复的内容:

问题是您正在使用无效的语法。正确的语法被描述为:

明确的默认子句可以指定默认值是null,字符串常数,斑点常数,签名数或任何括号中包含的任何常数表达式。默认值也可能是与特殊情况无关的关键字current_time,current_date或current_timestamp。

换句话说,您必须将gen_random_uuid()包含在括号中,例如(gen_random_uuid())。现在必须执行相同的操作() - &gt; (现在())。

I got a reply from better-sqlite3 community on github that solved my problem, the link of the reply: Help: syntax error. Below is the content of the reply:

The problem is that you're using invalid syntax. The correct syntax is described as:

An explicit DEFAULT clause may specify that the default value is NULL, a string constant, a blob constant, a signed-number, or any constant expression enclosed in parentheses. A default value may also be one of the special case-independent keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP.

In other words, you must enclose gen_random_uuid() in parenthesis, like (gen_random_uuid()). The same must be done for now() -> (now()).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文