laravel报错的问题 42S01 420004 42S22]等...
laravel避坑笔记
来源网络
1.Laravel 5.4: Specified key was too long error
原因:从LV 5.4起数据库默认字符集为utf8mb4(包括了对 emojis 的支持)。如果使用的是 MySQL v5.7.7 或更高版本不需要做什么修改。
使用更早版本的MySQL数据库(包括MariaDB)的童鞋可以这样修改了,修改文件 /project/app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Schema; // 注意要引入命名空间
public function boot()
{
Schema::defaultStringLength(191); // 针对 早期 mysql 数据迁移
}
再重新使用迁移命令:
php artisan migrate
2.SQLSTATE[42S01]: Base table or view already exists: 1050
迁移数据时表已存在,
解决办法:
删除已存在的表,然后重新迁移。
3.PDOException::(“SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for ‘published_at’”)
- 修改方法一:vim config/database.php
'mysql' => [
'driver' => 'mysql',
.... ...
'prefix' => '',
'strict' => true, // 修改这里
'engine' => null,
],
修改为:
'mysql' => [
'driver' => 'mysql',
.... ...
'prefix' => '',
'strict' => false, // 修改这里
'engine' => null,
],
设置可为空的时间戳:
$table->nullableTimestamps()
设置默认时间戳
$table->timestamps('created_at');// 会生成created_at\updated_at字段
4.PDOException::(“SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘created_at’ in ‘field list’”)
此问题是由于 table 迁移时没有设置默认时间戳字段,但使用的是 factory 方法 生成 seed 数据,可以修改
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('slug')->unique();
$table->string('title');
$table->text('content');
$table->timestamps(); // 添加这行
});
}
重复make:migration
确切来说,这个也不算是坑,因为这个操作本身就是只需执行一次
但对于新手来说,可能无意间就重复执行多次,而 make:migration 时是不会报错的;
而在执行迁移时问题就来了:
解决办法就是删除或修改同一 table 的 schema 名称。
5.php artisan db:seed 表名变复数 单数
这可能和 Chinglish 有关,老外习惯把表名写为复数,所以干脆默认Model 对应的表名是这个英文单词的复数形式
因此,要在 model 里重写 $table 属性,如:
protected $table=’student’;
查找于网络,储存方便自己方便他人
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: