首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >laravel未定义表:7错误:

laravel未定义表:7错误:
EN

Stack Overflow用户
提问于 2018-03-11 19:44:19
回答 1查看 2.8K关注 0票数 2

我正在一个新的laravel 5.5站点上工作,当应用程序试图连接到Postgresql数据库时,我得到了一个我不理解的异常。

我的env文件中的所有设置对于数据库连接都是正确的

我正在使用迁移创建表,当我运行php artisan migrate时,表就被创建了。

在/app下,我有一个包含以下内容的文件NbnCo.php

代码语言:javascript
复制
class NbnCo extends Model
{

    protected $fillable=[

// All the db table fields are here

];
}

在/app/Http/Controllers下,我有NbnCoController.php

它包含

代码语言:javascript
复制
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错误

代码语言:javascript
复制
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" = $...
^

代码语言:javascript
复制
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语句的。

EN

回答 1

Stack Overflow用户

发布于 2018-10-21 06:33:08

Laravel和Postgres要求模型中包含表以及表名。

protected $table = 'database.table';

因此,如果您的数据库名为test,而表为nbnco,那么它将类似于:

protected $table = 'test.nbnco'

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49219620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档