[英]Laravel SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value
I'm working on a Laravel application for a while now, but when I run php artisan migrate
now, all out of a sudden I always get the error: 我现在在Laravel应用程序上工作了一段时间,但是当我现在运行
php artisan migrate
migration时,突然之间我总会收到错误消息:
SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into `migrations` (`migration`, `batch`) values (2019_06_05_080701_create_departments_table, 22))
and I have no clue why this happens since thsi never happened before. 我不知道为什么会这样,因为以前从未发生过。 When I try to migrate a specific migration I get the same error, so what is going on here?
当我尝试迁移特定迁移时,会遇到相同的错误,那么这是怎么回事?
The migration looks like this: 迁移如下所示:
public function up()
{
Schema::create('departments', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('company_id');
$table->string('name');
$table->string('p_number')->nullable();
$table->string('address')->nullable();
$table->string('zipcode')->nullable();
$table->string('city')->nullable();
$table->string('contact_name')->nullable();
$table->string('contact_email')->nullable();
$table->string('contact_phone')->nullable();
$table->timestamps();
});
}
Result of SHOW COLUMNS FROM migrations;
SHOW COLUMNS FROM migrations;
结果SHOW COLUMNS FROM migrations;
Fire this query should work for autoincrement, that's the reason value of id is not getting auto incrementing 触发此查询应适用于自动递增,这是id值未自动递增的原因
ALTER TABLE migrations MODIFY id INTEGER NOT NULL AUTO_INCREMENT;
Note: If there are ids with 0 then you should change it to some other unique id value, and later get the max value of id by query and set autoincrement to +1
注意:如果ID为0,则应将其更改为其他唯一ID值,然后通过查询获取ID的最大值,并将autoincrement设置为+1
SET @new_index = (SELECT MAX(id) FROM migrations );
SET @sql = CONCAT('ALTER TABLE migrations AUTO_INCREMENT = ', @new_index);
PREPARE st FROM @sql;
EXECUTE st;
After a lot of research and good answers I found this solution, which solved it for me: 经过大量研究和良好答案,我找到了这个解决方案,它为我解决了:
ALTER TABLE migrations CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT = 123456;
The solution is based on this answer from a similiar question- Thanks to @Dracula Predator for the good hint. 该解决方案基于此 ,从一个类似question-感谢答案@Dracula捕食者的很好的提示。
You have been getting this error because probably you have modified migrations table. 您一直收到此错误,因为您可能已修改了迁移表。 You can try few options
您可以尝试几种选择
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.