When creating a new table, there is no option to set fields as 'not null'.
Thus it is not possbile to create non nullable fields. It is also not possbile to create primary keys.

CommentFileSizeAuthor
#4 data-1054616-notnull-4.patch2.16 KBjosebc

Comments

alansaviolobo created an issue. See original summary.

alansaviolobo’s picture

This line in TableConfig::createTable() is required to generate the correct sql

 'not null' => isset($field['not null'])?:null,
josebc’s picture

I'm facing something similar in the case of the primary key not being a serial "SQLSTATE[42000]: Syntax error or access violation: 1171 All parts of a PRIMARY KEY must be NOT NULL;"

Adding $table_definition['fields'][$field['name']]['not null'] = TRUE; in

if ($field['primary']) {
$primary_keys[] = $field['name'];
$table_definition['fields'][$field['name']]['not null'] = TRUE;
}

fixes the issue, however I think the UI should allow for adding "NOT NULL" columns

josebc’s picture

Status: Active » Needs review
StatusFileSize
new2.16 KB

Patch to add option for not null

joachim’s picture

+++ b/src/Entity/TableConfig.php
@@ -65,12 +65,14 @@ class TableConfig extends ConfigEntityBase implements TableConfigInterface {
+        $table_definition['fields'][$field['name']]['not null'] = TRUE;

Is that the right place for this? It's only getting added if the field's a primary key AFAICT.

josebc’s picture

This is more like a fallback to make sure no "All parts of a PRIMARY KEY must be NOT NULL" happen in case PK its not set to NOT NULL from UI, the ones added from there are added from
+ 'not null' => $field['not_null'],
Not sure if its the best way to do it though