【问题标题】:Laravel - SQLSTATE[42S22]: Column not found: 1054 Unknown column [closed]Laravel - SQLSTATE [42S22]:找不到列:1054 未知列 [关闭]
【发布时间】:2015-12-24 01:06:58
【问题描述】:

您好,我收到此错误 Illuminate\Database\QueryException 并带有消息 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'posts.user_id' in 'where clause' (SQL: select * frompostswhereposts.user_id= 1 andposts.user_idis not null)' 我不知道为什么如果在我的数据库中我没有user_id,我有id_user...

这是我的迁移表

<?php

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

class CreateUsersTable extends Migration
{

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('user')->unique();
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->string('img');

            $table->rememberToken();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('users');
    }
}

另一个是我的帖子迁移存档

<?php

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

class AddPosts extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nombre');
            $table->longText('contenido');

            $table->unsignedInteger('id_user');

            $table->timestamps();
        });

        Schema::table('posts', function($table) {
          $table->foreign('id_user')->references('id')->on('users');
        });
    }

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

这是我的帖子模型

<?php

namespace NacionGrita;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $table = "posts";
    protected $fillable = ['nombre', 'contenido', 'id_user'];

    public function imagenes()  {
      return $this->belongsToMany('NacionGrita\Imagen');
    }
    public function categorias()  {
      return $this->belongsToMany('NacionGrita\Categoria');
    }
    public function tags() {
      return $this->belongsToMany('NacionGrita\Tag');
    }
    public function user()  {
      return $this->belongsTo('NacionGrita\User');
    }

}

这是我的用户模型

<?php

namespace NacionGrita;

use Illuminate\Foundation\Auth\User as Authenticatable;


class User extends Model
{

    protected $table = "users";
    protected $fillable = [
        'user', 'email', 'password', 'img'
    ];

    public function posts() {
      return $this->hasMany('NacionGrita\Post');
    }

    protected $hidden = [
        'password', 'remember_token',
    ];
}

如果我将“posts”表列从 id_user 更改为 user_id,它可以工作,但我不知道为什么我必须更改列名,如果它应该工作,因为我指定了外键或我做错了什么?

感谢您的帮助

【问题讨论】:

    标签: php mysql sql-server laravel database-migration


    【解决方案1】:

    为了指定外键,您需要在定义关系时在模型中这样做。

    来自belongsTo 关系的文档:

    在上面的示例中,Eloquent 将尝试将 Phone 模型中的 user_id 匹配到 User 模型上的 id。 Eloquent 通过检查关系方法的名称并在方法名称后面加上_id 来确定默认的外键名称。但是,如果 Phone 模型上的外键不是 user_id,您可以将自定义键名称作为第二个参数传递给 belongsTo 方法

    换句话说,在您定义与用户的关系的 Post 模型中,您需要添加第二个参数来指定外键名称:

    public function user()  {
      return $this->belongsTo('NacionGrita\User', 'id_user');
    }
    

    来自hasOnehasMany 关系的文档:

    Eloquent 根据模型名称假设关系的外键。在这种情况下,Phone 模型被自动假定为具有user_id 外键。如果您希望覆盖此约定,您可以将第二个参数传递给 hasOne 方法:

    换句话说,在您定义与 Post 关系的 User 模型中,您需要再次添加第二个参数来指定外键名称:

    public function posts() {
      return $this->hasMany('NacionGrita\Post', 'id_user');
    }
    

    文档链接:https://laravel.com/docs/5.1/eloquent-relationships

    【讨论】:

    • 非常感谢,先生。是的,这解决了我的问题,我不必设置新参数,因为我使用了默认键名。谢谢。
    猜你喜欢
    • 2018-02-21
    • 2015-12-08
    • 2018-11-02
    • 2019-10-02
    • 2017-04-03
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多