SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'field_access_role_id' cannot be null

Created on 30 May 2023, over 1 year ago
Updated 31 May 2023, over 1 year ago

Postponed on Field type modules cannot maintain their field schema (field type schema change C(R)UD is needed) Needs work which will hopefully make this easy and beautiful to solve.

Problem/Motivation

After updating the module from >2.0.1, the following SQLSTATE Exception will appear, when trying to save a node with a entity_access_by_role_field field with no roles checked:

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'field_access_role_id' cannot be null: INSERT INTO "node__field_access" ("entity_id", "revision_id", "bundle", "delta", "langcode", "field_access_role_id", "field_access_access") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array ( [:db_insert_placeholder_0] => 9 [:db_insert_placeholder_1] => 20 [:db_insert_placeholder_2] => article [:db_insert_placeholder_3] => 0 [:db_insert_placeholder_4] => en [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => allowed ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 815 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

This problem appears through the fixes in 🐛 Access radio value gets removed, when no role is selected Fixed . As already discussed there, it is currently very complex to update an existing field schema having multiple values. For further information on this quite complex topic, visit Field type modules cannot maintain their field schema (field type schema change C(R)UD is needed) Needs work .

Workaround:
visit the field and field revision database tables and make the "role_id" column NULLABLE (node__field_my_field_name and node_revision__field_my_field_name)

Steps to reproduce

Create or Edit an entity, which has a role field and leave the roles selection empty with an initial installation of this module < 2.0.2

Proposed resolution

Write an update hook to make the role_id NULL'able in the field schema.
There are helper methods by @Anybody: https://gist.github.com/JPustkuchen/ce53d40303a51ca5f17ce7f48c363b9b to implement such update hooks for field schema, but that's heavy lifting.

Remaining tasks

Implement the update hook

User interface changes

None

API changes

None

Data model changes

Make role_id column nullable in the field schemas.

🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇩🇪Germany Grevil

Live updates comments and jobs are added and updated live.
  • Needs manual testing

    The change/bugfix cannot be fully demonstrated by automated testing, and thus requires manual testing in a variety of environments.

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024