Darlington Okorie
Darlington Okorie

Reputation: 53

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `users`

I have a question and the answers i have been getting are not really helping me.

So I have been trying to basically create a Profiles table for each users. I followed through the rules for One to One relationship but i keep getting this error

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into users (name, email, password, admin, updated_at, created_at) values (Darlington Okorie, [email protected], $2y$10$Ob161LW8sbbv5uDv9VVbienlmW.DWXVDK3wdfC0I0NlnOrg1Jx/a2, 1, 2019-12-23 17:41:30, 2019-12-23 17:41:30))

Heres the code i wrote in my UsersTableSeeder

public function run()
{

 $user = App\User::create([
        'name' => 'Darlington Okorie',
        'email' => '[email protected]',
        'password' => bcrypt('password'),
        'admin' => 1
    ]);

    App/Profile::create([
        'user_id' =>  $user->id,
        'avatar' => 'uploads/avatar/img.png',
        'about' => 'Great at writing. Better at blogging',
        'facebook' => 'facebook.com',
        'youtube' => 'youtube.com'
    ]);
}

And i had also defined user_id in my create_users_table

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        Schema::dropIfExists('users');
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('user_id');
        $table->string('email')->unique();
        $table->boolean('admin')->default(0);
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

And defined the user_id in my create_profiles_table

 public function up()
{
    Schema::create('profiles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('avatar');
        $table->integer('user_id');
        $table->text('about');
        $table->string('facebook');
        $table->string('youtube');

        $table->timestamps();
    });
}

What could be wrong?

Upvotes: 5

Views: 17921

Answers (5)

Nuruzzaman Khan
Nuruzzaman Khan

Reputation: 155

I faced this kind of issue as well. My Case, I forgot to write the field in fillable.

protected $fillable = [
        'name', 
    ];

After writing the field in $fillable. My Issue was solved.

Upvotes: 5

Aamir Anwar
Aamir Anwar

Reputation: 175

You can set 'user_id' to allow null from db.

Upvotes: 0

Slycreator
Slycreator

Reputation: 1230

General error: 1364 Field 'user_id' doesn't have a default value

what this error is saying is that you have a column in the table that you didn't pass

a value for in your form...In your case you didn't pass a value for the field

user_id.I guess what you are trying to implement is that you want to give the primary

key a name.so the fix should be to rename the column $table->bigIncrements('id'); to

$table->bigIncrements('user_id'); and delete the $table->string('user_id');

Upvotes: 0

livreson ltc
livreson ltc

Reputation: 733

I don't how your relationship is set up but try to make some changes as mentioned below. REMOVE THE use_id from users table. You don't need that column. Or Just rename it to USER_CODE for example make it nullable if it is not a mandatory column $table->string('user_id')->nullable(); To be honest I don't see the purpose of having user_id column in usrs table

// Profiles Migration:    

public function up()
{
Schema::create('profiles', function (Blueprint $table) {
    ...
    $table->unsignedInteger('user_id');
     ....
   $table->foreign('user_id')->references('id')->on('users')
            ->onUpdate('cascade')->onDelete('cascade');
  });
}

// Profile.php Model
public function user()  

{
  return $this->belongsTo('App\User');
}

// User.php Model
public function user()
  {
      return $this->hasOne('App\Profile');
  }

// Inside your Controller

   $user = new App\User;
   $user->name = 'Darlington Okorie';
   $user->email = '[email protected]';
   $user->password = bcrypt('password');
   $user->admin => 1;
   if($user->save()) {

     $userID = $user->id;

     $profile = new App/Profile;
     $profile->user_id =  $userID;
     $profile->avatar = 'uploads/avatar/img.png';
     $profile->about = 'Great at writing. Better at blogging';
     $profile->facebook = 'facebook.com';
     $profile->youtube = 'youtube.com';
     $profile->save();
    }

Upvotes: -1

Tim Lewis
Tim Lewis

Reputation: 29258

This is an issue with your users migration, and not the profiles migration. Your migration has this line:

Schema::create('users', function (Blueprint $table) {
  ...
  $table->string('user_id'); // This one

Which is creating a non-null varchar column called user_id. In your Seed file, you're not setting that, and null is not allowed. Add and configure the following line below:

 $user = App\User::create([
  'name' => 'Darlington Okorie',
  'email' => '[email protected]',
  'password' => bcrypt('password'),
  'admin' => 1,
  'user_id' => 'SomeRandomString' // Add this, set to whatever is logical.
]);

Alternatively, set nullable() on your users migration:

Schema::create('users', function (Blueprint $table) {
  $table->string('user_id')->nullable();

Honestly, unless you need a separate identifier for each User, your users table likely doesn't need a user_id, and you can remove it from the migration completely.

Edit: I'm really not sure who downvoted, but read the error message:

Field 'user_id' doesn't have a default value (SQL: insert into users ...)

When this error is encountered, the query is trying to insert into the users table, not the profiles one. Since users.user_id doesn't have a default value, nor does it allow null, the error is being thrown. The 3 alternatives above provide a solution to this.

The error has nothing to do with the User or Profile models, as there is an issue with the migration itself.

Upvotes: 0

Related Questions