Drupal 10.1: fatal error on account creation

Created on 2 July 2023, 12 months ago
Updated 10 May 2024, about 2 months ago

Problem/Motivation

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'uid' cannot be null: INSERT INTO "profile" ("revision_id", "type", "uuid", "status", "uid", "is_default", "data", "created", "changed") 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, :db_insert_placeholder_7, :db_insert_placeholder_8); Array ( [:db_insert_placeholder_0] => [:db_insert_placeholder_1] => customer [:db_insert_placeholder_2] => c1d9e509-098f-42c1-b0de-64a584a0d861 [:db_insert_placeholder_3] => 1 [:db_insert_placeholder_4] => [:db_insert_placeholder_5] => 0 [:db_insert_placeholder_6] => a:0:{} [:db_insert_placeholder_7] => 1688326150 [:db_insert_placeholder_8] => 1688326150 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
Drupal\Core\Database\StatementWrapperIterator->execute(Array, Array) (Line: 44)
Drupal\mysql\Driver\Database\mysql\Insert->execute() (Line: 948)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->doSaveFieldItems(Object) (Line: 718)
Drupal\Core\Entity\ContentEntityStorageBase->doSave(NULL, Object) (Line: 486)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 806)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 339)
Drupal\Core\Entity\EntityBase->save() (Line: 321)
Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem->preSave() (Line: 233)
Drupal\Core\Field\FieldItemList->delegateMethod('preSave') (Line: 191)
Drupal\Core\Field\FieldItemList->preSave() (Line: 938)
Drupal\Core\Entity\ContentEntityStorageBase->invokeFieldMethod('preSave', Object) (Line: 888)
Drupal\Core\Entity\ContentEntityStorageBase->invokeHook('presave', Object) (Line: 529)
Drupal\Core\Entity\EntityStorageBase->doPreSave(Object) (Line: 753)
Drupal\Core\Entity\ContentEntityStorageBase->doPreSave(Object) (Line: 483)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 806)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 339)
Drupal\Core\Entity\EntityBase->save() (Line: 100)
Drupal\user\RegisterForm->save(Array, Object)
call_user_func_array(Array, Array) (Line: 114)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 52)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 597)
Drupal\Core\Form\FormBuilder->processForm('user_register_form', Array, Object) (Line: 325)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object) (Line: 39)
Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 583)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 166)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 74)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
require('/project/app/index.php') (Line: 4)

Steps to reproduce

Core 10.1,
profile 8.x-1.7
Create an account on /user/register and on the form mode there is the profile field to let you create the profile at the same time.

Proposed resolution

With Core 10.1, I also got a lot on problem on reinstallation linked with user permissions:
- 🐛 Compatibility with Core 10.1.* Fixed
- 🐛 Error on hook_install when invoked from drush site-install Fixed

I think some thing about user handling changed in Core and it has a lot of repercussions.

Remaining tasks

Identify root cause
provide a fix.

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇫🇷France Grimreaper France 🇫🇷

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @Grimreaper
  • 🇸🇪Sweden ordermind

    Upping the priority to Major.

  • 🇪🇸Spain paucala

    We are have the same probleme, any idea how to fix it?

  • Confirming the issue on Drupal 10.1.1
    With the same version (8.x-1.7) on Drupal 9.5.9, it works fine.

  • 🇬🇧United Kingdom cwales95

    Just came across this myself on Drupal 10.1.0 and profile version 8.x-1.7.

  • 🇬🇧United Kingdom cwales95

    Something has changed during the code execution I think. preSave is being called on the Profile before the User entity is saved i.e before a uid is available.

  • 🇪🇸Spain paucala

    This have sense, I have been able to create a user and then create a profile for this user, but not at the same time.

  • Assigned to TomTech
  • 🇺🇸United States TomTech

    This is because of the hardening of isAnonymous() that occurred in 10.1 See: [#3345487]

    Patch attached to convert isAnonymous() to !isAuthenticated(). (And the inverse)

    Occurs in 3 places.

    (Also, added a gitignore file)

  • Status changed to Needs review 11 months ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update 11 months ago
    23 pass
  • Status changed to Fixed 11 months ago
  • 🇮🇱Israel jsacksick

    Committed thanks!

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • First commit to issue fork.
  • Status changed to Fixed 7 months ago
  • 🇬🇧United Kingdom JeremySkinner

    I'd like to re-open this issue if possible as the fix in #8 causes some other issues.

    Rather than checking isAuthenticated I'd recommend going back to checking isAnonymous but also check isNew as well. The problem with isAuthenticated is that other modules may have switched out the logic here.

    For example, the decoupled_auth module allows the concept of users without logins (eg for CRM systems where a user record may exist for CRM purposes, but not have a login attached to it), to do this it switches out the logic of isAuthenticated.

    The change in #8 means that the decoupled_auth module no longer works with profile.

    By using a combination of isAnonymous() and isNew(), you'd preserve the original behaviour without breaking modules that swap out the logic of isAuthenticated.

    I've created an issue fork to propose the changes, I'd appreciate if you'd consider it. Many thanks.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update 7 months ago
    23 pass
  • 🇦🇹Austria drupalfan2

    I have the same problem.
    Patch #15 helps so far.

    Hoping for a new version of the profile module with correct solution in it.
    Thank you.

  • 🇺🇸United States chucksimply

    #8 Applied to 1.7, and fixes the error. I tried the merge patch on #15 first, but it failed to apply.

  • 🇬🇧United Kingdom MrDaleSmith

    The patch on #15 applies cleanly to the dev version of the module, as is standard practice for Drupal patches.

  • 🇺🇸United States mlncn Minneapolis, MN, USA

    This is a critical bug— the maintainers should make a release of this module (double-checks real quickly to make sure i am not a maintainer of this module)

  • 🇬🇧United Kingdom cwales95

    I can confirm this is still an issue. The patch in #8 seemingly works.

  • 🇩🇪Germany Anybody Porta Westfalica

    @jsacksick: Sorry to disturb, could you please have a look at the previous comments?

    This major issue currently still breaks sites using the stable version of this module. Furthermore #14 raises concerns about the fix!

    I'd like to suggest
    - Reopening the issue if it makes sense
    or
    - Creating a follow-up if needed
    or
    - Tag a new stable release, if this fix is fine from your perspective, so that sites don't break with the "stable" version.

    Feedback would be great, thank you!

  • 🇩🇪Germany Anybody Porta Westfalica

    @jsacksick: My suggestion after reviewing the change would be:
    1. Tag a new stable release
    2. @JeremySkinner or someone else should create a follow-up for the edge-case in #14

    What do you think?

  • 🇬🇧United Kingdom JeremySkinner

    I'm very happy to create a follow up issue if needed, but the fix that's already been merged is not equivalent with the original behaviour. The fix I proposed in #15 is equivalent to the original correct behavior and would be much better if this could go into a stable release.

  • 🇮🇱Israel jsacksick

    A new release was just tagged.

  • 🇬🇧United Kingdom JeremySkinner

    Many thanks for merging my fix!

  • 🇫🇷France Grimreaper France 🇫🇷

    Hi,

    Finally back on my side project where I encountered the issue. Thanks for the fix! I was able to update from Core 10.0 to 10.2 and updating the Profile module, no fatal error so far.

Production build 0.69.0 2024