Composite key in Laravel migrate

Asked

Viewed 143 times

2

I would like to create a double primary key with an auto increment field "id" and the "term", but it gives error and I do not know the correct way to do, if anyone can help me, I would appreciate it very much!

Migrate

Schema::create('disciplinas', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('periodo_letivos_id')->unsigned();
        $table->foreign('periodo_letivos_id')
            ->references('id')
            ->on('periodo_letivos')
            ->onDelete('cascade');
        $table->integer('cursos_id')->unsigned();
        $table->foreign('cursos_id')
            ->references('id')
            ->on('cursos')
            ->onDelete('cascade');
        $table->integer('periodo_regular')->nullable();
        $table->string('matriz_curricular')->nullable();
        $table->string('nome', 200);
        $table->string('abrev_nome', 15);
        $table->integer('professors_id')->unsigned();
        $table->foreign('professors_id')
            ->references('id')
            ->on('professors')
            ->onDelete('cascade');
        $table->string('aulas', 3)->nullable();
        $table->string('aulas_hora', 3)->nullable();
        $table->string('obs', 400)->nullable();
        $table->primary(['id', 'periodo_letivos_id']);
        $table->timestamps();
        $table->softDeletes();
    });
  • Try using $table->unsignedInteger('periodo_letivo_id');

  • I tried, but it didn’t work, I needed this field to be auto increment too. For example I cannot insert the same discipline in the same period 2 times!

  • Have you checked whether the two fields are in exactly the same typing ? int unsignd

  • So, if I leave $table->increments('id')->unsigned(); and $table->integer('periodo_letivos_id')->unsigned(); both as unsigned and $table->Primary(['id', 'periodo_letivos_id']); it doesn’t work, but if I put id as integer and unsigned accepts but then I miss auto-registration!

  • And if you leave the unsigned ones, I did right on the table, but this way it’s to work

  • He did not accept to add the constrait of academic periods without being unsigned!

  • Your bank is on which engine ? Myisam or Innodb

  • I am using Mysql as vejoqual engine?

  • I decided to leave the id without increment, will help me create a Rigger that works?

  • I need to create an auto increment

  • Try putting this in shchema of the table: $table->engine = 'Innodb', if not solve ai movo pro chat para gente tentar resolver

  • It didn’t work either, I’m trying to create a Rigger but it’s not working!

  • 1

    I solved otherwise, I changed the fields to unsigned and after creation I turned a command and changed the id structure to auto increment! wait for when to start the consultations to know

  • Great, if it doesn’t work, revisit the topic kk

Show 9 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.