First of all: I will classify this as bug, as it may cause problems with other modules on the same site.
The problem is, that update 8106 simply calls \Drupal::entityDefinitionUpdateManager()->applyUpdates(). The API documentation warns that "Use this with care, as it will apply updates for any module, which will lead to unpredictable results."
So even, if you keep your module clean, this function call may cause problems to other modules, or even update could fail because of any other pending entity definition update.
Example: our first D8 site, that we started to build with 8.0.0-rc1 - there we also started to build some custom modules, we have extended and improved over the last months. Somehow, there's now a mismatch regarding two fields of one of our custom entity types. I don't know exactly, what kind of mismatch is detected, but everything works as expected. And it's unfortunately not possible to apply the definition update, as the fields are already in use and therefore the modification is not allowed. And therefore also metatag update 8106 will fail.
This is just an example. There's for sure also a good reason, why the entity definition updates are not applied automatically on DB updates. Therefore a single module should never decide to run all the pending entity updates at all.
Comments
Comment #2
damienmckennaYeah, lets bump this to a 'major' issue. Urk.
Comment #3
agoradesign commentedThis may help: https://www.drupal.org/node/2554097
Comment #4
davemybes commentedJust hit this same issue, but in my case its for the Name field from the User entity. I would assume that Name should never change as its part of the core user entity. Drush entity-update fails too, because there is obviously data in the name field.
Comment #5
agoradesign commented@incrn8: the important thing to understand here is, that not Metatag is trying to change your user name field or the things I've mentioned.
This can be the result of any other custom or contrib module, that is changing or overriding for example base field definitions of own entities (of types defined by its own) or entities of types defined in another module, like the user entity. This is all legitimate, but overriding base field definitions is a two-step process: first you define it your source code, next you need to update the database on existing sites (via an update script).
Obviously the module that is responsible for changing your user name field, hasn't done that cleanly because it hasn't implemented an update script. Metatag on the other side, has "overshot" in its update script by running all pending entity defintion updates. So not only its own changes, but also any other open one. And this can have unpredictable results and cause problems like in our cases
Comment #6
damienmckennaTo continue the analogy, it seems we need to improve the update script to only fix the Metatag entity so it doesn't overshoot the runway and crash into the terminal. Unfortunately I'm still rather all-thumbs when it comes to working with D8 (continuing the analogy further, I'm the guy on the left) so tips on how to improve 8106 would be appreciated.
Comment #7
agoradesign commentedIn this particular case, I haven't too much experience so far either. Until now, I didn't have the need to write an update script that does entity definition updates.
If I would run into this situation, I'd have a deeper look into the EntityDefinitionUpdateManager class, that you're already using. There's a distinction made between changes of field storage definitions and entity types. There install/unistall/update functions for both of them available. Further it would be interesting to debug, what the getChangeList() function returns for a particular pending update. This one is called e.g. by the applyUpdates() function.
Comment #8
sprite commentedThis problem of "Mismatched entity and/or field definitions" upon installation or removal of modules appears to be a global Drupal 8 problem that ought to have global attention for a universal fix that would prevent the bug from showing up in dozens of situations as it does currently. There are at 67 reports of the problem in d8 on this website.
https://www.drupal.org/search/site/%22Mismatched%20entity%20and%252for%20field%20definitions%22
Comment #9
damienmckennaIs this still a problem, given how old the code is?
Comment #10
agoradesign commentedI'd say: for this particular update no.. If someone is still on such old versions, updates must be done with great care (and step by step) anyway. I'd close the issue and keep that future updates in mind
Comment #11
damienmckennaYeah, that's what I was thinking too. Thanks everyone.