function InsertTest::testInsertIntegrityViolation

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/InsertTest.php \Drupal\KernelTests\Core\Database\InsertTest::testInsertIntegrityViolation()
  2. 11.x core/tests/Drupal/KernelTests/Core/Database/InsertTest.php \Drupal\KernelTests\Core\Database\InsertTest::testInsertIntegrityViolation()

Tests insertion integrity violation with no default value for a column.

File

core/tests/Drupal/KernelTests/Core/Database/InsertTest.php, line 218

Class

InsertTest
Tests the insert builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testInsertIntegrityViolation() : void {
  // Remove the default from the 'age' column, so that inserting a record
  // without its value specified will lead to integrity failure.
  $this->connection
    ->schema()
    ->changeField('test', 'age', 'age', [
    'description' => "The person's age",
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
  ]);
  // Try inserting a record that misses the value for the 'age' column,
  // should raise an IntegrityConstraintViolationException.
  $this->expectException(IntegrityConstraintViolationException::class);
  $this->connection
    ->insert('test')
    ->fields([
    'name',
  ])
    ->values([
    'name' => 'Elvis',
  ])
    ->execute();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.