OctoberCMS Rainlab.Builder- Error On "hasOne" Relation To Same Model
I'm developing an October CMS Plugin for managing animals (with rainlab.builder). Animals have a couple of fields and relations. Every animal have a father and a mother animal. But when I try to safe my animal the following error appears:
PDOException: SQLSTATE: Integrity constraint violation: 1048 Column 'id' cannot be null in dir/website/vendor/laravel/framework/src/ Illuminate/Database/Connection.php:413
Plugin animal form:
SQLSTATE: Integrity constraint violation: 1048 Column 'id' cannot be null (SQL: update `prefix_animals_animal` set `id` = where `prefix_animals_animal`.`id` = 1 and `prefix_animals_animal`.`id` is not null)" on line 666 of dir/website/vendor/laravel/framework/ src/Illuminate/Database/Connection.php
The error only appers when I use the relations (child -> father, child -> mother).
I have implemented the following hasOne - relations to my animal model:
/* Relation */ public $hasOne = [ 'father' => [ 'Namespace\Animals\Models\Animal', 'key' => 'id', 'otherKey' => 'id' ], 'mother' => [ 'Namespace\Animals\Models\Animal', 'key' => 'id', 'otherKey' => 'id' ] ];
This are my fields from field.yaml:
father: label: Father oc.commentPosition: '' nameFrom: name descriptionFrom: description emptyOption: 'No father' span: left type: relation mother: label: Mother span: right oc.commentPosition: '' nameFrom: name descriptionFrom: description emptyOption: 'No mother' type: relation
I would be very happy if someone have a solution for these kind of relations. Cheerio!
id as the primary key for the relation as this is not good practice.
Actually this creates the problem you are seeing also. Currently your model is using the
id for primary Id, mother Id and father Id. You can't do that.
father_id to the Animal model and change the relation definition to this:
public $hasOne = [ 'father' => [ 'Namespace\Animals\Models\Animal', 'key' => 'father_id', 'otherKey' => 'id' ], 'mother' => [ 'Namespace\Animals\Models\Animal', 'key' => 'mother_id', 'otherKey' => 'id' ] ];
PS. In your case you dont have to define
otherKey as the default value for
otherKey is "id" and the
otherKey value is created from the relation name with the "_id" suffix.
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms