Initial Value Sequences Postgresql - Laravel 5.1

3

Hello, I'm running some tests in my application, but I'm having the following problem: After migrate and db:seed , whenever I'm going to insert some registry by applying a duplicate primary key error. I have already seen that this error is due to increment continuing in 1 even after inserting data with db:seed . Is there any way I can set this value manually automatically, without having to go from table to table and changing?

Seed:

 public function run()
    {
        Model::unguard();

        App\Models\Admin\Profissao::create(['id'                       => '1', 
            'titulo'                => 'Engenheiro(a)',
        ]);

        App\Models\Admin\Profissao::create(['id'                       => '2', 
            'titulo'                => 'Tecnólogo(a)',
        ]);

        Model::reguard();
    }

Migration:

public function up()
{
    Schema::create('profissoes', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('titulo', 16);
        $table->longText('descricao')->nullable();
        $table->timestamps();
    });
}
    
asked by anonymous 11.01.2016 / 13:43

1 answer

4

The problem is with your Seeder:

 public function run()
 {
    Model::unguard();
    App\Models\Admin\Profissao::create(['titulo' => 'Engenheiro(a)']);
    App\Models\Admin\Profissao::create(['titulo' => 'Tecnólogo(a)']);
    Model::reguard();
}

In this way, you will not specify the value of the ID column manually.

The problem is that, unlike MySQL, PostgreSQL uses sequences. These sequences are separate structures from the table. In MySQL, when you insert a record with the ID column set, the table automatically moves to the next value when it auto-increments. In PostgreSQL, the sequence structure is not aware that the value was entered because you did not use the string.

Since you do not specify the value, Laravel will use the string for you and it will automatically be incremented.

    
11.01.2016 / 14:06