我正在一个新的laravel 5.5站点上工作,当应用程序试图连接到Postgresql数据库时,我得到了一个我不理解的异常。
我的env文件中的所有设置对于数据库连接都是正确的
我正在使用迁移创建表,当我运行php artisan migrate时,表就被创建了。
在/app下,我有一个包含以下内容的文件NbnCo.php
class NbnCo extends Model
{
protected $fillable=[
// All the db table fields are here
];
}在/app/Http/Controllers下,我有NbnCoController.php
它包含
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();
}
}
}其思想是显示一个简单的表单,并选择要上载到nbn数据库中的nbnco表的CSV文件。
我收到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" = $...
^和
QueryException in Connection.php line 770:
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "nbn_cos" does not
exist
LINE 1: select * from "nbn_cos" where ("nbn_location_identifier" = $...
^ (SQL: select * from "nbn_cos" where ("nbn_location_identifier" =
LOC000012257222) limit 1)我不知道laravel试图连接的nbn_cos表来自何处。我也不明白nbn_cos关系是从哪里来的。
我所能找到的最好的情况是,我在类的命名和数据库表的名称上做了一些错误的事情。我无法计算出,即使使用了大量的Google搜索,数据库表名实际上是传递给connection和SQL语句的。
发布于 2018-10-21 06:33:08
Laravel和Postgres要求模型中包含表以及表名。
protected $table = 'database.table';
因此,如果您的数据库名为test,而表为nbnco,那么它将类似于:
protected $table = 'test.nbnco'
https://stackoverflow.com/questions/49219620
复制相似问题