마이그레이션 수준 5.1에서 자동 증분 필드 시작을 1000부터 설정
사용자 테이블에서 ID를 1000부터 시작해야 하는데 이에 대한 마이그레이션을 생성하려면 어떻게 해야 합니까?
현재 마이그레이션은 다음과 같습니다.
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;");
라라벨 8에서 사용할 수 있습니다.from()MySQL / Postgre에 한해SQL:
자동 증분 필드의 시작 값 설정(MySQL / Postgre)SQL)
$table->id()->from(...);
이것.startingValue()method도 효과가 있지만 설명서 어디에도 이것이 언급된 것을 보지 못했습니다.
$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();
}
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 문을 실행하는 데 사용할 수 있습니다.
대부분의 테이블은 다음으로 큰 정수에서 증가하는 증분으로 작동합니다.
현재 자동 증가 인덱스보다 높은 정수를 항상 삽입할 수 있습니다.그러면 자동 증가 인덱스가 새 값 +1부터 자동으로 따라옵니다.
따라서 새로 주조된 표가 있으면 현재 색인이 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();
이제 다음 삽입 ID가 1000이어야 하는 빈 테이블이 있습니다.
ID 값이 있는 테이블에 시드할 값이 있는 경우 <startId이 명령문을 실행하기 전에 먼저 실행해야 합니다.그렇지 않으면 데이터베이스에서 제약 조건 위반 오류가 발생합니다.
이것은 데이터베이스에 구애받지 않고 작동하지만, 이 자동 증가 규칙을 따르지 않는 데이터베이스가 있다면 그것에 대해 듣고 싶습니다.
//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;");
접두사 테이블 추가가 필요합니다.그래서 라인을 교체해야 합니다.
DB::update("ALTER TABLE users AUTO_INCREMENT = 1000;");
아래 2줄까지:
$prefix = DB::getTablePrefix();
DB::update("ALTER TABLE ".$prefix."users AUTO_INCREMENT = 1000;");
Prashant의 방법은 문제없이 작동합니다.하지만 앞서 말한 것처럼, 하지 마세요.use DB;파일 상단에 저장할 수 있습니다.
다음 결과는 다음과 같습니다.php artisan migrate
언급URL : https://stackoverflow.com/questions/34196045/set-auto-increment-field-start-from-1000-in-migration-laravel-5-1
'programing' 카테고리의 다른 글
| .map() 자바스크립트 ES6 지도? (0) | 2023.10.19 |
|---|---|
| C++ 표준 라이브러리가 OS 대신 컴파일러와 함께 번들로 제공되는 이유는 무엇입니까? (0) | 2023.10.19 |
| AngularJS : $scope.$watch가 사용자 지정 지시에 따라 $resource에서 가져온 값을 업데이트하지 않습니다. (0) | 2023.10.19 |
| Mysql: 유형이 비트인 열을 쿼리하는 방법은 무엇입니까? (0) | 2023.10.19 |
| 순수 자바스크립트에서 요소의 클래스를 토글하려면 어떻게 해야 합니까? (0) | 2023.10.19 |

