Eloquent ORM错误:未找到列:1054

时间:2014-03-27 01:37:07

标签: mysql laravel laravel-4 eloquent

我有关于hasOne关系的问题,但我不明白为什么。 一篇文章只是一个供应商。所以在Article模型中放置了一个指向主键供应商的外键。

class Article extends Eloquent {

    public $timestamps = true;

    protected $softDelete = true;

    // Accesibles to edit
    //protected $fillable = array( 'code', 'name', 'size', 'price', 'redial', 'supplier_id', 'count', 'description', 'colour' );
    protected $fillable = array('*');

    // prevent edit
    protected $guarded = array( 'updated_at', 'created_at', 'id' );


    public function supplier() {
        return $this->hasOne('Supplier');
    }

}

class Supplier extends Eloquent {

    public $timestamps = true;

    protected $softDelete = true;

    protected $fillable = array('*');

    protected $guarded = array('id', 'created_at', 'updated_at');


    public function article() {
        return $this->belongsTo('Article');
    }

}

迁移:

Schema::create('articles', function($table)
        {
            // DB Engine
            $table->engine = 'InnoDB';

            // Columns
            $table->increments('id');
            $table->string('code')->unique();
            $table->string('name', 100);
            $table->text('description')->nullable();
            $table->string('colour')->nullable();
            $table->integer('count')->unsigned();
            $table->float('price')->unsigned();
            $table->float('redial')->default(100)->unsigned();

            $table->integer('supplier_id')->unsigned();
            $table->foreign('supplier_id')->references('id')->on('suppliers');

            // Additionals columns
            $table->softDeletes(); // Never removed, only sets a date of deletion in delete_at column
            $table->timestamps();    // Set created_at updated_at columns automatically

            // Index and others atributtes
            $table->index('code');
        });

Schema::create('suppliers', function($table)
        {
            // DB Engine
            $table->engine = 'InnoDB';

            // Columns
            $table->increments('id');
            $table->string('full_name', 50);
            $table->string('company', 50);

            // Additionals columns
            $table->softDeletes(); // Never removed, only sets a date of deletion in delete_at column
            $table->timestamps();    // Set created_at updated_at columns automatically

        });

文章管理员:

public function create()
    {
        $article = new Article;
        $article->code = Input::get('code');
        $article->name = Input::get('name');
        $article->description = Input::get('description');
        $article->size = Input::get('size');
        $article->colour = Input::get('colour');
        $article->count = Input::get('count');
        $article->price = Input::get('price');
        $article->redial = Input::get('redial');

        $supplier = Supplier::find(Input::get('supplier_id'));

        $article->supplier->associate($supplier);

        $article->save();

        return View::make('articles.show')->with( array('article' => $article) );
    }

输出:

Illuminate \ Database \ QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'suppliers.supplier_id' in 'where clause' (SQL: select * from `suppliers` where `suppliers`.`deleted_at` is null and `suppliers`.`supplier_id` is null limit 1)

2 个答案:

答案 0 :(得分:1)

您错过了foreign key表格中的suppliers,因此请在suppliers

的迁移中添加此内容
$table->integer('article_id');

由于您在hasOne('Supplier')模型中使用了Article,因此Laravel会使用suppliersarticle_id表中查找与a id相关的相关模型使用articles在父表(文章)中建模。换句话说,suppliers表中的记录将使用以下内容与articles建立关系:

id | ... 1 | ... 2 | ... (父):

suplierss

id | article_id | ... 1 | 1 | ... //<-- article_id in this table is id in articles table 2 | 2 | ... //<-- article_id in this table is id in articles table (孩子):

articles

primary Key/id表中foreign key/article_id引用suppliers表中的suppliers

另外,您使用articlesarticle_id表格中引用了suppliers,因此您应该在public function article() { return $this->hasMany('Article'); } 表格中使用

belongsTo

然后在对立模型(文章)中使用反向关系(// In Article Model public function supplier() { return $this->belongsTo('Supplier'); } ):

{{1}}

答案 1 :(得分:0)

您的where子句正在寻找suppliers.supplier_id - 但在您的CREATE架构中,未指定此内容。

Schema::create('suppliers', function($table)
        {
            // DB Engine
            $table->engine = 'InnoDB';

            // Columns
            $table->increments('id');
            $table->integer('supplier_id');  // consider adding an integer for supplier id.
            $table->string('full_name', 50);
            $table->string('company', 50);

            // Additionals columns
            $table->softDeletes(); // Never removed, only sets a date of deletion in delete_at column
            $table->timestamps();    // Set created_at updated_at columns automatically

        });