delete() 从脚本和解释器给出不同的结果。为什么?
delete() from the script and the interpretator gives a different result. Why?
来自脚本的函数:
function test_delete(val)
local result = box.space.upbx_test_redirections.index.name_index:delete(val)
return result
end
我得到:"message":"Get() doesn't support partial keys and non-unique indexes"
。
如果我从口译员那里打电话,一切都很好,我得到了元组。选项我的索引
name_index: 3: &3
unique: true
parts:
- type: string
is_nullable: false
fieldno: 4
id: 3
type: TREE
space_id: 517
name: name_index
bucket_id: *1
index_name: *3
ID: *0
secondary: *2
test:1234567890abcdefghijkl0987654321_uuid
的价值
创建代码 space:
local upbx_test_redirections = box.schema.space.create(
"upbx_test_redirections",
{
format = {
{"ID", "integer"},
{"bucket_id", "unsigned"},
{"test1", "string"},
{"test2", "string"},
{"test3", "string"},
{"test4", "string"},
{"test5", "string"},
{"test6", "string"},
{"test7", "string"},
{"test8", "string"},
{"test9", "integer"},
{"test10", "integer"},
},
if_not_exists = true,
}
)
和索引:
upbx_test_redirections:create_index(
"name_index",
{
parts = {"test2"},
unique = true,
if_not_exists = true,
}
)
解决方案:在所有情况下都需要更改。
我建议您再次修改架构。
我的假设 - 之前您创建了 non-unique 索引,然后将 unique=false
修改为 unique=true
。
tarantool> upbx_test_redirections:create_index(
> "name_index",
> {
> parts = {"test2"},
> unique = false,
> if_not_exists = true,
> }
> )
---
- unique: false
parts:
- type: string
is_nullable: false
fieldno: 4
id: 1
space_id: 512
type: TREE
name: name_index
...
tarantool> upbx_test_redirections:create_index(
> "name_index",
> {
> parts = {"test2"},
> unique = true,
> if_not_exists = true,
> }
> )
---
- unique: false -- unique option isn't changed
parts:
- type: string
is_nullable: false
fieldno: 4
id: 1
space_id: 512
type: TREE
name: name_index
- not created
...
请参阅我的示例,其中包含您可能会遇到的错误。
box.cfg{}
s = box.schema.space.create('test')
s:format({{name = 'id', type = 'unsigned'}, {name = 'value', type = 'string'}})
-- Unique primary index
s:create_index('pk', {unique = true, parts = {{field = 'id', is_nullable = false}}})
-- Unique secondary index
s:create_index('sk', {unique = true, parts = {{field = 'value', is_nullable = false}}})
-- Unique multipart index
s:create_index('multipart', {unique = true, parts = {{field = 'id', is_nullable = false}, {field = 'value', is_nullable = false}}})
-- Non-unique multipart indexes
s:create_index('multipart_non_unique', {unique = false, parts = {{field = 'id', is_nullable = false}, {field = 'value', is_nullable = false}}})
-- Test data
s:replace{1, '1'}
s:replace{2, '2'}
s:replace{3, '3'}
s:replace{4, '4'}
s:replace{5, '5'}
-- Examples with errors
box.space.test:delete({1}) -- OK
box.space.test.index['pk']:delete({2}) -- OK
box.space.test.index['pk']:delete(2) -- OK
box.space.test.index['pk']:delete(nil) -- Fail
box.space.test.index['sk']:delete('3') -- OK
box.space.test.index['sk']:delete({'3'}) -- OK
box.space.test.index['multipart']:delete({4}) -- Fail: Invalid key part count in an exact match (expected 2, got 1)
box.space.test.index['multipart']:delete({4, '4'}) -- OK
box.space.test.index['multipart_non_unique']:delete({5}) -- Fail: Get() doesn't support partial keys and non-unique indexes
box.space.test.index['multipart_non_unique']:delete({5, '5'}) -- Fail: Get() doesn't support partial keys and non-unique indexes
如果问题仍然存在,请随时 report a bug
来自脚本的函数:
function test_delete(val)
local result = box.space.upbx_test_redirections.index.name_index:delete(val)
return result
end
我得到:"message":"Get() doesn't support partial keys and non-unique indexes"
。
如果我从口译员那里打电话,一切都很好,我得到了元组。选项我的索引
name_index: 3: &3
unique: true
parts:
- type: string
is_nullable: false
fieldno: 4
id: 3
type: TREE
space_id: 517
name: name_index
bucket_id: *1
index_name: *3
ID: *0
secondary: *2
test:1234567890abcdefghijkl0987654321_uuid
的价值创建代码 space:
local upbx_test_redirections = box.schema.space.create(
"upbx_test_redirections",
{
format = {
{"ID", "integer"},
{"bucket_id", "unsigned"},
{"test1", "string"},
{"test2", "string"},
{"test3", "string"},
{"test4", "string"},
{"test5", "string"},
{"test6", "string"},
{"test7", "string"},
{"test8", "string"},
{"test9", "integer"},
{"test10", "integer"},
},
if_not_exists = true,
}
)
和索引:
upbx_test_redirections:create_index(
"name_index",
{
parts = {"test2"},
unique = true,
if_not_exists = true,
}
)
解决方案:在所有情况下都需要更改。
我建议您再次修改架构。
我的假设 - 之前您创建了 non-unique 索引,然后将 unique=false
修改为 unique=true
。
tarantool> upbx_test_redirections:create_index(
> "name_index",
> {
> parts = {"test2"},
> unique = false,
> if_not_exists = true,
> }
> )
---
- unique: false
parts:
- type: string
is_nullable: false
fieldno: 4
id: 1
space_id: 512
type: TREE
name: name_index
...
tarantool> upbx_test_redirections:create_index(
> "name_index",
> {
> parts = {"test2"},
> unique = true,
> if_not_exists = true,
> }
> )
---
- unique: false -- unique option isn't changed
parts:
- type: string
is_nullable: false
fieldno: 4
id: 1
space_id: 512
type: TREE
name: name_index
- not created
...
请参阅我的示例,其中包含您可能会遇到的错误。
box.cfg{}
s = box.schema.space.create('test')
s:format({{name = 'id', type = 'unsigned'}, {name = 'value', type = 'string'}})
-- Unique primary index
s:create_index('pk', {unique = true, parts = {{field = 'id', is_nullable = false}}})
-- Unique secondary index
s:create_index('sk', {unique = true, parts = {{field = 'value', is_nullable = false}}})
-- Unique multipart index
s:create_index('multipart', {unique = true, parts = {{field = 'id', is_nullable = false}, {field = 'value', is_nullable = false}}})
-- Non-unique multipart indexes
s:create_index('multipart_non_unique', {unique = false, parts = {{field = 'id', is_nullable = false}, {field = 'value', is_nullable = false}}})
-- Test data
s:replace{1, '1'}
s:replace{2, '2'}
s:replace{3, '3'}
s:replace{4, '4'}
s:replace{5, '5'}
-- Examples with errors
box.space.test:delete({1}) -- OK
box.space.test.index['pk']:delete({2}) -- OK
box.space.test.index['pk']:delete(2) -- OK
box.space.test.index['pk']:delete(nil) -- Fail
box.space.test.index['sk']:delete('3') -- OK
box.space.test.index['sk']:delete({'3'}) -- OK
box.space.test.index['multipart']:delete({4}) -- Fail: Invalid key part count in an exact match (expected 2, got 1)
box.space.test.index['multipart']:delete({4, '4'}) -- OK
box.space.test.index['multipart_non_unique']:delete({5}) -- Fail: Get() doesn't support partial keys and non-unique indexes
box.space.test.index['multipart_non_unique']:delete({5, '5'}) -- Fail: Get() doesn't support partial keys and non-unique indexes
如果问题仍然存在,请随时 report a bug