Problem/Motivation
Over at #606840-78: Enable internal page cache by default, we have a bunch of failures in UserRegistrationTest
. The failures happen after registering two users. The fatal error is caused by a SQL error:
[31-Mar-2015 15:47:14 Europe/Brussels] Uncaught PHP Exception Drupal\Core\Entity\EntityStorageException: "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '4a0944f2-9f46-4187-9f09-9b3743105ef6' for key 'user_field__uuid__value': INSERT INTO {users} (uid, uuid, langcode) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array
STR
- Enable page cache.
- Log out.
- Go to
/user/register
- Register once.
- Register again. BOOM! SQL error.
Root cause
- All entity forms set the entity-being-built in form state.
- This means all entity forms use the form cache.
- Form state/form cache are tied to the form build ID.
- The form build ID is present in the HTML.
- Enabling page caching (or any reverse proxy for anon users) ⇒ HTML is cached ⇒ same form build ID used for all anon users ⇒ same form cache/form state used for all anon users.
- So far, no problems. The final, lethal ingredient: creating a (content) entity also means generating a UUID ⇒ the entity in form state, that is used for all users, already has a UUID.
Conclusion: every user signing up at /user/register
is going to get the same UUID for 6 hours (that's the default form state TTL). That doesn't quite work, obviously.
Proposed resolution
- Don't let
UuidItem::applyDefaultValue()
create a UUID (this is called when creating an entity) - Instead, let
ContentEntityBase::uuid()
create it.
Remaining tasks
Review.
User interface changes
None.
API changes
Unsure.