Tables - Flaarum Tutorials
A table is a store of similar information. In flaarum a table structure (or definition) is enforced. This helps checks for typing errors and reduces bugs in a setup.
Supported Data Types
The following datatypes are supported in flaarum:
- int - maps to the int64 golang type
- string - an indexed string - not more than 220 characters
- text - an unindexed string - infinite size
Table Defintion Statements
The syntax is very different from SQL. The syntax also does not support comments.
Sample One
table: users fields: firstname string required surname string required email string unique required year_of_birth int biography text ::
Note that:
- The primary key is automatically created. It is of type `int` and is named `id`. Also it is autoincremented and it is unique.
- Each section is ended with '::' and is compulsory even if only the fields section is present.
Sample Two
table: grades fields: userid int required grade int required remarkid int required creation_year int required creation_month int required :: foreign_keys: userid users on_delete_delete remarkid remarks on_delete_delete ::
Note that:
- Foreign keys in Flaarum only supports linking against the `id` of a table.
- A foreign_key part takes either an on_delete_delete or an on_delete_restrict
Sample Three
table: comments fields: userid int required comment1 string required comment2 string nindex comment3 string nindex :: foreign_keys: userid users on_delete_delete ::
Note that: A field ending in nindex means indexes would not be created for that field. And thus you cannot search by that field.
Actual Table Creation (Golang)
You can use the cli to create a table or use the following functions:
func (cl *Client) CreateTable(stmt string) error func (cl *Client) CreateOrUpdateTable(stmt string) error
About Table Updates
Updating a table's definitions doesn't make all the existing rows to adapt to the change. But it becomes enforced in the insert-row and update-row function calls that happens after the change.
After any change to a tables' definitions; the new rows or recent updated rows would be given a different version number.