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 core.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 define
tablecolumnsMap of column names to their column definition
Refer Definitions section

Returns

TypeNameDescription
boolstatustrue on successful execution, false otherwise

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"
primaryboolfalseDetermines whether this column is the primary key:
• When true - the column is used as the table's primary key
• When false - the column is not part of the primary key
autoincrementboolfalseDetermines whether this column auto-increments:
• When true - the column's value increases automatically with each insert
• When false - the column's value must be provided manually
nullablebooltrueDetermines whether this column allows NULL values:
• When true - the column may be left empty
• When false - the column requires a value on insert

Examples

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

entity:define("players", {
    id = { type = "INT UNSIGNED", primary = true, autoincrement = true, nullable = false },
    name = { type = "VARCHAR(64)", nullable = false },
    score = { type = "INT", nullable = true }
})
entity:sync()

On this page