Laravel - SQLSTATE [42S22]:未找到列:1054 未知列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34445973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - SQLSTATE[42S22]: Column not found: 1054 Unknown column
提问by CanKer DiAlike
Hello im getting this error Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'posts.user_id' in 'where clause' (SQL: select * from
postswhere
posts.
user_id= 1 and
posts.
user_idis not null)'
and I don't know why if in my database I don't have user_id, I have id_user...
您好,我收到此错误 Illuminate\Database\QueryException 消息'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'posts.user_id' in 'where clause' (SQL: select * from
帖子where
帖子.
user_id= 1 and
帖子.
user_id is not null)'
,我不知道为什么如果在我的数据库中我没有 user_id,我有 id_user...
This is my migration table
这是我的迁移表
<?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');
}
}
This other is my posts migration archive
另一个是我的帖子迁移存档
<?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');
}
}
this is my Post model
这是我的 Post 模型
<?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');
}
}
and this is my users Model
这是我的用户模型
<?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',
];
}
If I change my "posts" table column from id_user to user_id it works but I don't know why I have to change the column name if its supposed to works because I specified the foreigns keys or Im doing something wrong?
如果我将我的“posts”表列从 id_user 更改为 user_id 它可以工作,但我不知道为什么我必须更改列名,如果它应该可以工作,因为我指定了外键或者我做错了什么?
Thanks for help
感谢帮助
回答by Thomas Kim
In order to specify the foreign keys, you need to do so in the model when you define the relationships.
为了指定外键,您需要在定义关系时在模型中执行此操作。
From the docs for a belongsTo
relationship:
来自belongsTo
关系的文档:
In the example above, Eloquent will try to match the
user_id
from thePhone
model to anid
on theUser
model. Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with_id
. However, if the foreign key on thePhone
model is notuser_id
, you may pass a custom key name as the second argument to thebelongsTo
method
在上面的示例中,Eloquent 将尝试
user_id
将Phone
模型中的id
与User
模型上的相匹配。Eloquent 通过检查关系方法的名称并为方法名称添加后缀来确定默认外键名称_id
。但是,如果Phone
模型上的外键不是user_id
,您可以将自定义键名称作为第二个参数传递给belongsTo
方法
In other words, in your Post model where you define the relationship with the User, you need to add a second argument that specifies the foreign key name:
换句话说,在您定义与用户关系的 Post 模型中,您需要添加第二个参数来指定外键名称:
public function user() {
return $this->belongsTo('NacionGrita\User', 'id_user');
}
From the docs for a hasOne
and hasMany
relationship:
来自 ahasOne
和hasMany
关系的文档:
Eloquent assumes the foreign key of the relationship based on the model name. In this case, the
Phone
model is automatically assumed to have auser_id
foreign key. If you wish to override this convention, you may pass a second argument to thehasOne
method:
Eloquent 根据模型名称假定关系的外键。在这种情况下,
Phone
模型被自动假定为具有user_id
外键。如果您希望覆盖此约定,您可以向该hasOne
方法传递第二个参数:
In other words, in your User model where you define the relationship with the Post, you need to once again add a second argument that specifies the foreign key name:
换句话说,在您定义与 Post 关系的 User 模型中,您需要再次添加第二个参数来指定外键名称:
public function posts() {
return $this->hasMany('NacionGrita\Post', 'id_user');
}
Link to docs: https://laravel.com/docs/5.1/eloquent-relationships