Postgresql laravel未定义表:7错误:
我正在一个新的laravel 5.5站点上工作,当应用程序尝试连接到Postgresql数据库时,我遇到了一个我不理解的异常 “我的环境”文件中的所有设置对于数据库连接都是正确的 我正在使用迁移创建表,当我运行php artisan migrate时,表将被创建 在/app下,我有一个文件NbnCo.php,其中包含Postgresql laravel未定义表:7错误:,postgresql,pdo,laravel-5.5,Postgresql,Pdo,Laravel 5.5,我正在一个新的laravel 5.5站点上工作,当应用程序尝试连接到Postgresql数据库时,我遇到了一个我不理解的异常 “我的环境”文件中的所有设置对于数据库连接都是正确的 我正在使用迁移创建表,当我运行php artisan migrate时,表将被创建 在/app下,我有一个文件NbnCo.php,其中包含 class NbnCo extends Model { protected $fillable=[ // All the db table fields are her
class NbnCo extends Model
{
protected $fillable=[
// All the db table fields are here
];
}
namespace App\Http\Controllers;
use App\NbnCo;
use Illuminate\Http\Request;
use App\Http\Requests;
class NbnCoController extends Controller
{
public function showForm()
{
return view('upload');
}
public function store(Request $request)
{
//get file
$upload=$request->file('upload-file');
$filePath=$upload->getRealPath();
//open and read
$file=fopen($filePath, 'r');
$header= fgetcsv($file);
$processedHeader=[];
//validate
foreach ($header as $key => $value) {
$lheader=strtolower($value);
array_push($processedHeader, $lheader);
}
while($columns=fgetcsv($file))
{
if($columns[0]=="")
{
continue;
}
$data=array_combine($processedHeader, $columns);
// Table update
$nbn_location_identifier=$data['nbn_location_identifier'];
// the rest of the folumns follow the same style
$nbn = NbnCo::firstOrNew(['nbn_location_identifier'=>$nbn_location_identifier]);
// the rest are added in the same way
$nbn->save();
}
}
}
在/app/Http/Controllers下,我有NbnCoController.php
它包含
class NbnCo extends Model
{
protected $fillable=[
// All the db table fields are here
];
}
namespace App\Http\Controllers;
use App\NbnCo;
use Illuminate\Http\Request;
use App\Http\Requests;
class NbnCoController extends Controller
{
public function showForm()
{
return view('upload');
}
public function store(Request $request)
{
//get file
$upload=$request->file('upload-file');
$filePath=$upload->getRealPath();
//open and read
$file=fopen($filePath, 'r');
$header= fgetcsv($file);
$processedHeader=[];
//validate
foreach ($header as $key => $value) {
$lheader=strtolower($value);
array_push($processedHeader, $lheader);
}
while($columns=fgetcsv($file))
{
if($columns[0]=="")
{
continue;
}
$data=array_combine($processedHeader, $columns);
// Table update
$nbn_location_identifier=$data['nbn_location_identifier'];
// the rest of the folumns follow the same style
$nbn = NbnCo::firstOrNew(['nbn_location_identifier'=>$nbn_location_identifier]);
// the rest are added in the same way
$nbn->save();
}
}
}
其思想是显示一个简单的表单,并选择一个CSV文件上载到nbn数据库中的nbnco表中
我得到2个PDO错误
PDOException in Connection.php line 337:
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "nbn_cos" does not
exist
LINE 1: select * from "nbn_cos" where ("nbn_location_identifier" = $...
^
及
我不明白laravel试图连接的nbn_cos表来自何方。我也不明白nbn_cos关系是从哪里来的
我所能找到的最好的结果是,我的类的命名和数据库表的名称有问题。即使进行了大量的Google搜索,我也无法确定数据库表名实际上是如何传递给connection和SQL语句的。Laravel和Postgres要求在模型中包含该表以及表名
protected$table='database.table'代码>
因此,如果您的数据库名为test
,而表名为nbnco
,则它将类似于:
protected$table='test.nbnco'