Vital.sandbox
Database

self:define

Server

Defines the schema for a table, registering it for use with the database instance


Syntax

local status = self:define(table, columns)

Defining a schema does not create the table in the database. Call self:sync after all schemas have been defined to synchronise and create any tables that do not already exist.

  • Before sync — the schema is registered locally on the database instance only
  • After sync — the table is created in the database if it does not already exist

Parameters

TypeNameDescription
stringtableName of the table to be defined
tablecolumnsMap of column names to their column definition
Refer Definitions section

Returns

TypeNameDescription
boolstatustrue on successful execution, or false on failure

Definitions

Each entry in the columns table is a key-value pair where the key is the column name and the value is a definition table with the following fields:

FieldTypeDefaultDescription
typestring"VARCHAR(255)"SQL column type:
"INT" / "INT UNSIGNED" / "BIGINT"
"FLOAT" / "DOUBLE"
"BOOLEAN"
"VARCHAR(n)" / "TEXT"
"DATETIME" / "TIMESTAMP"
primaryboolfalseWhether this column is the primary key
autoincrementboolfalseWhether this column auto-increments
Typically used alongside "INT UNSIGNED" primary keys
nullablebooltrueWhether this column allows NULL values

Examples

Define and sync the 'players' table
local db = database.create("127.0.0.1", "root", "", "vital_sandbox")

--Define the players table schema
db:define("players", {
    id = { type = "INT UNSIGNED", primary = true, autoincrement = true, nullable = false },
    name = { type = "VARCHAR(64)", nullable = false },
    score = { type = "INT", nullable = true }
})

--Sync all defined schemas to the database
db:sync()

On this page