2017-02-14 110 views
1

我收到此錯誤消息與關係,但我無法找到我需要做的事情來阻止此錯誤OcotberCMS - 「SQLSTATE [42S22]:未找到列:1054未知列'users.application_id'

我有一個控制器和組件的應用模式。在後臺,我可以添加一個新的應用程序記錄。但把我得到的錯誤。

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.application_id' in 'where clause' (SQL: select * from `users` where `users`.`deleted_at` is null and `users`.`application_id` = 1 and `users`.`application_id` is not null limit 1)" on line 662 of /srv/users/sssandwich/apps/website/public/vendor/laravel/framework/src/Illuminate/Database/Connection.php 

我隔離了錯誤,當我加入這個代碼出現in.yaml Acme \ Models \ Application \ fields.yaml。

# =================================== 
# Form Field Definitions 
# =================================== 

fields:   
    client: 
     label: Client 
     type: relation 
     disabled: true 
     select: concat(name, ' ', surname) 
     span: left 

Acme\Models\Application.php 

我必須設置爲

/** 
    * @var array Relations 
    */ 
    public $hasOne = [ 
    'client' => ['RainLab\User\Models\User', 'table' => 'users'], 
    'cv' => ['Acme\Acme\Models\Cv'] 
    ]; 
    public $hasMany = []; 
    public $belongsTo = [ 
    'owner' => ['Backend\Models\User'] 
    ]; 

下面是數據庫結構的關係...

public function up() 
{ 
    Schema::create('acme_acme_applications', function(Blueprint $table) { 
     $table->engine = 'InnoDB'; 
     $table->increments('id'); 
     $table->string('jobs_id')->nullable()->index(); 
     $table->string('application_id')->nullable()->index(); 
     $table->string('owner_id')->nullable()->index(); 
     $table->string('user_id')->nullable()->index(); 
     $table->string('vacancy_id')->nullable()->index(); 
     $table->string('ref_no', 50)->nullable(); 
     $table->char('status_update')->nullable(); 
     $table->timestamps(); 
    }); 
} 

我已經grep'd 'APPLICATION_ID' 和它只出現在數據庫結構中。 users表是Rainlab User插件,但我不確定它爲什麼試圖在該表中找到application_id。

+0

刪除應用程序中的hasOne客戶關係,它看起來像試圖鑽取太多。或者爲它添加鍵。 – aynber

+0

Thankyou但如果我刪除hasOne客戶端,那麼它將不會顯示提交應用程序的用戶的名稱。但我不知道爲什麼它仍然在尋找application_id。 – ServerSideSkittles

+0

它可能試圖拉取應用程序的客戶端。嘗試將密鑰添加到客戶端(id爲外鍵,user_id爲本地密鑰)。我不認識這種格式,所以我不確定你會如何添加它們。 – aynber

回答

3
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.application_id'... 

我認爲這個錯誤是不言自明假定應用程序模型有一個application_id外鍵,因爲您定義了一個一對一的關係;

如果應用程序,其中一個用戶,然後在用戶表應包括application_id柱:

public $hasOne = [ 
    'client' => [ 
    'RainLab\User\Models\User', 
    'table' => 'users', 
    'key' => 'application_id', // the foreign key in User's table 
    'otherKey' => 'id', // Model record id 
     ], 
    ]; 

,你應該定義在用戶模式的反比關係,用戶belongsTo

public $belongsTo = [ 
    'application' => [ 
    'Acme\Models\Application', 
    'table' => 'acme_acme_applications', 
    'key' => 'application_id', 
    'otherKey' => 'id', 
     ], 
    ]; 

如果您不想直接編輯RainLab用戶的插件(更新期間更改將會丟失),請查看here它如何擴展插件並向數據庫添加多列。

相關問題