违反完整性约束:1048列"taggable_id"不能为空 [英] Integrity constraint violation: 1048 Column 'taggable_id' cannot be null
问题描述
我正在尝试设置 laravel-tagging ,它似乎最受欢迎Laravel的标签系统.但不幸的是,它没有任何前端功能.有一个指南为此,我彻底进行了跟踪.最后,尝试创建标签时遇到错误:
I am trying to set up laravel-tagging, which seems to be the most popular tagging system for Laravel out there. But unfortunately it doesn't come with any front-end features. There is a guide for it that I followed through thoroughly. At the end, I run into error while trying to create a tag:
SQLSTATE [23000]:违反完整性约束:1048列 'taggable_id'不能为null(SQL:插入
tagging_tagged
(tag_name
,tag_slug
,taggable_type
,taggable_id
)值 (奶酪,奶酪,App \链接,))
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'taggable_id' cannot be null (SQL: insert into
tagging_tagged
(tag_name
,tag_slug
,taggable_type
,taggable_id
) values (Cheese, cheese, App\Links, ))
我发现了其他一些帖子,人们遇到了类似的错误,例如 this ,此和
I found several other posts where people ran into similar errors, such as this, this and this. But none of them provide a definitive solution. People, and the common sense, say that the model that contains taggable_id
should be saved so the tag gets stored in the database. My code for the controller looks like this:
public function storeStuff(Request $request)
{
// Create the link first
$link = new Links;
// Now add tags
$link->tag(explode(',', $request->tags));
// Try to save tags?
$link->save();
}
在我的情况下,我尝试使用$link->save();
保存它的尝试无效.我仍然遇到相同的错误,并且包含taggable_id
列的数据库表tagging_tagged
仍然充满空值.有人对如何解决此问题有任何建议吗?
My attempt to save it using $link->save();
in my case doesn't work. I still get the same error and my database table tagging_tagged
, which contains taggable_id
column is still full of nulls. Does anyone have any advice on how to approach this problem?
编辑:我通过添加另一个保存来工作,如Tobias Karlsson建议的那样:
I got it to work by adding another save as suggested by Tobias Karlsson:
$link = new Links;
$link->tag_name = $request->tags;
$link->save();
// Now add tags
$link->tag(explode(',', $request->tags));
$link->save();
我还必须添加时间戳以修复丢失的created_at
错误.时间戳不包含在laravel-tagging软件包随附的初始迁移中,即使我不确定它们是否对标签有用.我的tagging_tagged
表现在看起来像这样:
I also had to add timestamps to fix missing created_at
error. Timestamps are not included in initial migration that comes with laravel-tagging package, even though I am not sure if they are useful for tags. My tagging_tagged
table now looks like this:
FIELD TYPE NULL KEY
id int(10)unsigned NO PRI auto_increment
taggable_id int(10)unsigned NO MUL
taggable_type varchar(255) NO MUL
tag_name varchar(255) NO
tag_slug varchar(255) NO MUL
created_at timestamp YES
updated_at timestamp YES
我仍在尝试使用Javascript自动填充标签.一切正常后,我将更新此问题.
I am still trying to get Javascript to autofill tags. I will update this question once I get it all working.
推荐答案
您将必须先保存模型,然后设置标签,然后再次保存.
You will have to save the model first and then set tags and then save again.
$link = new Links;
$link->someProperty = $request->someProperty;
// Save model to get a taggable_id (model id).
$link->save();
// Now add tags
$link->tag(explode(',', $request->tags));
// Save tags.
$link->save();
这篇关于违反完整性约束:1048列"taggable_id"不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!