Documentation for the "fields
" JSON key.
Fields are a required JSONArray for each table that is defined. Field definitions are the basis for the interactions throughout the h2zero generated code. The type, length, nullability, uniqueness and other attributes determine how the various helper classes are generated.
Throughout the code generation, the field definitions are linked through lookups to ensure that the maximum amount of integration is met and to ensure that the naming and lookup of fields is consitent throughout the code.
"tables": [
{
"fields": [
...
]
}
]
Please see the database naming conventions
page for information recommended naming patterns for h2zero,
Allowable key/value pairs on the field JSON Object
JSON key | usage | allowable values | notes |
---|---|---|---|
name | mandatory | any valid MySQL field name value | Each field MUST have a name. It is recommended to use snake_case for field naming. |
type | mandatory | Any valid MySQL field type, the currently supported types are:
| The field types will automatically be mapped to the appropriate java type by h2zero. |
length | optional | Any integer value | Lengths are only required certain fields, varchar , tinyint(1) for example. |
decimalLength | optional | Any integer value | Used in conjunction with decimal and float types, the number of decimal places for the field. |
nullable | optional | Any boolean value | By default, fields are always nullable, unless otherwise specified. |
primary | optional | Any boolean value | This may occur only once on any table. |
unique | optional | Any boolean value | Whether this field should be unique throughout the table - this will automatically generate a unique index on the field for the table. |
index | optional | any boolean value | Whether an index is created on the table in the SQL createion script. |
default | optional | Any String value in single quotation marks (') | This sets the default value on the field |
foreign | optional (deprecated) | A JSON Object | This allows the current key to reference a foreign key on another table, see the example below. |
onUpdate | optional | One of RESTRICT , CASCADE , SET NULL , NO ACTION | The action to take for the mysql ON UPDATE , only valid if this field has a foreign key reference. |
onDelete | optional | One of RESTRICT , CASCADE , SET NULL , NO ACTION | The action to take for the mysql ON DELETE , only valid if this field has a foreign key reference. |
Primary Key Field
Each table MUST have one, and only one primary key definition
"fields": [
{ "name": "id_user", "type": "bigint", "nullable": false, "primary": true },
{ "name": "id_user_type", "type": "bigint", "nullable": false, "index": true, "foreignKey": { "user_type.id_user_type" } },
...
]
In the above example, the primary key definition in longer format is as follows:
{
"name": "id_user",
"type": "bigint",
"nullable": false,
"primary": true
}
name
- The name if the field - as a primary key, it is recommended that it be named thusly:
id_<table_name>
type
- For a primary key, the type MUST be a
bigint
nullable
- The primary key should never be null
primary
- Indicating that the field is primary - this leads to indexes being placed automatically on this field
Foreign Key Reference
{
"name": "id_user_type",
"type": "bigint",
"nullable": false,
"index": true,
"foreignKey": { "user_type.id_user_type" }
}
name
- The name if the field
type
- Since the foreign key references a primary key, the type MUST be a
bigint
nullable
- The foreign key may be null
index
- Whether to create an index on the field.
foreignKey
- This is a JSON Object that references the
table
andfield
of the referenced primary key.