How would I set a database auto increment field that is not a primary key on the creation method?
The only way is using a raw query?
DB::statement('ALTER TABLE table CHANGE field field INT(10)AUTO_INCREMENT');
It is not implemented to do so. However, I found this over at the Laravel Forums:
Schema::table('table', function(Blueprint $t) {
// Add the Auto-Increment column
$t->increments("some_column");
// Remove the primary key
$t->dropPrimary("table_some_column_primary");
// Set the actual primary key
$t->primary(array("id"));
});
This is not tested but should work. I am not sure about how Laravel calls their primary keys, maybe you have to check that first and adapt the dropPrimary()
line in order to make it work.
index_name
. $t->dropPrimary()
. Laravel auto-detects the name of the primary key index for the specific column. I am using Laravel 5.5 with PostgreSQL
Apr 9, 2018 at 9:50
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
$table->uuid("id");
$table->increments("aux_id");
// other colums definitions ...
//auto increment needs to be an index (not necessarily primary) $table->index(array("aux_id"));
//after setting "normal" index, drop primary $table->dropPrimary();
//add primary to the field you want $table->primary("id");
I do not feel safe doing some workaround at migrations, so I did this inside model file:
/**
* Setup model event hooks
*/
public static function boot()
{
parent::boot();
self::creating(function ($model) {
$model->field_here = $model->max('field_here') + 1;
});
}
If you want disable autoincrementing add this at the beginning of the model file:
public $incrementing = FALSE;
Schema::table('table_name', function(Blueprint $table) {
DB::statement('ALTER TABLE table_name ADD column_name INT NOT NULL AUTO_INCREMENT AFTER after_column_name, ADD INDEX (column_name)');
});
The current answer does not work, auto-incrementing columns must be primary keys. I recommend using firstOrCreate
when inserting to get the same level of uniqueness (provided you still want an auto-incrementing key available).
i was using pgsql
, i achieved using modifier generatedAs()
with laravel 9
Schema::create('form_section', function (Blueprint $table) {
$table->id()->from(100);
$table->unsignedInteger('form_id');
$table->foreign('form_id')->references('id')->on('forms');
$table->unsignedInteger('section_id');
$table->foreign('section_id')->references('id')->on('sections');
$table->unsignedInteger('section_sequence')->generatedAs(); //solution
$table->timestamps($precision = 0);
});
gerneratedAs()
with a starting value? for example starting att 1000 and up from there?
Oct 31, 2022 at 21:26
I had a problem like yours, i could resolve It so Try This Example It Works:
Schema::create('etendu', function (Blueprint $table) {
$table->Increments('id_etendu');
$table->Integer('TypeActivite');
$table->Integer('id_activite')->unsigned();
$table->Integer('id_wilaya')->unsigned();
$table->Integer('id_user')->unsigned();
// autoIncrements needs index
$table->index(['id_etendu']);
// Remove the primary key
$table->dropPrimary("id_etendu");
$table->foreign('id_activite')
->references('id')
->on('activite')
->onCascade('delete');
$table->foreign('id_wilaya')
->references('id')
->on('wilayas')
->onCascade('delete');
$table->foreign('id_user')
->references('id')
->on('user')
->onCascade('delete');
// make them unique
$table->unique(['id_etendu','id_activite','id_wilaya']);
// make it primary key
$table->primary('id_etendu');
});