用户定义的功能在Better-Sqlite中不起作用3
我有一个 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
的文档清楚地表明用户定义的功能
,我不知道如何解决此问题。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从
better-sqlite3
github上得到了答复,该链接解决了我的问题,答复的链接:帮助:语法错误。以下是答复的内容:问题是您正在使用无效的语法。正确的语法被描述为:
换句话说,您必须将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:
In other words, you must enclose gen_random_uuid() in parenthesis, like (gen_random_uuid()). The same must be done for now() -> (now()).