FQLv10 and creation of databases

Hello,

I’m running into an issue when trying to migrate some test fixtures from FQLv4 to FQLv10. I have an existing FQLv4 pytest fixture that generates test databases to run tests against:

@pytest.fixture
def test_db(fauna_admin_client):
    """Create a randomly named test child database for use in this test module and
    return its name.

    This will delete the test database after the session completes.
    """
    # Create the test database
    test_db_name = f"test-fluctuate-{uuid4()}"
    result = fauna_admin_client.query(query.create_database({"name": test_db_name}))

    # Yield the test database.
    yield result

    # Use a top level admin key to delete the child database.
    fauna_admin_client.query(query.delete(result["ref"]))

I converted it to FQLv10 like so:

@pytest.fixture
def test_db(fauna_admin_client):
    """Create a randomly named test child database for use in this test module and
    return its name.

    This will delete the test database after the session completes.
    """
    # Create the test database
    test_db_name = f"test-fluctuate-{uuid4()}"
    result = fauna_admin_client.query(
        fql("Database.create({name: ${test_db_name}})", test_db_name=test_db_name)
    )

    # Yield the test database.
    yield result.data

    # Use a top level admin key to delete the child database.
    fauna_admin_client.query(
        fql("Database.byName(${test_db_name}).delete()", test_db_name=test_db_name)
    )

The FQLv4 version works fine. However, when I run the FQLv10 version, I get the following error:

E                   fauna.errors.errors.QueryRuntimeError: 400: constraint_failure
E                   Failed to create document in collection Database.
E                   ---
E                   constraint failures: [ConstraintFailure(message='Invalid identifier.', name=None, paths=[['name']])]
E                   ---
E                   error: Failed to create document in collection Database.
E                   constraint failures:
E                     name: Invalid identifier.
E                   at *query*:1:16
E                     |
E                   1 | Database.create({name: <value>})
E                     |                ^^^^^^^^^^^^^^^^^
E                     |

The name it was trying to use in this case was test-fluctuate-bca9ec33-9c58-493b-9616-442655a647c6. Are there new limitations on DB names in FQLv10? There are no docs on the Database collection yet that I can find to confirm one way or the other.

Resources in FQL 10 are referenced directly by their name, so their names must be valid identifiers.

Your DB name is not a valid identifier, because it contains hyphens.

I agree that we need to improve documentation in this area. We talk about identifiers, but I don’t see that we require Database names to be valid identifiers. Thanks for bringing this to our attention! I have filed an internal ticket to review this.

@ptpaterson why are hyphens not allowed?

For example, when you create a Collection named “User”, future queries have the identifier User added to the global scope.

create a collection

Collection.create({ name: "User" })

User is not available in the global scope

User.create({ /*...*/ })

Not all schema are available in the global scope, for example Roles and child Databases are not, but the naming convention is still applied consistently across schema.

@ptpaterson thanks, but I’m not sure that answers my question: What’s the technical limitation as to why a - is not allowed, but a _ is, at least according to the identifier docs? From the outside looking in, it doesn’t seem much different to say foo-bar instead of foo_bar.

Why are hyphens not allowed in identifiers of other languages, but underscores are? FQL is code, and hyphens in code mean subtraction.

If you named a Collection “foo-bar” that suggests you would use that collection like

foo-bar.create({...})

But that won’t do what you want it to do. It will look for an identifier foo and try to perform subtraction with the result of bar.create({...})

To avoid this issue, schema names must be valid identifiers.

Note that Collection.byName("Foo") does not return the same type as Foo. Foo is a “companion” object that gives you the functions like .create. Collection.byName("Foo") is actually the definition document for the Collection. This means that you cannot do something like

// Invalid FQL
Collection.byName("Foo").create({
  title: "my foo document"
})
error: The function `create` does not exist on `Doc`
at *query*:1:28
  |
1 | Collection.byName("Foo").create({
  |                          ^^^^^^

Ah, I had incorrectly assumed whitespace would be necessary in order to distinguish subtraction from a name. Thanks for the details.

No problem! Thanks for the excellent questions and feedback. :smile:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.