gpt4 book ai didi

php - Laravel 播种错误 - 错误的表名

转载 作者:可可西里 更新时间:2023-11-01 07:05:16 25 4
gpt4 key购买 nike

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

我的表是 institution_school 而不是 institution_schools,这是 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');
});
}
}

最佳答案

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

protected $table = "institution_school";

关于php - Laravel 播种错误 - 错误的表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24580175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com