1

When I try to create a new field collection for a node I just saved, I encounter following error:

PDOException: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "node_pkey" DETAIL: Key (nid)=(47) already exists.:

However, if I try to create a node this way, it works fine, so I assume it's a bug in Field collection.

I don't know it it matters, but I'm using PostgreSQL 9.3 as my DB. Also, this issue might be related to the issue about Field Collection Item Duplicate with Rules, but I'm not sure (there is not DB level error mentioned there).

Example of failing rule:

{ "rules_generate_field_collection_items" : {
    "LABEL" : "Generate field collection items",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_insert--smlouva" : { "bundle" : "smlouva" } },
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "field_collection_item",
            "param_field_name" : "field_dodatky_ke_smlouve_fc",
            "param_host_entity" : [ "node" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      }
    ]
  }
}

For example, this works fine:

{ "rules_generate_nodes" : {
    "LABEL" : "Generate nodes",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_insert--smlouva" : { "bundle" : "smlouva" } },
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "node",
            "param_type" : "mesto",
            "param_title" : [ "node:title" ],
            "param_author" : [ "site:current-user" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      }
    ]
  }
}
4
  • I'm voting to close this question as off-topic because it is a bug report for a contributed module. Please report all bugs to the module issue queue as we can't deal with them here. Many thanks
    – Clive
    Aug 11 '15 at 8:40
  • I already reported the issue at drupal.org/node/2549265 However, there are often some workaround, which can help to use the functionality even if there is some bug. Also, I'm not sure if I'm doing everything right - Rules is a very complex module. So, please consider re-opening the question, as it might become useful resource for other with the same problem. I think, that this might be a relatively common issue. Aug 11 '15 at 8:49
  • Sorry, workarounds for bug reports are explicitly off-topic here (always have been) - you did the right thing raising this in the issue queue, that's where the discussion needs to take place for something like this
    – Clive
    Aug 11 '15 at 9:06
  • OK, I understand. However, I now have an answer to this question, which isn't workaround (as I understand it). Would it be possible to re-open this question, so I can answer it? Aug 13 '15 at 14:40
0

It seems to be working fine, when I'm using event "Before saving content (of type ...)". If you also want to make sure, that it only affects newly created entities (mostly nodes), you'll also want to add condition "Data value is empty" for node:nid or another entity ID.

0

I doubt a little bit about the comment"... it is a bug report for a contributed module ...". And IMO changing the event to "Before saving content (of type ...)", as in the existing answer, seems like changing the question, which was NOT about that Rules Event.

Have a look at the issue about "After updating existing content" runs before content is saved, about an event which is a variation of your event here, and somehow explains that "after saving something" actually happens "before saving something". Therefor I'm about sure this is not a bug, but simply how the Rules module works.

A possible compromise to avoid these kind of issues, is to transform what you're trying to do into a Rules component, so that your original Rule "schedules the execution" (using the Rules Scheduler sub-module) of that Rules component. For example after only a few seconds or minutes 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 entity is for sure saved (so that can't be the reason anymore then why things don't work as expected).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.