0

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:

Event Log:

PDOException: SQLSTATE[23000]: 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[23000]: 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).


Code

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!

Lukas Rotermund
  • 419
  • 4
  • 14

1 Answers1

2

Don't use 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.

Add mother_id and 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 key and otherKey as the default value for otherKey is "id" and the otherKey value is created from the relation name with the "_id" suffix.

dragontree
  • 1,709
  • 11
  • 14
  • Good morning dragontree. Thank your for your fast answer. I'll test this out when I'm back at home! – Lukas Rotermund Jul 31 '17 at 06:25
  • Thank your for your great answer! I forgot to add the two colums (father_id and mother_id) to my child model. With the new database keys and changes to the relation everything works. I thought this works similar to the file attachments (without an extra column)... – Lukas Rotermund Aug 01 '17 at 20:26
  • AND: I changed the hasOne relation to "belongsTo" ralation. public $belongsTo = [ 'father' => [ 'Namespace\Animals\Models\Animal', 'key' => 'father_id' ], 'mother' => [ 'Namespace\Animals\Models\Animal', 'key' => 'mother_id' ] ]; – Lukas Rotermund Aug 01 '17 at 20:46