Unique violation when trying to create a new Field Collection using Rules 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" } }
        }
      }
    ]
  }
}
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.