K
K
Kerm2019-10-18 17:03:07
Laravel
Kerm, 2019-10-18 17:03:07

Problem with migration, how to create primery and unique keys with their own names?

I am just starting to learn laravel, I wrote the following lines in migration:

public function up()
    {
        $tableName = 'nx_path';

        Schema::create($tableName, function (Blueprint $table) {
            $table->smallInteger('id',true, true)->primary();
            $table->smallInteger('node',false, true)->unique();
            $table->string('title', 255)->default('');
            $table->string('keywords', 255)->default('');
            $table->string('description', 255)->default('');
            $table->string('path', 255)->default('');
            $table->text('anchor_pattern');
            $table->text('title_synonyms');
        });

        DB::statement("ALTER TABLE `$tableName` comment 'Ноды и их типы'");
    }

Firstly, I get an error:
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table `nx_path` add primary key `nx_path_id_primary`(`id`))
Secondly, I can’t understand whether you can specify your names for the keys in the process of creating them?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daria Motorina, 2019-10-18
@Kerm

Use the bigIncrements() function to create an id instead of a small integer (usually not used for id) this will not give much savings UPD . I figured out the problem - why it turned out to double add the primary key: Under the hood $table->smallInteger('id', true, true) will create id as the primary key due to the autoIncrements = true flag (second argument), so calling primary() created the key again. You can simply do:
$table->smallInteger('id', true, true);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question