英文:
Calling function with multi argument on surrealdb
问题
以下是翻译好的部分:
我想在surrealdb中定义一个函数,以便以后能够调用它。
为什么调用这个函数(fn::insert_user(...);
)失败了?
表格:
定义表格 user SCHEMAFULL;
定义字段 email 在表格 user 上 TYPE string
ASSERT $value != NONE AND is::email($value);
定义索引 email_unique_index 在表格 user 上 COLUMNS email UNIQUE;
定义字段 username 在表格 user 上 TYPE string;
定义索引 username_unique_index 在表格 user 上 COLUMNS username UNIQUE;
定义字段 name 在表格 user 上 TYPE string;
定义字段 hash 在表格 user 上 TYPE string
ASSERT $value != NONE;
定义字段 created_at 在表格 user 上 TYPE datetime
ASSERT $value != NONE;
函数:
定义函数 fn::insert_user(
$email: string,
$username: string,
$hash: string,
$name: string
) {
RETURN INSERT INTO user {
email: $email,
username: $username,
hash: $hash,
name: $name,
created_at: time::now()
};
};
调用函数:
RETURN fn::insert_user('user@surrealdb.com', 'user0', 'hash', 'name');
错误信息:
数据库出现问题:无法反序列化二进制响应:未知的变体
Database index
email_unique_indexalready contains 'user@surrealdb.com', with record
user:hz34bz2kroymoryx1sld``, 期望是其中之一None
,Null
,False
,True
,Number
,Strand
,Duration
,Datetime
,Uuid
,Array
,Object
,Geometry
,Bytes
,Param
,Idiom
,Table
,Thing
,Model
,Regex
,Block
,Range
,Edges
,Future
,Constant
,Function
,Subquery
,Expression
请注意,用户表格是空的。
英文:
I want to define function in surrealdb to be able to call it later.
Why calling this function (fn::insert_user(...);
) fails?
Table:
DEFINE TABLE user SCHEMAFULL;
DEFINE FIELD email ON TABLE user TYPE string
ASSERT $value != NONE AND is::email($value);
DEFINE INDEX email_unique_index ON TABLE user COLUMNS email UNIQUE;
DEFINE FIELD username ON TABLE user TYPE string;
DEFINE INDEX username_unique_index ON TABLE user COLUMNS username UNIQUE;
DEFINE FIELD name ON TABLE user TYPE string;
DEFINE FIELD hash ON TABLE user TYPE string
ASSERT $value != NONE;
DEFINE FIELD created_at ON TABLE user TYPE datetime
ASSERT $value != NONE;
Function:
DEFINE FUNCTION fn::insert_user(
$email: string,
$username: string,
$hash: string,
$name: string
) {
RETURN INSERT INTO user {
email: $email,
username: $username,
hash: $hash,
name: $name,
created_at: time::now()
};
};
Calling the function:
RETURN fn::insert_user('user@surrealdb.com', 'user0', 'hash', 'name');
The error:
> There was a problem with the database: Failed to deserialize a binary response: unknown variant Database index
email_unique_indexalready contains 'user@surrealdb.com', with record
user:hz34bz2kroymoryx1sld``, expected one of None
, Null
, False
, True
, Number
, Strand
, Duration
, Datetime
, Uuid
, Array
, Object
, Geometry
, Bytes
, Param
, Idiom
, Table
, Thing
, Model
, Regex
, Block
, Range
, Edges
, Future
, Constant
, Function
, Subquery
, Expression
Keep in mind that the user table is empty.
答案1
得分: 1
主要问题是自定义函数无法写入数据,只能读取数据。这个问题已在夜间分支中修复,将在下一个版本中发布。
英文:
There might be more than one thing going on here, but the main reason the custom function fails is that custom functions have not been able to write data, only read.
This has been fixed in the nightly branch and will come in the next release.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论