Ref

Ref( schema_ref, id )
Ref( schema_ref, id )
Ref( schema_ref, id )
Ref( schema_ref, id )
ref( schema_ref, id )
ref schema_ref, id
Ref( schema_ref, id )
Ref( schema_ref, id )

Description

The Ref function returns the reference for a specific document, based in the provided id, within a specific database, implicitly based on the schema_ref. Typically, this would used to get the reference for a user document in a collection.

Everything in FaunaDB is described by a document. The documents that describe fundamental FaunaDB infrastructure are created by the system and are called schema documents, or core schemas. Any other documents are user-created, and are called user documents.

References are like primary keys in other database systems, in that they are used to provide a pointer to a unique document.

References to core schema documents, that you pass in via schema_ref, are provided by their respective functions:

The Ref command does not verify that the schema_ref, or the id within the schema_ref, exists. This means that you can use Ref to create a reference to a non-existent document in a non-existent schema.

This feature is particularly useful when creating parent-child relationships. The parent record can be created with a list of non-existent child references before the child records are created.

You have to use this feature carefully or queries that use such references could fail.

Parameters

Argument Type Definition and Requirements

schema_ref

Ref

A reference to a specific schema document to which the desired document belongs.

id

String

The identifier for the specific reference.

Returns

A reference to the document in the specified schema_ref that has the specified id.

Examples

The following query returns the reference to a specific document in the "spells" collection:

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{ "ref": { "class": "spells" }, "id": "181388642046968320" }'
client.Query(
  Ref(Collection("spells"), "181388642046968320")
);
System.out.println(
    client.query(
        Ref(Collection("spells"), "181388642046968320")
    ).get()
);
result, _ := client.Query(
  f.RefClass(f.Collection("spells"), "181388642046968320")
)

fmt.Println(result)
client.query(
  Ref(Collection("spells"), "181388642046968320")
)
print(client.query(
  q.ref(q.collection("spells"), "181388642046968320")
))
puts $client.query {
  ref(collection("spells"), '181388642046968320')
}
client.query(
  Ref(Collection("spells"), "181388642046968320")
)
client.query(
  q.Ref(q.Collection('spells'), '181388642046968320')
)
.then((ret) => console.log(ret))
{ "resource": { "@ref": "classes/spells/181388642046968320" } }
Ref(id=181388642046968320, class=Ref(id=spells, class=Ref(id=classes)))
Ref(id=181388642046968320, class=Ref(id=spells, class=Ref(id=classes)))
{181388642046968320 0xc0000ba680 <nil>}
Ref(id=181388642046968320, class=Ref(id=spells, class=Ref(id=classes)))
Ref(id=181388642046968320,class=Ref(id=spells,class=Ref(id=classes)))
Ref(id=181388642046968320, class=Ref(id=spells, class=Ref(id=classes)))
Ref(id=181388642046968320, class=Ref(id=spells, class=Ref(id=classes)))
Ref(Class("spells"), "181388642046968320")

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve! documentation@fauna.com

Thank you for your feedback!