【发布时间】: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