在迁移中设置自动增量字段从 1000 开始 laravel 5.1
Set Auto Increment field start from 1000 in migration laravel 5.1
我需要从用户 table 中的 1000 开始我的 ID,如何为此创建迁移。
我当前的迁移是:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id'); // how can I start this from 1000
$table->integer('qualification_id')->nullable();
$table->integer('experience_id')->nullable();
});
}
应该是这样的(未测试)
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
class MyTableMigration extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$statement = "ALTER TABLE MY_TABLE AUTO_INCREMENT = 111111;";
DB::unprepared($statement);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
更新
//Your migrations here:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->integer('qualification_id')->nullable();
$table->integer('experience_id')->nullable();
});
//then set autoincrement to 1000
//after creating the table
DB::update("ALTER TABLE users AUTO_INCREMENT = 1000;");
Prashant 的方法没有问题。
但如前所述,不要将 use DB;
放在文件的顶部。
这里是 php artisan migrate
之后的结果
大多数 tables 使用从下一个最大整数开始递增的增量。
总是可以插入一个高于当前自动递增索引的整数。自动递增索引将自动从新值 +1 开始。
所以,如果你有一个新生成的 table 你当前的索引是 0,下一个键将是 0 + 1 = 1。
我们要的是一个从1000开始的主键,所以我们做的是插入一条id值为999的记录,这样下次插入就会变成1000。
在代码中:
$startId = 1000;
DB::table('users')->insert(['id'=> $startId - 1]);
DB::table('users')->where('id',$startId - 1)->delete();
现在你有一个空的 table,下一个插入 ID 应该是 1000。
请注意,如果您有值要播种到 table 中且 id 值小于 startId
,您需要在执行这些语句之前 .否则数据库会抛出约束冲突错误。
这应该与数据库无关,但如果有一个数据库不遵循这个自动增量规则,我很乐意听到它。
迁移以创建 table 并将其 auto-increment 值设置为 Laravel 5.5
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('qualification_id')->nullable();
$table->integer('experience_id')->nullable();
});
// Here's the magic
\DB::statement('ALTER TABLE table_name AUTO_INCREMENT = 1000;');
}
DB::statement()
可用于执行您需要的任何单个 SQL 语句。
//Your migrations here:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->integer('qualification_id')->nullable();
$table->integer('experience_id')->nullable();
});
//then set autoincrement to 1000
//after creating the table
DB::update("ALTER TABLE users AUTO_INCREMENT = 1000;");
我们需要添加前缀table。所以我们需要替换行
DB::update("ALTER TABLE users AUTO_INCREMENT = 1000;");
下面两行:
$prefix = DB::getTablePrefix();
DB::update("ALTER TABLE ".$prefix."users AUTO_INCREMENT = 1000;");
在 Laravel 8 中,您只能将 from()
用于 MySQL / PostgreSQL:
Set the starting value of an auto-incrementing field (MySQL /
PostgreSQL)
$table->id()->from(...);
这个 startingValue()
方法也有效,但我没有在文档中的任何地方看到这个。
$table->id()->startingValue(...);
mysql 的底层使用:
public function compileAutoIncrementStartingValues(Blueprint $blueprint)
{
return collect($blueprint->autoIncrementingStartingValues())->map(function ($value, $column) use ($blueprint) {
return 'alter table '.$this->wrapTable($blueprint->getTable()).' auto_increment = '.$value;
})->all();
}
我需要从用户 table 中的 1000 开始我的 ID,如何为此创建迁移。
我当前的迁移是:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id'); // how can I start this from 1000
$table->integer('qualification_id')->nullable();
$table->integer('experience_id')->nullable();
});
}
应该是这样的(未测试)
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
class MyTableMigration extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$statement = "ALTER TABLE MY_TABLE AUTO_INCREMENT = 111111;";
DB::unprepared($statement);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
更新
//Your migrations here:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->integer('qualification_id')->nullable();
$table->integer('experience_id')->nullable();
});
//then set autoincrement to 1000
//after creating the table
DB::update("ALTER TABLE users AUTO_INCREMENT = 1000;");
Prashant 的方法没有问题。
但如前所述,不要将 use DB;
放在文件的顶部。
这里是 php artisan migrate
大多数 tables 使用从下一个最大整数开始递增的增量。
总是可以插入一个高于当前自动递增索引的整数。自动递增索引将自动从新值 +1 开始。
所以,如果你有一个新生成的 table 你当前的索引是 0,下一个键将是 0 + 1 = 1。
我们要的是一个从1000开始的主键,所以我们做的是插入一条id值为999的记录,这样下次插入就会变成1000。
在代码中:
$startId = 1000;
DB::table('users')->insert(['id'=> $startId - 1]);
DB::table('users')->where('id',$startId - 1)->delete();
现在你有一个空的 table,下一个插入 ID 应该是 1000。
请注意,如果您有值要播种到 table 中且 id 值小于 startId
,您需要在执行这些语句之前 .否则数据库会抛出约束冲突错误。
这应该与数据库无关,但如果有一个数据库不遵循这个自动增量规则,我很乐意听到它。
迁移以创建 table 并将其 auto-increment 值设置为 Laravel 5.5
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('qualification_id')->nullable();
$table->integer('experience_id')->nullable();
});
// Here's the magic
\DB::statement('ALTER TABLE table_name AUTO_INCREMENT = 1000;');
}
DB::statement()
可用于执行您需要的任何单个 SQL 语句。
//Your migrations here:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->integer('qualification_id')->nullable();
$table->integer('experience_id')->nullable();
});
//then set autoincrement to 1000
//after creating the table
DB::update("ALTER TABLE users AUTO_INCREMENT = 1000;");
我们需要添加前缀table。所以我们需要替换行
DB::update("ALTER TABLE users AUTO_INCREMENT = 1000;");
下面两行:
$prefix = DB::getTablePrefix();
DB::update("ALTER TABLE ".$prefix."users AUTO_INCREMENT = 1000;");
在 Laravel 8 中,您只能将 from()
用于 MySQL / PostgreSQL:
Set the starting value of an auto-incrementing field (MySQL / PostgreSQL)
$table->id()->from(...);
这个 startingValue()
方法也有效,但我没有在文档中的任何地方看到这个。
$table->id()->startingValue(...);
mysql 的底层使用:
public function compileAutoIncrementStartingValues(Blueprint $blueprint)
{
return collect($blueprint->autoIncrementingStartingValues())->map(function ($value, $column) use ($blueprint) {
return 'alter table '.$this->wrapTable($blueprint->getTable()).' auto_increment = '.$value;
})->all();
}