I am getting this error message with relations but I can't find out what I need to do to stop this error.
I have an Application model with a controller and component. In the backend I can add a new Application record. But to view I get the error.
"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
I isolated the error to appear when I add this code in fields.yaml of Acme\\Models\\Application\\fields.yaml.
# ===================================
# Form Field Definitions
# ===================================
fields:
client:
label: Client
type: relation
disabled: true
select: concat(name, ' ', surname)
span: left
In
Acme\Models\Application.php
I have the relations set up as
/**
* @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']
];
Below is the database structure...
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();
});
}
I have grep'd 'application_id' and it only appears in the db structure. The users table is the Rainlab User plugin, but I am not sure why it trying to find the application_id in that table.
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.application_id'...
I think this error is self-explanatory the Application Model is assumed to have a application_id
foreign key because you defined a One-To-One Relation ;
If the Application Has-One User then the User's table should include an application_id
column :
public $hasOne = [
'client' => [
'RainLab\User\Models\User',
'table' => 'users',
'key' => 'application_id', // the foreign key in User's table
'otherKey' => 'id', // Model record id
],
];
and you should define the inverse relation in the User's Model, a User belongsTo
public $belongsTo = [
'application' => [
'Acme\Models\Application',
'table' => 'acme_acme_applications',
'key' => 'application_id',
'otherKey' => 'id',
],
];
If you don't want to directly edit the RainLab User's plugin ( changes will be lost during updates ), have a look here how it extends the plugin and adds multiple columns to the database .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.