Problem/Motivation

Hi, I get:
Mismatched entity and/or field definitions
The following changes were detected in the entity type and field definitions.
Stock location
The User ID field needs to be installed.

after updating the commerce_stock dev module.
As far as I can see, this is due to the change of the StockLocation Entity:
commerce_stock/modules/local_storage/src/Entity/StockLocation.php
We see the new entity_key owner = uid here:

 *   entity_keys = {
 *     "id" = "location_id",
 *     "bundle" = "type",
 *     "label" = "name",
 *     "uuid" = "uuid",
 *     "langcode" = "langcode",
 *     "status" = "status",
 *     "owner" = "uid",
 *   },

Steps to reproduce

Update the module from a version without the uid field to a version with the uid field

Proposed resolution

Update the entity definition table via an update hook

Data model changes

There should be a uid field in the table commerce_stock_location_field_data

Best, Lars

Comments

Synflag created an issue. See original summary.

jrochate’s picture

Same situation here.

But I have created the uid fields on commerce_stock_location and also commerce_stock_location_field_data but the message kept on report.

And also added user 1 to both fields, resaved Stock UI settings, but nothing changed.

jrochate’s picture

Anyway, if you run:

drush entity-updates

the uid field will be created and the problem is solved :)

guy_schneerson’s picture

Category: Bug report » Support request
Status: Active » Closed (works as designed)

My understanding of Drupal release types is that Alpha versions do not require update hooks for schema changes. If like @jrochate suggests "drush entity-updates" solves this then it sounds like you can easily get around this issue.
In any case, if a patch is offered I will be happy to review it.

gkaas’s picture

Same issue here, but drush entity-updates returns the following message in our case:

In UpdateDBCommands.php line 111: Drupal removed its automatic entity-updates API in 8.7. See https://www.drupal.org/node/3034742.
jrochate’s picture

gkaas’s picture

Thanks @jrochate! However, the Devel Entity Updates module page states:

Do not use this to fix the Mismatched entity and/or field definitions error: again, this is not meant to fix production sites. If you encounter that error you should identify which module defines the problematic entity type or field type and open a bug report or support request in its issue queue.

jrochate’s picture

Always backup as precaution.

Then install and run the drush command.

Check that mysql tables identified above now has the correct fields.
Check if everything works.

You're done.

if not, restore.

artis’s picture

Category: Support request » Bug report
Status: Closed (works as designed) » Needs work

From the change record which removed 'drush entup':

"Starting with 8.7.0, Drupal core no longer provides support for automatic entity updates. Whenever an entity type or field storage definition needs to be created, changed or deleted, it has to be done with an explicit update function as provided by the Update API, and using the API provided by the entity definition update manager."

We need to write an update function for this change and push a new release to properly fix this issue.

guy_schneerson’s picture

Just to confirm this is likely to be related to the change in StockLocation.php where

"owner" = "uid", 

was added to the entity_keys.
If a patch is submitted I will commit it. In the meantime, if you have this issue you should be able to use "Devel Entity Updates" to fix this.

harcher’s picture

I am also having this issue now after updating to alpha6.

As #7 mentions above, Devel Entity Updates module page states not to use for fixing Mismatched entities.

Is it safe to fix with Devel Entity?

renrhaf’s picture

Same here

agoradesign’s picture

You can implement an update hook in a custom module like this:

/**
 * Install the uid field of commerce_stock_location on behalf of commerce_stock.
 */
function YOURMODULE_update_XXXX() {
  $field = BaseFieldDefinition::create('entity_reference')
    ->setLabel(new TranslatableMarkup('User ID'))
    ->setSetting('target_type', 'user')
    ->setTranslatable(TRUE)
    ->setDefaultValueCallback(StockLocation::class . '::getDefaultEntityOwner');
  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('uid', 'commerce_stock_location', 'commerce_stock_local', $field);
}
nathaniel’s picture

Status: Needs work » Needs review
StatusFileSize
new1.22 KB

Attached a patch that adds modules/local_storage/commerce_stock_local.post_update.php.

Note that the default uid for existing {commerce_stock_location_field_data}.uid will be NULL (0 after edit).

nathaniel’s picture

Status: Needs review » Needs work

Err, @agoradesign is correct this should be in hook_update_N() then I guess the data could be updated in post_update if needed.

jrochate’s picture

@nathaniel what about people that solved this using drush entity-updates. Does this script colide with those with the problem fixed, or the script will simply ignore and move on?

thanks.

nathaniel’s picture

Looks like it will ignore and move on. Ran it a few times on a test site with no issues. I'm hesitant to write a patch for a hook_update_N script though since that could cause issues for someone if it isn't committed and the same number is used later on for something else. Adding it to a custom module worked fine for me.

agoradesign’s picture

I'm sure there's a possibility to check the field existence before actually trying to create it. And Nathanial is fully right: I would not recommend to anyone to use a patch including an update hook, if it can be avoided easily. If the maintainer decides to commit the update hook, he has to take care anyway to check the case, if the field already exists (because not only the custom updated installations already have the field, also new installations)

renrhaf’s picture

Status: Needs work » Needs review
StatusFileSize
new1.19 KB

Here is a patch that adds an update hook.
If the field is already created, the update hook won't perform any action.
Tested with and without the field installed.

renrhaf’s picture

StatusFileSize
new1.15 KB

Ooops patch rerolled for 8.x-1.x

harcher’s picture

Thanks @Renrhaf, patch #20 works on alpha6.

guy_schneerson’s picture

I have moved the update hook to the local_storage module as it creates the entity that is updated in the hook.
I only tested with the field installed but as others have tested I am happy to commit this

  • guy_schneerson committed a01332e on 8.x-1.x
    Issue #3211778 by Renrhaf, Nathaniel, guy_schneerson, agoradesign:...
guy_schneerson’s picture

Status: Needs review » Fixed

Thanks to everyone for their contribution. Marking as fixed but do reopen if any issues.

mitrpaka’s picture

Status: Fixed » Needs review
StatusFileSize
new1.44 KB
new515 bytes
new482 bytes

Was not able to run update hook of 8006 successfully due to missing imports of BaseFieldDefinition and TranslatableMarkup classes.

---------------------- 
 commerce_stock_local   8006        hook_update_n   8006 - Fix #3211778: missing update hook for "uid" field on "StockLocation" entity.
---------------------- 

// Do you wish to run the specified pending updates?: yes.

>  [notice] Update started: commerce_stock_local_update_8006
>  [error]  Class 'BaseFieldDefinition' not found
>  [error]  Update failed: commerce_stock_local_update_8006
[error]  Update aborted by: commerce_stock_local_update_8006
[error]  Finished performing updates.

New patch files attached:
- commerce_stock-fix-uid-missing-3211778-22.patch - Full patch file (including committed code to 8.x-1.x) to apply on top of 8.x-1.0-alpha6 version.
- commerce_stock-fix-uid-missing-3211778-22-updates-only.patch (including only changes after latest commit to 8.x-1.x)

melpers’s picture

Thanks - commerce_stock-fix-uid-missing-3211778-22.patch worked for me applied to 8.x-1.0-alpha6.

j.’s picture

When i tried the patch in #22, I also hit the same issues described in #25. I'm on 8.x-1.0-alpha6 and have not performed any entity updates.

commerce_stock-fix-uid-missing-3211778-22.patch worked for me.

criz’s picture

Status: Needs review » Reviewed & tested by the community

I also can confirm that with latest alpha version the updates from #25 are necessary so that update hook 8006 runs through.

jdcllns’s picture

I'm sorry but I'm lost. I'm using 8.x-1.0-alpha6. Which patch do I apply to clear up the User ID field error message?

vikramy’s picture

jdcllns -- use this commerce_stock-fix-uid-missing-3211778-22.patch

+1 for this commit.

guy_schneerson’s picture

Status: Reviewed & tested by the community » Fixed

Thanks @mitrpaka
I have added both missing classes to the use section, so the dev version should work now.

Status: Fixed » Closed (fixed)

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