Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DTech_Proz's avatar

PostgreSQL Error :SQLSTATE[23502]: Not null violation: 7 ERROR

Hi, everyone

Can someone please help me fix this error: SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "id" violates not-null constraint DETAIL:

I have deployed my code to Heroku and created a new database, locally I was using MySQL. When i run the app the home page works but when i register I get the error.

Here is my Register Controller Code :

/** * Create a new user instance after a valid registration. * * @param array $data * @return \App\User */

protected function create(array $data)
{
    $settings = Settings::first();

    $user =  User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'usertype' => $data['usertype'],
        'password' => Hash::make($data['password']),
        'active'=>0,
        'token'=> Str::uuid(40),
        'bonus' => Carbon::now(),
        'main_balance'=>$settings->signup_bonus,

    ]);

    Reflink::create([

        'user_id'=> $user->id,
        'link'=> $data['email'],

    ]);

    $user->sendVerificationEmail();

    event(new UserReferred(request()->cookie('ref'), $user));

    session()->flash('message', 'Dear user your account has been successfully created but not active. To active your account please check your email for verify code.');
    Session::flash('type','success');
    Session::Flash('title','Account Created Successfully, Check Verification Email');
    Session::flash('icon','success');

    return $user;
}
0 likes
4 replies
kbuczynski's avatar

Can we see your migration file with the table structure ?

I would think the id is not set as auto increment but I cant confirm without seeing the database table structure

DTech_Proz's avatar

Hi, thanks for replying.

Here is the migration table :

class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->integer('id');
        $table->string('name');
        $table->string('email')->index();
        $table->string('usertype')->nullable();
        $table->string('address');
        $table->string('country');
        $table->decimal('main_balance')->default("0.000")->unsigned();
        $table->decimal('deposit_balance')->default("0.000")->unsigned();
        $table->decimal('referral_balance')->default("0.000")->unsigned();
        $table->dateTime('bonus')->nullable();
        $table->tinyInteger('d_code')->nullable();
        $table->string('note')->nullable();
        $table->string('token')->nullable();
        $table->tinyInteger('active')->default('0');
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('users');
}
kbuczynski's avatar

As I suspected the id is just a integer if you would change it to be a increments the SQL error should resolve

1 like
josipfrljic's avatar

Easy fix is add nullable() on the end of column, reason is that psql expects some value but your column is empty Example fix $table->string('something')->nullable();

Please or to participate in this conversation.