简体   繁体   English

流明数据库播种错误

[英]Lumen database seeding error

I'm learning back-end development with Laravel's Lumen framework, and I am writing database seeding class following Laravel's documentation . 我正在学习使用Laravel的Lumen框架进行后端开发,并且正在按照Laravel的文档编写数据库种子类。 Below are the codes: 以下是代码:

Model app\\Photo.php 模型app\\Photo.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Photo extends Model
{
    protected $fillable = [
        'link', 'category_id', 'date'
    ];
}

Model factory database\\factories\\ModelFactory.php 模型工厂database\\factories\\ModelFactory.php

$factory->define(App\Photo::class, function (Faker\Generator $faker) {
    return [
        'link' => 'https://placehold.it/' . mt_rand(200, 400),
        'category_id' => rand(1, 6),
        'date' => date("Y-m-d", mt_rand(1263618000, 1481428800))
    ];
});

DatabaseSeeder and PhotoTableSeeder classes database\\seeds\\DatabaseSeeder.php DatabaseSeederPhotoTableSeederdatabase\\seeds\\DatabaseSeeder.php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
         $this->call('PhotoTableSeeder');
    }
}

class PhotoTableSeeder extends Seeder
{
    public function run()
    {
        factory(App\Photo::class, 10)->create();
    }
}

Previously the table name in MySQL database was photos and the seeding works perfectly using the command php artisan db:seed : 以前,MySQL数据库中的表名是photos ,使用php artisan db:seed命令可以很好地进行php artisan db:seed

Seeded: PhotoTableSeeder 种子:PhotoTableSeeder

But when I changed the table name to photo and run the same command, these errors occured: 但是,当我将表名称更改为photo并运行相同的命令时,发生了以下错误:

[Illuminate\\Database\\QueryException] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.photos' doesn't exist (SQL: insert into `photos` (`link`, `category_id`, `date`) values ( https://placehold.it/259 , 3, 2012-05-28)) [Illuminate \\ Database \\ QueryException] SQLSTATE [42S02]:找不到基表或视图:1146表'homestead.photos'不存在(SQL:插入到`photos`中(`link`,`category_id`,`date` )值( https://placehold.it/259,3,2012-05-28 ))

[PDOException] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.photos' doesn't exist [PDOException] SQLSTATE [42S02]:找不到基表或视图:1146表'homestead.photos'不存在

I don't know why the old table name is still called. 我不知道为什么仍然使用旧表名。 I guess it was saved somewhere or am I missing something, I can't figure out the cause of the error. 我猜它保存在某个地方,或者我丢失了什么,我无法弄清错误的原因。

Eloquent looks for the Plural form of the Model Class Name as the table in the database. Eloquent在数据库表中查找Model Class Name的复数形式。 So in this case it was expecting to find a table called "Photos" 因此,在这种情况下,它期望找到一个名为“照片”的表

if for some reason you need to name the table Photo you can always override the default by adding 如果由于某种原因需要命名表格“照片”,则始终可以通过添加来覆盖默认设置

protected $table = 'photo';

to your photo model class. 到您的照片模型课。 I tend to do this even when I am using the default conventions just as a note to myself exactly which table a model is referencing 即使当我使用默认约定时,我也倾向于这样做,以作为我自己对模型所引用的表的确切说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM