无法使用 SQLSTATE PHP Artisan Migrate [42000]
Can't do PHP Artisan Migrate with SQLSTATE [42000]
我只想 PHP Artisan 迁移,我刚刚完成 table 迁移,但出现错误
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'modified_date'
(SQL: create table `dso` (`id` bigint unsigned not null auto_increment primary key,
`id_dso` bigint unsigned not null, `id_rso` bigint unsigned not null,
`id_focus` bigint unsigned not null, `id_wilayah` bigint unsigned not null, `id_grup_wilayah` bigint unsigned not null,
`nama_dso` varchar(255) not null, `created_by` varchar(255) not null,
`created_date` timestamp not null, `modified_by` varchar(255) not null,
`modified_date` timestamp not null, `status` tinyint(1) not null,
`created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
我检查了我的完成情况,没有错误或任何拼写错误
public function up()
{
Schema::create('dso', function (Blueprint $table) {
$table->id();
$table->foreignId('id_dso')->constrained('dso_table_name');
$table->foreignId('id_rso')->constrained('rso_table_name');
$table->foreignId('id_focus')->constrained('focus_table_name');
$table->foreignId('id_wilayah')->constrained('wilayah_table_name');
$table->foreignId('id_grup_wilayah')->constrained('grup_wilayah_table_name');
$table->string('nama_dso');
$table->string('created_by');
$table->timestamp('created_date',$precision = 0);
$table->string('modified_by');
$table->timestamp('modified_date',$precision = 0);
$table->boolean('status');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('dso');
}
}
我没有添加可为空的,因为所有列都必须填写
我正在使用 Laravel 8.6 并在 Windows 上使用 XAMPP 和 MariaDB
你的问题是你使用了timestamps()
instead of timestamp()
.
所以,你的迁移应该是这样的:
public function up()
{
Schema::create('dso', function (Blueprint $table) {
$table->id();
$table->foreignId('id_dso')->unique();
$table->id('id_rso');
$table->id('id_focus');
$table->id('id_wilayah');
$table->id('id_grup_wilayah');
$table->string('nama_dso');
$table->string('created_by');
$table->timestamp('created_date');
$table->string('modified_by');
$table->timestamp('modified_date');
$table->boolean('status');
$table->timestamps();
});
}
检查 documentation 中可用的方法。
foreignId
方法创建了一个UNSIGNED BIGINT等价列,所以不需要使用unsignedBigInteger
。换掉就好了
public function up()
{
Schema::create('dso', function (Blueprint $table) {
$table->id();
$table->foreignId('id_dso');
$table->foreignId('id_rso');
$table->foreignId('id_focus');
$table->foreignId('id_wilayah');
$table->foreignId('id_grup_wilayah');
$table->string('nama_dso');
$table->string('created_by');
$table->timestamp('created_date', $precision = 0);
$table->string('modified_by');
$table->timestamp('modified_date', $precision = 0);
$table->boolean('status');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('dso');
}
编辑:如果您的 table 名称与 Laravel 的约定不匹配,您可以通过将其作为参数传递给约束方法来指定 table 名称:
public function up()
{
Schema::create('dso', function (Blueprint $table) {
$table->id();
$table->foreignId('id_dso')->constrained('dso_table_name');
$table->foreignId('id_rso')->constrained('rso_table_name');
$table->foreignId('id_focus')->constrained('focus_table_name');
$table->foreignId('id_wilayah')->constrained('wilayah_table_name');
$table->foreignId('id_grup_wilayah')->constrained('grup_wilayah_table_name');
$table->string('nama_dso');
$table->string('created_by');
$table->timestamp('created_date', $precision = 0);
$table->string('modified_by');
$table->timestamp('modified_date', $precision = 0);
$table->boolean('status');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('dso');
}
我认为问题在于您对同一列声明了两次。
$table->foreignId('id_dso');
创建一个无符号位整数。
之后你再次使用 $table->unsignedBigInteger('id_rso');
做同样的事情。
我只想 PHP Artisan 迁移,我刚刚完成 table 迁移,但出现错误
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'modified_date'
(SQL: create table `dso` (`id` bigint unsigned not null auto_increment primary key,
`id_dso` bigint unsigned not null, `id_rso` bigint unsigned not null,
`id_focus` bigint unsigned not null, `id_wilayah` bigint unsigned not null, `id_grup_wilayah` bigint unsigned not null,
`nama_dso` varchar(255) not null, `created_by` varchar(255) not null,
`created_date` timestamp not null, `modified_by` varchar(255) not null,
`modified_date` timestamp not null, `status` tinyint(1) not null,
`created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
我检查了我的完成情况,没有错误或任何拼写错误
public function up()
{
Schema::create('dso', function (Blueprint $table) {
$table->id();
$table->foreignId('id_dso')->constrained('dso_table_name');
$table->foreignId('id_rso')->constrained('rso_table_name');
$table->foreignId('id_focus')->constrained('focus_table_name');
$table->foreignId('id_wilayah')->constrained('wilayah_table_name');
$table->foreignId('id_grup_wilayah')->constrained('grup_wilayah_table_name');
$table->string('nama_dso');
$table->string('created_by');
$table->timestamp('created_date',$precision = 0);
$table->string('modified_by');
$table->timestamp('modified_date',$precision = 0);
$table->boolean('status');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('dso');
}
}
我没有添加可为空的,因为所有列都必须填写 我正在使用 Laravel 8.6 并在 Windows 上使用 XAMPP 和 MariaDB
你的问题是你使用了timestamps()
instead of timestamp()
.
所以,你的迁移应该是这样的:
public function up()
{
Schema::create('dso', function (Blueprint $table) {
$table->id();
$table->foreignId('id_dso')->unique();
$table->id('id_rso');
$table->id('id_focus');
$table->id('id_wilayah');
$table->id('id_grup_wilayah');
$table->string('nama_dso');
$table->string('created_by');
$table->timestamp('created_date');
$table->string('modified_by');
$table->timestamp('modified_date');
$table->boolean('status');
$table->timestamps();
});
}
检查 documentation 中可用的方法。
foreignId
方法创建了一个UNSIGNED BIGINT等价列,所以不需要使用unsignedBigInteger
。换掉就好了
public function up()
{
Schema::create('dso', function (Blueprint $table) {
$table->id();
$table->foreignId('id_dso');
$table->foreignId('id_rso');
$table->foreignId('id_focus');
$table->foreignId('id_wilayah');
$table->foreignId('id_grup_wilayah');
$table->string('nama_dso');
$table->string('created_by');
$table->timestamp('created_date', $precision = 0);
$table->string('modified_by');
$table->timestamp('modified_date', $precision = 0);
$table->boolean('status');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('dso');
}
编辑:如果您的 table 名称与 Laravel 的约定不匹配,您可以通过将其作为参数传递给约束方法来指定 table 名称:
public function up()
{
Schema::create('dso', function (Blueprint $table) {
$table->id();
$table->foreignId('id_dso')->constrained('dso_table_name');
$table->foreignId('id_rso')->constrained('rso_table_name');
$table->foreignId('id_focus')->constrained('focus_table_name');
$table->foreignId('id_wilayah')->constrained('wilayah_table_name');
$table->foreignId('id_grup_wilayah')->constrained('grup_wilayah_table_name');
$table->string('nama_dso');
$table->string('created_by');
$table->timestamp('created_date', $precision = 0);
$table->string('modified_by');
$table->timestamp('modified_date', $precision = 0);
$table->boolean('status');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('dso');
}
我认为问题在于您对同一列声明了两次。
$table->foreignId('id_dso');
创建一个无符号位整数。
之后你再次使用 $table->unsignedBigInteger('id_rso');
做同样的事情。