I am creating a simple node in some module code. Here is the code, the identical logic of which I have used many times:

    $sub = new Node();
    $sub->type = 'subscriber';
    $sub->title = $email;
    node_object_prepare($sub);
    $sub->uid = $user->uid;
    $sub->language = LANGUAGE_NONE;
    $sub->status = 1;
    $sub->comment = 0;
    $sub->promoted = 0;
    $sub->sticky = 0;
    $sub->created = time();
    $sub = node_submit($sub);
    node_save($sub);

On the node_save() call I am getting this error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'last_comment_uid' cannot be null

I have explicitly turned off comments. Am I missing something new that I need to do in order to prevent this error? Any help greatly appreciated.

 

Accepted answer

If your intention was for $user to be the logged in user, you need to use the statement

global $user;

before your code. Check your code to see if you forgot this?

Most helpful answers

Wait a minute. I see that your code has:

$sub->uid = $user->uid;

Where is $user coming from and what's its value? This is most likely the culprit. If $user is null or empty, then uid will be null, throwing this fatal error. 

Can you post the code where you are assigning $user?

Comments

I'm not able to reproduce this problem.  Can you check the log at admin/reports/dblog to see if there are notices or errors that are occurring right before you save the new node? 

A few thoughts: 

- Even if comments are off for a content type, the table node_comment_statistics will still be written when a new node is saved (even if that content type has comments off). This is done by the hook comment_node_insert()

- It seems like this error may be produced by a uid that's null. This is strange, as your call to node_object_prepare() would take care of filling in the $node->uid with the uid of the current logged in user. This is true even when the user is anonymous (uid = 0)

So, my guess is that there is some unexpected interaction perhaps with other custom code, or with a contrib module.

Can you try a few things here (not all at the same time, rather, one at a time):

- Try using $sub = entity_create('node'); instead of $sub = new Node(); and see if the error persists.

- Try disabling the comment module and see if the error persists.

- Try debugging your code with a watchdog() command before the node_save call and check if $sub->uid exists  

See if you have any installed contrib modules that mess up with comments.

Wait a minute. I see that your code has:

$sub->uid = $user->uid;

Where is $user coming from and what's its value? This is most likely the culprit. If $user is null or empty, then uid will be null, throwing this fatal error. 

Can you post the code where you are assigning $user?

If your intention was for $user to be the logged in user, you need to use the statement

global $user;

before your code. Check your code to see if you forgot this?

sorry i was not getting notifications of these comments, and thanks for the responses. i had modified the code to NOT use nodes to store a simple email address, i created my own table for it, which simplified things for me. 

while i have globalized $user in hundreds of functions, i must admit that i cannot recall if i may have forgotten in that case, and it totally makes sense that that could have caused the problem. in fact that would be my guess. 

in this case i prefer having a separate simple table to store these emails, but your thought about was a good one. thanks very much for your response.