OctoberCMS -“SQLSTATE [42S22]:未找到列:1054 未知列 'users.application_id'
OcotberCMS - "SQLSTATE[42S22]: Column not found: 1054 Unknown column '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
我隔离了在 fields.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 了 'application_id',它只出现在数据库结构中。 users table 是 Rainlab User 插件,但我不确定为什么它试图在 table.
中找到 application_id
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.application_id'...
我认为这个错误是 self-explanatory 假定应用程序模型具有 application_id
外键,因为您定义了 One-To-One 关系;
如果应用程序 Has-One 用户,则用户的 table 应包含一个 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 User 的插件(更改将在更新期间丢失),请查看here它如何扩展插件并向数据库添加多列。
我收到有关关系的错误消息,但我无法找到我需要做什么来阻止此错误。
我有一个带有控制器和组件的应用程序模型。在后端我可以添加一个新的应用程序记录。但是查看我得到了错误。
"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
我隔离了在 fields.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 了 'application_id',它只出现在数据库结构中。 users table 是 Rainlab User 插件,但我不确定为什么它试图在 table.
中找到 application_id "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.application_id'...
我认为这个错误是 self-explanatory 假定应用程序模型具有 application_id
外键,因为您定义了 One-To-One 关系;
如果应用程序 Has-One 用户,则用户的 table 应包含一个 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 User 的插件(更改将在更新期间丢失),请查看here它如何扩展插件并向数据库添加多列。