5

Whenever I try and create a new node (of type article, page, blog, etc), for some reason I get thrown the following error upon hitting the save button.

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'entity_id' cannot be null: INSERT INTO {pathauto_state} (entity_type, entity_id, pathauto) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => [:db_insert_placeholder_2] => 1 ) in pathauto_entity_state_save() (line 492 of /sites/all/modules/pathauto/pathauto.module).

I've tried going into the database and setting these columns to allow NULL, but this doesn't seem to fix the issue. I've also tried uninstalling and reinstalling the pathauto module, but no luck.

This error was occurring previously with a couple of other modules, and I seem to have fixed them. I'm just not sure why this error is occurring at all (e.g. I can edit and save existing nodes, but creating new ones all of a sudden is a problem?)

UPDATE: Another issue I seem to get:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id' cannot be null: INSERT INTO {file_usage} (fid, module, type, id, count) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4); Array ( [:db_insert_placeholder_0] => 126 [:db_insert_placeholder_1] => file [:db_insert_placeholder_2] => node [:db_insert_placeholder_3] => [:db_insert_placeholder_4] => 1 ) in file_usage_add() (line 698 of /includes/file.inc).

6
  • 1
    Backup your DB, then try disabling and uninstalling pathauto and then enabling again so you start from scratch. Fill in all your paths with tokens, then you can use the pathauto bulk update feature to update all the paths, and hopefully this will fix your node creation problem.
    – No Sssweat
    Nov 30, 2016 at 2:07
  • @NoSssweat Tried that, still a no go :/
    – Brittany
    Nov 30, 2016 at 16:42
  • @NoSssweat It actually seems like nothing I create has a an 'id'. E.g. I tried to create a new field and I get the same error - no default id.
    – Brittany
    Nov 30, 2016 at 17:07
  • hmmm sounds as if your ID table was emptied or deleted or something. Have you tried creating a View of articles and then adding node id as a field? Does it display the id?
    – No Sssweat
    Dec 1, 2016 at 8:04
  • @NoSssweat Just added nid to the Articles view and yes, it displays the nid. How strange?
    – Brittany
    Dec 1, 2016 at 16:56

3 Answers 3

3
+25

Short answer

It sounds like you have some code which deletes the node ID on saved nodes causing trouble.

Longer answer.

When you create a new node, Drupal will internally make an insert to the node table to generate an ID and then use that id to populate node_field_data and node_field_revision tables (assuming you are using revisions). The ID is also set on the node entity itself and is accessed via the id() method on the Node class.

The NodeForm does a check on the node object upon being saved, and rebuild the form in case the node doesn't get an ID.

Since you don't get an error for node_field_data and node_field_revision tables, it's pretty safe to assuming that the ID generation works and the node is populated with the ID (Core has lots of test cases covering this).

Since stuff reacting to the node being saved fails, with the missing ID, some code must delete the ID after it's set. Class instances are passed by reference, meaning that if you change one instance you change it every where that class instance is used. So most likely you have some code messing stuff up. There is a good chance that it's some code implementing hook_entity_insert or hook_entity_update (or the node specific variations of those hooks).

3
  • Where can I go about checking this? I'm not even sure what would've caused this issue other than "it must have been caused by restoring the backed up database". Lol.
    – Brittany
    Dec 5, 2016 at 17:09
  • If it used to work and stop working after database import. Chances are that something in MySQL is messed up. If the issue was related purely to MySQL it should fail sooner, which makes me think the issue is located elsewhere. You would need to debug your system to figure out where fails occurs. You should look at modules implementing the hooks mentioned in the answer.
    – googletorp
    Dec 6, 2016 at 12:07
  • 1
    Issue found (I think): Because I imported my old Drupal database, & used a brand new Drupal installation for the core, Drupal was trying to create nodes beginning with nid "1" when it was newly installed. Of course, this couldn't be done, because nid "1" already existed (as did 311 nodes after that). I figured this out by going into my database and creating a node manually with a nid beginning at 312. I was able to do this successfully, and then edit the node in the Drupal admin panel and save as usual. Now, I just need to set my system module to create nodes beginning with id 313 I'd assume.
    – Brittany
    Dec 9, 2016 at 18:10
1

Make sure your database is fully up-to-date (run drush updb or visit /update.php). If you have pending database updates you can run into errors like your describing. There are times when errors occur during the update process and you either have to run them a second time, or have to fix another problem before they will run.

Make sure that all updates run successfully and then check the site's status page and make sure nothing is flagged as red.

1
  • Everything is up to date.
    – Brittany
    Dec 5, 2016 at 17:10
1

Here are a few pointers to help solve the issue:

  • Are you creating nodes using code anywhere? If yes, then make sure that the node is being saved properly and an ID is being generated for it.
  • Can you try creating a node using code and see if the node ID is being generated properly?
  • If nothing works, can you try doing another fresh Drupal install on the same server and see if creating a node still throws errors?
  • Check if your modules are up-to-date by visiting the /admin/modules page and clicking Check for updates.
  • Check if your schema is up to date. Visit update.php and do a database update (or use drush if you use it).

Hope it helps. I had faced this kind of a problem once. But in my case, I was attaching files to the node using custom code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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