在surrealdb上调用具有多个参数的函数

huangapple go评论55阅读模式
英文:

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 recorduser: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 recorduser: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.

huangapple
  • 本文由 发表于 2023年6月26日 00:16:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定