简体   繁体   English

SQLSTATE[HY000]:一般错误:1364 字段“parent_id”没有默认值

[英]SQLSTATE[HY000]: General error: 1364 Field 'parent_id' doesn't have a default value

I'm building my own forum as practice, I set up Topic and Comment models with polymorphic relationship, a comment can belong to a topic or another comment , this is because I want to have nested comments.我正在建立自己的论坛作为练习,我设置了具有多态关系的TopicComment模型, comment可以属于一个topic或另一个comment ,这是因为我想要嵌套评论。 Another issue is when I reply to the topic I get the following error:另一个问题是当我回复该主题时,我收到以下错误:

SQLSTATE[HY000]: General error: 1364 Field 'parent_id' doesn't have a default value (SQL: insert into comments ( user_id , body , commentable_id , commentable_type , updated_at , created_at ) values (1 <\p>ljlnlnlnlnlnllnlnlnlnlnln</p>, 1, App\Models\Topic, 2019-11-08 20:41:43, 2019-11-08 20:41:43)) SQLSTATE[HY000]:一般错误:1364 字段“parent_id”没有默认值(SQL:插入commentsuser_idbodycommentable_idcommentable_typeupdated_atcreated_at )值(1 <\p>ljlnlnlnlnlnllnlnlnlnlnln</p >, 1, App\Models\Topic, 2019-11-08 20:41:43, 2019-11-08 20:41:43))

My models and Migrations.我的模型和迁移。

Topic model:主题 model:

class Topic extends Model
{
    protected $table = 'topics';

    public function author()
    {
        return $this->belongsTo('App\Models\User', 'user_id');
    }

    public function comments()
    {
        return $this->morphMany('App\Models\Comment', 'commentable')->whereNull('parent_id');
    }
}
public function up()
{
    Schema::create('topics', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('user_id')->index();
        $table->foreign('user_id')->references('id')->on('users');
        $table->string('title');
        $table->text('body');
        $table->string('url')->unique();
        $table->string('slug')->unique();
        $table->boolean('isVisible')->default(false);
        $table->timestamps();
    });
}

Comment model:评论 model:

class Comment extends Model
{
    protected $table = 'comments';

    public function author()
    {
        return $this->belongsTo('App\Models\User', 'user_id');
    }

    public function replies()
    {
        return $this->hasMany('App\Models\Comment', 'parent_id');
    }
}
public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('user_id')->unsigned();
        $table->integer('parent_id')->unsigned();
        $table->integer('commentable_id')->unsigned();
        $table->string('commentable_type');
        $table->text('body');
        $table->timestamps();
    });
}

The error is pretty obvious;错误很明显; the column parent_id doesn't have a value (you're not setting one, and there is no default). parent_id列没有值(您没有设置值,也没有默认值)。 Either set the column as nullable , or pass a parent_id将列设置为nullable ,或传递parent_id

In migration :migration中:

$table->integer('parent_id')->unsigned()->nullable();

Since a comment on a topic has no parent comment (not a reply) then the parent_id would be null, however your database schema doesn't allow it, make the column nullable由于对主题的评论没有父评论(不是回复),因此parent_id将是 null,但是您的数据库架构不允许它,使列可为空

$table->integer('parent_id')->unsigned()->nullable();

or give it null as the default value或者给它null作为默认值

$table->unsignedInteger('parent_id')->default(null);

Hope this helps希望这可以帮助

i had this problem and solved by adding my column into fillable property我遇到了这个问题并通过将我的列添加到可填充属性中来解决

Add parent_id to the fillable array in your model Comment , to allow saving through create and massive methods将 parent_id 添加到 model注释中的可填充数组中,以允许通过 create 和 mass 方法保存

protected $fillable = ['parent_id'];

i am trying to insert data into database using laravel which gives me this error Illuminate \ Database \ QueryException (HY000) SQLSTATE[HY000]: General error: 1364 Field 'body' doesn't have a default value (SQL: insert into categories ( id , title , updated_at , created_at ) values (1, khan, 2019-11-09 13:14:21, 2019-11-09 13:14:21)).我正在尝试使用 laravel 将数据插入数据库,这给了我这个错误 Illuminate \ Database \ QueryException (HY000) SQLSTATE[HY000]:一般错误:1364 字段“正文”没有默认值(SQL:插入categoriesid , title , updated_at , created_at ) 值 (1, khan, 2019-11-09 13:14:21, 2019-11-09 13:14:21))。 plz instruct me what to do请指导我该怎么做

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE [HY000]:一般错误:1364 字段“agent_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'agent_id' doesn't have a default value Laravel Voyager SQLSTATE [HY000]:常规错误:1364字段“ id”没有默认值 - Laravel Voyager SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value SQLSTATE[HY000]:一般错误:1364 字段“client_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'client_id' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段&#39;author_id&#39;没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'author_id' doesn't have a default value Laravel SQLSTATE [HY000]:常规错误:1364字段“ id”没有默认值 - Laravel SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“ reservation_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'reservation_id' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“ mov_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'mov_id' doesn't have a default value SQLSTATE[HY000]:一般错误:1364 字段“country_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'country_id' doesn't have a default value SQLSTATE [HY000]:一般错误:1364 字段“trans_id”没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'trans_id' doesn't have a default value SQLSTATE [HY000]:常规错误:1364字段“照片”在laravel 5.5中没有默认值 - SQLSTATE[HY000]: General error: 1364 Field 'photo' doesn't have a default value in laravel 5.5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM