Problem/Motivation
When creating or deleting a taxonomy vocabulary using taxonomy_vocabulary_save
or taxonomy_vocabulary_delete
or using the UI, the following artifacts are left in the database:
- Table: field_data_taxonomy_my_category
- Table: field_revision_taxonomy_event_category
- An entry in table
field_config
This issue is known to cause problems in the following modules:
Proposed resolution
catch proposed the latest solution in comment #26. I believe the solution is best summarized by the following comments:
- #17 Berdir -
The problem with the existing patches was they they assumed the field names, but that's wrong. taxonomy_tags is just what's used for the default tags field, fields created through the UI are prefixed with field and can have any name.We need to load all taxonomy fields. loop over them to see if they're using this vocabulary and if yes, delete them.
- #18 catch -
@Berdir: it's possible via the API to have a single field with multiple vocabularies yes. I don't think there's a UI for this yet, but it lets you do things like have structured vocabularies but a single autocomplete widget on the node form.Patch looks good, but if we're removing one of the cache_clear_all(), could we remove it from taxonomy_vocabulary_delete() instead and leave it in the submit handler? - node_save() and comment_save() already did that in D7. Vocabulary deletion is pretty rare so it doesn't matter much but good to have this consistent.
- #19 Bedir -
Sure, I can delete it inside the API function, just wondering if that is a good idea at this point because it would change what that function is doing, in case someone is using it.About the multiple vocabularies thing, what is supposed to happen when you have a field that points to two vocabularies and then delete one of them? Delete the field or just remove the reference to that vocab?
- #20 catch -
That's a good question and I'm not sure. When this kind of thing comes up, I'm usually more on the side of "Don't let people delete stuff that's tied into live configuration that their site might be relying on", but apparently a lot of people don't agree with me on that. Node reference iirc allows you to set up references only to particular content types, I wonder if and how that deals with node types getting deleted.Overall I think we really need to look at bundles again in Drupal 8 because it's very confusing what the expectations are from the API.
- #23 catch -
Had another look at this - since deleting a vocabulary deletes the terms, we can safely remove these fields, so it is a case of figuring out if the field is multi-reference or not - if it's multi-reference, we can nuke the allowed values key and save the field back again instead of deleting.
Remaining tasks
- #1007830: Nested transactions throw exceptions when they got out of scope is a blocking issue on this issue. When this is resolved, the patch can be re-tested.
User interface changes
None.
API changes
None.
Original report by sewid
When I create and delete a taxonomy vocabulary, either by taxonomy_vocabulary_save and by taxonomy_vocabulary_delete or using the UI, there remain these tables:
- field_data_taxonomy_my_category
- field_revision_taxonomy_event_category
and an entry in table field_config
Comment | File | Size | Author |
---|---|---|---|
#59 | 687180-59-combined.patch | 6.76 KB | anthbel |
#53 | 687180-53-tests.patch | 5.37 KB | xjm |
#53 | 687180-53-combined.patch | 6.8 KB | xjm |
#44 | 687180-taxonomy-field-cleanup.patch | 2.34 KB | Damien Tournoud |
#39 | taxonomy_field.patch | 5.75 KB | catch |
Comments
Comment #1
Island Usurper CreditAttribution: Island Usurper commentedThe vocabulary API just isn't using the Field API like it ought to. Adding the field_create_field() and field_delete_field() calls is pretty easy, but there's no UI to set the field settings from the vocabulary form. Personally, I think that's a separate issue, and should be easy enough to fix.
There's a similar problem with node types, but let's keep things simple for now.
Comment #3
Island Usurper CreditAttribution: Island Usurper commentedI guess I was wrong about the bundle name needing to change. Hopefully HEAD is better now.
Comment #5
Island Usurper CreditAttribution: Island Usurper commentedAha. I found the specific error message, and these changes made the standard profile a little out of date.
Comment #7
Island Usurper CreditAttribution: Island Usurper commented#5: 687180_taxonomy_node_fields.patch queued for re-testing.
Comment #9
Island Usurper CreditAttribution: Island Usurper commentedJust found #628244: No way to attach the automatically created taxo field, which explains why it works this way. So, whenever you delete a vocabulary, you have to remember to delete it from all of the bundles it's been attached to.
No thank you. When a vocabulary is deleted, its related term reference field should be deleted because it can't hold valid data any more.
I understand needing to create the field when it is first attached to a bundle, though. You have to do that to figure out which node types apply the vocabulary applies to, anyway.
Comment #11
Island Usurper CreditAttribution: Island Usurper commentedSeriously, I can't even get a one-line patch right?
Comment #13
Island Usurper CreditAttribution: Island Usurper commented...OK. Now the tests should work.
Comment #14
catch#13: 687180_vocabulary_delete_field.patch queued for re-testing.
Comment #16
David_Rothstein CreditAttribution: David_Rothstein commentedThis is a more serious bug than it looks like, and easy to reproduce. All you have to do is this:
1. Install Drupal with the standard profile.
2. Delete the "Tags" vocabulary via the UI.
3. Create an article, notice that the Tags field is still there, and type something into it. When you try to save the article you wind up with a whitescreen and the following PDO Exception:
Comment #17
BerdirConfirmed.
The problem with the existing patches was they they assumed the field names, but that's wrong. taxonomy_tags is just what's used for the default tags field, fields created through the UI are prefixed with field and can have any name.
We need to load all taxonomy fields. loop over them to see if they're using this vocabulary and if yes, delete them. What I *don't* understand is why the allowed_values structure is an array, it's not possible to assign a single field with multiple vocabularies, is it? (At least not through the UI... )
Added a simple test, the test fails without the fix.
I actually did this inside the API function, that also removes bundles and stuff so I think that's ok. While doing that, I noticed that there is a clear_clear_all() call in there too, so there is no need for the one the form submit function.
Comment #18
catch@Berdir: it's possible via the API to have a single field with multiple vocabularies yes. I don't think there's a UI for this yet, but it lets you do things like have structured vocabularies but a single autocomplete widget on the node form.
Patch looks good, but if we're removing one of the cache_clear_all(), could we remove it from taxonomy_vocabulary_delete() instead and leave it in the submit handler? - node_save() and comment_save() already did that in D7. Vocabulary deletion is pretty rare so it doesn't matter much but good to have this consistent.
Comment #19
BerdirSure, I can delete it inside the API function, just wondering if that is a good idea at this point because it would change what that function is doing, in case someone is using it.
About the multiple vocabularies thing, what is supposed to happen when you have a field that points to two vocabularies and then delete one of them? Delete the field or just remove the reference to that vocab?
Comment #20
catchThat's a good question and I'm not sure. When this kind of thing comes up, I'm usually more on the side of "Don't let people delete stuff that's tied into live configuration that their site might be relying on", but apparently a lot of people don't agree with me on that. Node reference iirc allows you to set up references only to particular content types, I wonder if and how that deals with node types getting deleted.
Overall I think we really need to look at bundles again in Drupal 8 because it's very confusing what the expectations are from the API.
Comment #21
pazcu CreditAttribution: pazcu commentedFolks,
I read your thread and it looks like I have been the victim of what you are discussing. I deleted a vocabulary not thinking that would wipe out my stories but it happened. The nodes are still safe in the DB and the titles are showing up on the front page linking to pages not found.
I have been trying to reconnect the new vocabulary to these "lost" stories via the database but to no avail.
Any suggestions how I could recover these stories?
the site is ellatinopost.com
Thanks much.
.pablo
Comment #22
BerdirMoving to the 8.x queue and tagging for possible backport.
Comment #23
catchI'm not convinced we should delete a field just because it has a vocabulary referenced as an allowed value. It's feasible for a field to be allowed to reference more than one vocabulary, so what if there is valid content referencing the other vocabulary? Would rather prevent deletion of the vocabulary and instruct people to change the field settings first.
Comment #24
catchHad another look at this - since deleting a vocabulary deletes the terms, we can safely remove these fields, so it is a case of figuring out if the field is multi-reference or not - if it's multi-reference, we can nuke the allowed values key and save the field back again instead of deleting.
Patch is untested.
Comment #26
catchWell I did test it for syntax errors, just not the actual patch that got uploaded, grr.
Comment #28
catchThe error is:
Which is this bit:
I don't believe this patch could have caused that, so requesting retest.
Comment #30
catchHah, reproduced locally.
This looks like #1007830: Nested transactions throw exceptions when they got out of scope, which is supposed to be fixed.
Comment #31
catchThis patch should still fail, but would like to record that properly now that the main transaction overhaul patch is in.
Comment #32
catch#26: test_and_fix.patch queued for re-testing.
Comment #34
catchYep, same error as http://drupal.org/node/687180#comment-4694714
Marking this as postponed on #1007830: Nested transactions throw exceptions when they got out of scope.
Comment #35
catchhmm postponed doesn't work for that either, 'cos that's equivalent to hiding it at the moment.
Comment #36
xjmTagging.
Comment #37
13rac1 CreditAttribution: 13rac1 commentedSubscribe. This issue breaks http://drupal.org/project/filefield_paths paths don't generate correctly after a vocabulary has been deleted. I had fields in the vocabulary, so had to delete entries in field_config_instance also.
Comment #38
carn1x CreditAttribution: carn1x commentedsubscribe
Comment #39
catchHere's a re-roll of #26, plus the latest patch from #1007830: Nested transactions throw exceptions when they got out of scope for the bot.
Comment #41
xjmBot error was:
Retesting because that is kind of weird.
Comment #42
xjm#39: taxonomy_field.patch queued for re-testing.
Comment #44
Damien Tournoud CreditAttribution: Damien Tournoud commentedSee #1007830-107: Nested transactions throw exceptions when they got out of scope for the remaining issue we have around transactions and DDL.
About this issue: let's not call
field_update_field()
when it's not necessary. If the field has no data, this triggers a DROP TABLE / CREATE TABLE sequence which is the one that messes up the transaction stack. Without this, I'm pretty sure that this patch can pass.Comment #45
xjmSee also #1065814: Content remains tagged with a taxonomy term (via the {taxonomy_index} table) even after its corresponding term reference field is deleted and purged. I've requested in that issue that someone test to see if this patch resolves it as well.
Edit: and #89181: Use queue API for node and comment, user, node multiple deletes.
Comment #46
mikeryanRelated: #1363200: Term reference fields can be created with no vocabulary, another way to end up with a term reference field without a valid vocabulary.
Comment #47
hefox CreditAttribution: hefox commented(d7 Reference issue, but with deletion of nodes/users: #1346696: On deletion of node/user, references to it are not removed #589714: Nodereference filtering: invalid reference should be same as no reference)
Comment #48
xjmUntagging.
Comment #49
catch#44: 687180-taxonomy-field-cleanup.patch queued for re-testing.
Comment #50
catchSo this looks fine now, I'm wondering if we should add an additional test for #18 - i.e. create a field with two vocabularies, delete one of them, make sure the settings are correct afterwards.
Comment #51
xjmSounds like a good idea to me. I'd also like to see if the patch here resolves #1065814: Content remains tagged with a taxonomy term (via the {taxonomy_index} table) even after its corresponding term reference field is deleted and purged, and if so, add a test for that bug as well.
Comment #52
xjmSo aLearner confirmed that the patch here does not resolve #1065814: Content remains tagged with a taxonomy term (via the {taxonomy_index} table) even after its corresponding term reference field is deleted and purged, so we just need an additional test along the lines catch describes in #50. I'll work on that.
Comment #53
xjmAttached:
t()
from an assertion message. (Reference: #500866: [META] remove t() from assert message)Comment #54
catchLooks good to me.
Comment #55
catchCommitted/pushed to 8.x, moving back to 7.x for backport.
Comment #56
catchFailed to actually move this to 7.x.
Comment #57
catchShould be a straightforward backport, tagging novice.
Comment #58
anthbel CreditAttribution: anthbel commentedBack-porting #53.
Comment #59
anthbel CreditAttribution: anthbel commentedD7 backport of #53.
Comment #60
anthbel CreditAttribution: anthbel commentedChanged status.
Comment #61
xjmBackport matches up, passes tests, and doesn't include any D8-specific code => RTBC. Thanks @anthbel!
Comment #62
webchickLooks good. Committed and pushed to 7.x. Thanks!
Comment #64
kenorb CreditAttribution: kenorb commentedRelated:
#1778572: EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7562
Comment #64.0
kenorb CreditAttribution: kenorb commentedBeginning to write summary.