Schema

Xata databases are described by a schema. A schema describes each table in the database, and the relationships between tables. Xata schemas are expressed as code in JSON and used by our internal systems, as well as the Command Line Interface to work with your database.

Understanding the Schema

Let's look at a JSON representation of a sample Xata schema to understand how it describes a database.

{
  "formatVersion": "1.0",
  "tables": [
    {
      "name": "teams",
      "columns": [
        {
          "name": "name",
          "type": "string",
          "unique": true,
          "description": "Name of the team"
        },
        {
          "name": "labels",
          "type": "multiple"
        },
        {
          "name": "owner",
          "type": "link",
          "link": {
            "table": "users"
          }
        }
      ]
    },
    {
      "name": "users",
      "columns": [
        {
          "name": "email",
          "type": "email"
        },
        {
          "name": "full_name",
          "type": "string"
        },
        {
          "name": "address",
          "type": "object",
          "columns": [
            {
              "name": "street",
              "type": "string"
            },
            {
              "name": "zipcode",
              "type": "int"
            }
          ]
        },
        {
          "name": "team",
          "type": "link",
          "link": {
            "table": "teams"
          }
        }
      ]
    }
  ]
}

formatVersion

This field in a Xata schema describes which version of the Xata schema format we're using. This is usually auto-generated and something you'll not want to change manually.

tables

The tables field is an array (a collection) of objects, each representing a table. Every table has a unique name, and a set of columns. Each column is described by the following set of fields.

These fields are required:

  • name: A unique name for the column. Valid column names contain only alphanumerics and the '-', '_', or '~' characters.
  • type: The type of data intended to be stored in this column. For a detailed description of each column type, please see the Column Types section.

These fields are optional:

  • description: A human readable description behind the intent of this column.
  • unique: A boolean value answering the question "is every value in this column expected to be unique?"

Versioning the Schema

The schema is used for branching and can be versioned by committing the schema file in your versioning control system together with your project.


Last modified 1 mo17 days ago