skip to Main Content

I was trying to add a new column as a foriegn key in existing table in laravel 9. But, it continuously giving SQL error. I want to create a simple category_id as a foreign_key in brand table.

The below code doesn’t work,

$table->integer('category_id')->unsigned()->nullable()->after('password');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('SET NULL');

2

Answers


  1. Your category_id must be of big integer type
    or
    create user column like this

    $table->unsignedBigInteger('device_id')->index()->nullable();

    and
    if your table exist create new migration class and alter table to add new column in your table

    Login or Signup to reply.
  2. try:

    $table->foreignId('category_id')->after('password');
    $table->foreign('category_id')->references('id')->on('categories')->onDelete('SET NULL');
    

    See more about DB Column Types.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search