I was testing out a index + unique constraint combination on a collection in the Fauna dashboard shell the other day and I ran into some unexpected behavior. The full shell session is below but the TL:DR is that it appears to me that even though the index showed as queryable, and the unique constraint showed as ready, neither were being enforced. It appears that I was able to create multiple documents that violated the unique constraint, and that the index wasn’t returning any results when it should. Near the end of the session it appears that both started working as expected.
Did I fat finger something or am I missing something? Are the words token
and key
reserved in some fashion that makes this behavior expected?
let created_key = Key.create({role: "admin", data: {name: "test", token: "asdf"}})
unique_keys_by_token.create({token: "asdf", key: created_key})
created_key
{
id: "380980134326304836",
coll: Key,
ts: Time("2023-11-10T04:16:59.210Z"),
role: "admin",
secret: "fnAFSYNp5QAARH5KiW1l2_zNwBTlQfilypEcVkcZ",
data: {
name: "test",
token: "asdf"
}
}
unique_keys_by_token.by_token("asdf")
{
data: []
}
unique_keys_by_token.all()
{
data: [
{
id: "380980134334693444",
coll: unique_keys_by_token,
ts: Time("2023-11-10T04:16:59.210Z"),
token: "asdf",
key: Key.byId("380980134326304836")
}
]
}
unique_keys_by_token.by_token("asdf")
{
data: []
}
unique_keys_by_token.definition
{
name: "unique_keys_by_token",
coll: Collection,
ts: Time("2023-11-10T04:15:36.700Z"),
indexes: {
by_token: {
terms: [
{
field: "token"
}
],
queryable: true,
status: "complete"
}
},
constraints: [
{
unique: [
"token"
],
status: "active"
}
]
}
unique_keys_by_token.by_token("asdf")
{
data: []
}
Key.all()
{
data: [
{
id: "370288737620328516",
coll: Key,
ts: Time("2023-07-15T04:01:48.760Z"),
role: "server",
data: {
name: "appsync_test"
}
},
{
id: "380980134326304836",
coll: Key,
ts: Time("2023-11-10T04:16:59.210Z"),
role: "admin",
data: {
name: "test",
token: "asdf"
}
}
]
}
let created_key = Key.create({role: "admin", data: {name: "test", token: "asdf"}})
unique_keys_by_token.create({token: "asdf", key: created_key})
created_key
{
id: "380982657229520961",
coll: Key,
ts: Time("2023-11-10T04:57:05.240Z"),
role: "admin",
data: {
name: "test",
token: "asdf"
},
secret: "fnAFSYW1TcAAQQxjDQiTF9gwToRij1pfBofsQWoP"
}
unique_keys_by_token.all()
{
data: [
{
id: "380980134334693444",
coll: unique_keys_by_token,
ts: Time("2023-11-10T04:16:59.210Z"),
token: "asdf",
key: Key.byId("380980134326304836")
},
{
id: "380982657236860993",
coll: unique_keys_by_token,
ts: Time("2023-11-10T04:57:05.240Z"),
token: "asdf",
key: Key.byId("380982657229520961")
}
]
}
unique_keys_by_token.definition
{
name: "unique_keys_by_token",
coll: Collection,
ts: Time("2023-11-10T04:15:36.700Z"),
indexes: {
by_token: {
terms: [
{
field: "token"
}
],
queryable: true,
status: "complete"
}
},
constraints: [
{
unique: [
"token"
],
status: "active"
}
]
}
unique_keys_by_token.create({token: "asdf"})
constraint_failure
error: Failed unique constraint.
constraint failures:
token: Failed unique constraint
at *query*:1:28
|
1 | unique_keys_by_token.create({token: "asdf"})
| ^^^^^^^^^^^^^^^^^
|
unique_keys_by_token.create({token: "asdf", key: Key.all().first()})
constraint_failure
error: Failed unique constraint.
constraint failures:
token: Failed unique constraint
at *query*:1:28
|
1 | unique_keys_by_token.create({token: "asdf", key: Key.all().first()})
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
unique_keys_by_token.by_token("asdf")
{
data: [
{
id: "380982657236860993",
coll: unique_keys_by_token,
ts: Time("2023-11-10T04:57:05.240Z"),
token: "asdf",
key: Key.byId("380982657229520961")
}
]
}
unique_keys_by_token.all()
{
data: [
{
id: "380980134334693444",
coll: unique_keys_by_token,
ts: Time("2023-11-10T04:16:59.210Z"),
token: "asdf",
key: Key.byId("380980134326304836")
},
{
id: "380982657236860993",
coll: unique_keys_by_token,
ts: Time("2023-11-10T04:57:05.240Z"),
token: "asdf",
key: Key.byId("380982657229520961")
}
]
}