【问题标题】:Laravel seeding error - wrong table nameLaravel 播种错误 - 错误的表名
【发布时间】:2014-08-26 03:21:40
【问题描述】:

当我尝试为数据库播种时,Laravel 抛出此错误。

我的表是机构学校而不是机构学校,这是 Laravel 在错误中报告的。

 [Illuminate\Database\QueryException]
 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'eesdatabase.inst
 itution_schools' doesn't exist (SQL: insert into `institution_schools` (`sc
 hool_id`, `instituion_id`, `energy_id`, `year`, `updated_at`, `created_at`)
  values (38, 1, 1, 2005, 2014-07-04 19:38:41, 2014-07-04 19:38:41))

我尝试删除数据库并再次迁移和播种。 我尝试使用“php artisan:cache clear”重置 Laravel 缓存

有谁知道如何解决这个问题? 谢谢

<?php    
class InstitutionSchool extends Eloquent {


    protected $table = "institution_school";        
    protected $guarded = array('id');

    public function school() {
        return $this -> belongsTo('School');
    }

    public function institutio() {
        return $this -> belongsTo('Institution');
    }

    public function energy() {
        return $this -> belongsTo('Energy');
    }    
}
?>

<?php
class InstitutionSchoolTableSeeder extends DatabaseSeeder {
    public function run() {
        DB::table('institution_school') -> delete();

        $faker = $this -> getFaker();
        $schools = School::all();
        $institutions = Institution::all();
        $energies = Energy::all();

        foreach ($schools as $school) {
            for ($i = 0; $i < rand(1, 5); $i++) {
                $year = $faker -> randomNumber(2000, 2015);

                InstitutionSchool::create(array(
                'school_id' => $school -> id, 
                'instituion_id' => 1,
                'energy_id' => 1, 
                'year' => $year));
            }

        }
    }

}
?>

<?php    
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateInstitutionSchoolTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('institution_school', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('institution_id')->unsigned();
            $table->integer('school_id')->unsigned();
            $table->string('year');
            $table->string('other_source_name')->nullable();

            $table->integer('energy_id')->unsigned()->nullable();

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('institution_school', function(Blueprint $table)
        {
            Schema::drop('institution_school');
        });
    }    
}

【问题讨论】:

  • 现在有了代码,好多了。但老实说,我没有看到任何错误。显式表声明看起来没问题。
  • 我只想指出`Schema::drop('institution_school');` 不必在Schema::table('in... 中,但这不是你的错误的原因。

标签: php mysql laravel-4 seeding


【解决方案1】:

尝试在模型中明确定义表名。它可能会起作用。但这当然不是一个完整的解决方案,只是让它工作的黑客:D

protected $table = "institution_school";

【讨论】:

  • 不明白 - @Aleksandar,你的模型中已经有了这个。怎么解决的?
  • 粘贴前已评论。在其他表上,我没有该代码,我认为这并不重要。这就是解决方案!
【解决方案2】:

Laravel 假定您将 SQL 表命名为小写复数形式,并且模型是单数形式的 PascalCase。您可能需要重命名您的表格以包含“s”。

【讨论】:

  • protected $table = "institution_school"; 应该覆盖它。由于这是一种带有额外数据的数据透视表,因此用“s”命名它没有多大意义。
【解决方案3】:

在您的InstitutionSchoolTableSeeder 中,您有以下内容:

DB::table('institution_school') -&gt; delete();

应该是:

DB::table('institution_school') -&gt; truncate();

-&gt;delete() 表消失了,它会抛出错误。使用-&gt;truncate(),该表将被清空,但仍会存在,因此您可以对其进行播种。

【讨论】:

    猜你喜欢
    • 2014-12-08
    • 2014-11-26
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 2019-11-14
    • 2017-08-02
    • 2017-07-21
    • 1970-01-01
    相关资源
    最近更新 更多