【问题标题】:laravel Undefined table: 7 ERROR:laravel 未定义表:7 错误:
【发布时间】:2018-08-19 13:14:30
【问题描述】:

我正在开发一个新的 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();
     }

   }
}

思路是显示一个简单的表格,选择一个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" = $...
^

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 搜索,我也无法确定数据库表名实际传递给连接和 SQL 语句的位置。

【问题讨论】:

    标签: postgresql pdo laravel-5.5


    【解决方案1】:

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

    protected $table = 'database.table';

    因此,如果您的数据库名为 test 并且表为 nbnco,它将如下所示:

    protected $table = 'test.nbnco'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      • 2019-07-20
      • 2017-11-23
      • 2018-06-27
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多