Commit 5776d983 authored by Dries's avatar Dries

Issue #2279395 by alexpott, mradcliffe, bzrudi71: Fixed The PostgreSQL backend...

Issue #2279395 by alexpott, mradcliffe, bzrudi71: Fixed The PostgreSQL backend does not handle NULL values for serial fields gracefully.
parent 6fb3b61f
......@@ -58,6 +58,10 @@ public function execute() {
if ($index == 0) {
$last_insert_id = $serial_value;
}
// Sequences must be greater than or equal to 1.
if ($serial_value === NULL || !$serial_value) {
$serial_value = 1;
}
// Set the sequence to the bigger value of either the passed
// value or the max value of the column. It can happen that another
// thread calls nextval() which could lead to a serial number being
......
......@@ -882,9 +882,9 @@ protected function doSave($id, EntityInterface $entity) {
->fields((array) $record)
->execute();
// Even if this is a new entity the ID key might have been set, in which
// case we should not override the provided ID. An empty value for the
// ID is interpreted as NULL and thus overridden.
if (empty($record->{$this->idKey})) {
// case we should not override the provided ID. An ID key that is not set
// to any value is interpreted as NULL (or DEFAULT) and thus overridden.
if (!isset($record->{$this->idKey})) {
$record->{$this->idKey} = $insert_id;
}
$return = SAVED_NEW;
......@@ -1012,7 +1012,14 @@ protected function mapToStorageRecord(ContentEntityInterface $entity, $table_nam
if (!empty($definition->getSchema()['columns'][$column_name]['serialize'])) {
$value = serialize($value);
}
$record->$schema_name = drupal_schema_get_field_value($definition->getSchema()['columns'][$column_name], $value);
// Do not set serial fields if we do not have a value. This supports all
// SQL database drivers.
// @see https://www.drupal.org/node/2279395
$value = drupal_schema_get_field_value($definition->getSchema()['columns'][$column_name], $value);
if (!(empty($value) && $this->getSchema()[$table_name]['fields'][$schema_name]['type'] == 'serial')) {
$record->$schema_name = $value;
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment