php - 无法使用 SQLSTATE 执行 PHP Artisan 迁移 [42000]
问题描述
我只想进行 PHP Artisan Migrate,我刚刚完成了我的迁移表但有错误
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()
而不是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();
});
}
检查文档中的可用方法。
推荐阅读
- r - 根据两列中的信息向我的数据框添加一列
- iis - IIS反向代理内外的应用程序的CORS标头?
- c - 在c中读取没有FILE *的utmpx
- javascript - React Router Dom Link 有问题
- android - youtubeplayer 的 RecyclerView 上的 OnClickItem 出现异常
- java - org.mapstruct 不适用于基类
- d3.js - 根据输入范围更改 CSV 数据源
- typescript - 如何让泛型类型参数本身成为泛型
- spring-boot - 解析类路径时出错:/db/changelog/db.changelog-master.yaml
- java - 映射到 "" 而不是 null Jackson