I'm still working on user registration with Drupal 7 and Services 3 and I got a weird Error 500 :

Integrity constraint violation: 1048 Column 'uid' cannot be null

Here is the code I'm using :

// REST Server URL for auth
$request_url = SERVER_URL . 'rest/user/register';

// User data
$user_data = array(
  'name' => 'newuser',
  'mail' => 'newmail@mail.com',
  'pass' => 'newpass',
  'field_city' => array('und' => array('0' => array('value' => 'NA'))),

$user_data = json_encode($user_data);
// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $user_data);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_VERBOSE, true);

$response = curl_exec($curl);

$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  if ($http_code == 200) {
  return json_decode($response);
else {
  $http_message = curl_error($curl);
  return $http_message;

Is there something I'm doing wrong ?

  • It says you can't set uid to null, or omit it. But I don't see where you are setting it. So it looks like you are omitting it. Or am I missing something? Also, for uid there should be some kind of auto increment and null should be allowed, and mean "generate next one". Was you altering things directly in database?
    – Mołot
    Jun 12 '15 at 10:52
  • Hello Molot and thanks for your quick comment, but I didn't know the uid at that point, cause I'm trying to create the user, so I can't pass it to $user_data. The table "users" isn't auto-incremented and Null is not allowed, but this is not a problem when I create a new user with the Drupal form or with Drush. This error only appears when I try to create a user using Services, and I prefer not to alter this table.
    – nieud
    Jun 12 '15 at 11:02

I think that on the other side of your code you are trying to save the user. So, to create a new user using user_save() function you have to left the first parameter blank so a new user is created. Also, add the parameter true to your json_decode($response, true) to obtain an array rather than an object.

$account = user_save('', $fields);

  • Hello MMC, yes I'm trying to save the user but with Drupal Services API. So I won't use user_save() neither bootstrap Drupal.
    – nieud
    Jun 15 '15 at 9:41

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.