skip to Main Content

I have like this migration file:

Schema::create('posts', function($table)
{
    $table->engine = 'InnoDB';
    $table->increments('id')->unsigned();
    $table->string('title');
    $table->text('description')->nullable();
    $table->integer('sort_order')->default(0);
    $table->boolean('status')->default(0);
});

And I have also seeder:

class SeedPostsTable extends Seeder
{
    public function run()
    {
        $posts = $this->getPosts();

        foreach ($posts as $title => $column) {
            $posts[] = [
              'title' => $title,
              'slug' => Str::slug($title),
            ];
        }

        Post::insert($posts);
        Post::query()->update(['sort_order' => DB::raw('`id`')]);
    }
}

As database driver I use Postgres and when I run this seeder get like this error:

SQLSTATE[42883]: Undefined function: 7 ERROR:  operator does not exist: `integer`
LINE 1: update "posts" set "sort_order" = `id`
HINT:  No operator matches the given name and argument type. You might need to add an explicit type cast. (SQL: update "posts" set "sort_order" = `id`)

I can’t set public $incrementing = false; and protected $keyType = 'string'; in my model because I need integer data type as ID column.

How I can fix this error?

2

Answers


  1. Post::query()->update(['sort_order' => DB::raw('`id`')]);
    

    You should use:

    Post::query()->update(['sort_order' => DB::raw('"id"')]);
    
    Login or Signup to reply.
  2. I guess the problem is in a mismatch of the data types:

    In Laravel $table->increments('id') is a big unsigned integer, not an integer. Make sort_order the same type:

    Schema::create('posts', function($table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id')->unsigned();
        $table->string('title');
        $table->text('description')->nullable();
        $table->unsignedBigInteger('sort_order')->default(0);
        $table->boolean('status')->default(0);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search