How to prevent a PDO exception when using Rules to Set a data value after saving new content? I need to write a rule that edits some elements of the node, as soon as it is created. Unfortunately Drupal doesn’t like that and crashes whenever you try to save the node within a rule that was triggered by "After saving new content".

Event: After saving new content

Conditions: ...

Actions: Set a data value, with
Parameter: Data: [node:field-workflow...], Value: 1

The above results in:


  PDOException: SQLSTATE[23000]: Integrity constraint violation:


So far I managed to walk-around that with hook_forms, changing the workflow in my portal (so I never have to edit the node just after it is created) 

Things get really interesting when I try to use "Execute custom PHP code" action which will change the node value I want:


node_save will result in drupal crushing
db_update - seems to work most of the time...
db_insert - sometimes works (works for superuser only)


Unfortunately all my hacks don't work for all my problems. How else I can address this situation?
Have a look at issue # 430274, about an event which is a variation of your event here, and somehow explains that "after saving something" actually happens ... "before saving something" (which is not a bug, simply how the Rules module  works ...).

A possible compromise to avoid these kind of issues, is to transform what you're trying to do in your Rules Action (related to Set a data value) in a Rules Component, so that your Rule "schedules the execution" (using the Rules Scheduler submodule) of that Rules Component. For example after only a few seconds or minutes later on (if your cron job runs frequent enough, otherwise it'll be next time cron runs). This compromise will ensure that, at the time the Rules Component is executed, the "Save new content" for sure completed, and your new content will have been created as expected.

PS: I'm assuming this question is about D7.