Laravel OctorberCMS Rainlab.Builder-错误在“上”;“哈松”;与同一模型的关系

Laravel OctorberCMS Rainlab.Builder-错误在“上”;“哈松”;与同一模型的关系,laravel,orm,octobercms,octobercms-plugins,Laravel,Orm,Octobercms,Octobercms Plugins,我正在开发一个用于管理动物的十月CMS插件(使用rainlab.builder)。 动物有两个领域和关系。每种动物都有一个父亲和一个母亲。但当我试图保护我的动物时,出现以下错误: 事件日志: PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id' cannot be null in dir/website/vendor/laravel/framework/src/ Illuminate/Dat

我正在开发一个用于管理动物的十月CMS插件(使用rainlab.builder)。 动物有两个领域和关系。每种动物都有一个父亲和一个母亲。但当我试图保护我的动物时,出现以下错误:

事件日志:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048
Column 'id' cannot be null in dir/website/vendor/laravel/framework/src/
Illuminate/Database/Connection.php:413
插件动物形态:

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
只有当我使用关系(孩子->父亲,孩子->母亲)时才会出现错误


代码 我已经实现了以下hasOne-与我的动物模型的关系

/* Relation */

public $hasOne = [
    'father' => [
        'Namespace\Animals\Models\Animal',
        'key' => 'id',
        'otherKey' => 'id'
    ],
    'mother' => [
        'Namespace\Animals\Models\Animal',
        'key' => 'id',
        'otherKey' => 'id'
    ]
];
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
这是我的字段,来自字段。yaml

/* Relation */

public $hasOne = [
    'father' => [
        'Namespace\Animals\Models\Animal',
        'key' => 'id',
        'otherKey' => 'id'
    ],
    'mother' => [
        'Namespace\Animals\Models\Animal',
        'key' => 'id',
        'otherKey' => 'id'
    ]
];
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
如果有人能解决这种关系,我会非常高兴。
干杯

不要使用
id
作为关系的主键,因为这不是一种好的做法

实际上,这也造成了你所看到的问题。当前,您的模型将
id
用于主id、母亲id和父亲id。您不能这样做

母亲id
父亲id
添加到动物模型中,并将关系定义更改为:

public $hasOne = [
    'father' => [
        'Namespace\Animals\Models\Animal',
        'key' => 'father_id',
        'otherKey' => 'id'
    ],
    'mother' => [
        'Namespace\Animals\Models\Animal',
        'key' => 'mother_id',
        'otherKey' => 'id'
    ]
];

注:在您的情况下,您不必将
key
otherKey
定义为
otherKey
的默认值为“id”,而
otherKey
值是从带有“\u id”后缀的关系名称创建的。

不要将
id
用作关系的主键,因为这不是一个好做法

实际上,这也造成了你所看到的问题。当前,您的模型将
id
用于主id、母亲id和父亲id。您不能这样做

母亲id
父亲id
添加到动物模型中,并将关系定义更改为:

public $hasOne = [
    'father' => [
        'Namespace\Animals\Models\Animal',
        'key' => 'father_id',
        'otherKey' => 'id'
    ],
    'mother' => [
        'Namespace\Animals\Models\Animal',
        'key' => 'mother_id',
        'otherKey' => 'id'
    ]
];

注:在您的情况下,您不必将
key
otherKey
定义为
otherKey
的默认值为“id”,而
otherKey
值是从带有“\u id”后缀的关系名称创建的。

早上好,dragontree。谢谢你的快速回答。等我回到家里,我会测试一下!谢谢你的回答!我忘了在我的孩子模型中添加两个列(父亲id和母亲id)。有了新的数据库键和对关系的更改,一切都能正常工作。我认为这类似于文件附件(没有额外的列)…并且:我将hasOne关系更改为“belongsTo”关系。public$belongsTo=['father'=>['Namespace\Animals\Models\Animal','key'=>'father\u id'],'mother'=>['Namespace\Animals\Models\Animal','key'=>'mother\u id'];早上好,龙树。谢谢你的快速回答。等我回到家里,我会测试一下!谢谢你的回答!我忘了在我的孩子模型中添加两个列(父亲id和母亲id)。有了新的数据库键和对关系的更改,一切都能正常工作。我认为这类似于文件附件(没有额外的列)…并且:我将hasOne关系更改为“belongsTo”关系。public$belongsTo=['father'=>['Namespace\Animals\Models\Animal','key'=>'father\u id'],'mother'=>['Namespace\Animals\Models\Animal','key'=>'mother\u id'];