【问题标题】:Illuminate\Database\QueryException with message 'SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "contacts" does not existIlluminate\Database\QueryException 与消息'SQLSTATE [42P01]:未定义的表:7 错误:关系“联系人”不存在
【发布时间】:2021-12-20 09:10:37
【问题描述】:

我的代码是用 PHP 编写的,使用 Laravel 和 Postgresql 作为数据库。

我有一个模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    use HasFactory;
    protected $fillable = [
        'last_name',
        'first_name',
        'phone_number',
        'address',

    ];
    //protected $table = 'database.contact';
    protected $hidden = [

        'remember_token',
    ];
}

和迁移:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class Contact extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('contact ',function(Blueprint $table){
              $table->id();
              $table->string('first_name');
              $table->string('last_name');
              $table->integer('phone_number');
              $table->ipAddress('address')->nullable();
              $table->rememberToken();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
         Schema::dropIfExists('contact');
    }
}

但是当我在终端运行时:

php artisan tinker

然后:

Contact::create(['last_name' => 'joe', 'first_name' => 'ajoe.com', 'phone_number' => 88552])

我得到错误:

Illuminate\Database\QueryException with message 'SQLSTATE[42P01]: Undefined table: 7 ERROR: 关系“contacts”不存在 第1行:插入“联系人”(“last_name”,“first_name”,“phone_nu ... ^ (SQL: insert into "contacts" ("last_name", "first_name", "phone_number", "updated_at", "created_at") 值 (joe, ajoe.com, 88552, 2021-11-06 21:00:43 , 2021-11-06 21:00:43) 返回 "id")'

如果您知道为什么会发生这种情况,我很想知道 谢谢。

【问题讨论】:

标签: php laravel postgresql model tinker


【解决方案1】:

第一步:删除迁移中表名中多余的空格:

 Schema::create('contact ',function(Blueprint $table){

到:

 Schema::create('contact',function(Blueprint $table){

然后,在您的联系人模型中,设置 $table 属性:

class Contact extends Model
{
   

    /*
    |--------------------------------------------------------------------------
    | GLOBAL VARIABLES
    |--------------------------------------------------------------------------
    */

    protected $table = 'contact';

【讨论】:

  • 太棒了!谢谢
  • 如果此答案解决了问题,请将其标记为“已接受”,以便我们关闭此问题
【解决方案2】:

在模型中添加这一行

protected $table = 'contact';

【讨论】:

    【解决方案3】:

    将此行添加到您的模型protected $table = 'contact'; 或更改此行

    Schema::create('contact',function(Blueprint $table){

    Schema::create('contacts',function(Blueprint $table){ 
    

    又是migrate:fresh。您可以丢失您的数据,因此第一个使用起来更安全。但理想的表名应该是contacts

    【讨论】:

    • 非常感谢,但问题仍然存在。
    • 你用的是哪一个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2019-05-21
    • 2011-02-13
    • 2014-12-25
    • 2018-02-21
    • 1970-01-01
    相关资源
    最近更新 更多