php - 数据库某个字段没有默认值
伊谢尔伦
伊谢尔伦 2017-04-10 17:54:02
0
4
560

mysql error.

SQLSTATE[HY000]: General error: 1364 Field 'uuid' doesn't have a default value (SQL: insert into `comments` (`markdown`, `updated_at`, `created_at`) values (Hello, 2016-11-24 14:51:13, 2016-11-24 14:51:13))

CommentController

/**
     * Create comment.
     *
     * @param Request $request
     *
     * @return json
     */
    public function commentCreate(CommentCreateRequest $request)
    {
        try {
            $currentUser = JWTAuth::parseToken()->authenticate();
            $input = $request->input();

            $html = Parsedown::instance()->text($input['markdown']);
            $uuid = UUID::uuid1()->toString();
            $status = 'published';

            $input = array_add($input, 'uuid', $uuid);
            $input = array_add($input, 'html', $html);
            $input = array_add($input, 'status', $status);
            $input = array_add($input, 'user_id', $currentUser->id);
            $input = array_add($input, 'created_at', Carbon::now());

            // return $input;
            $comment = $this->comment->create($input);

            return response()->json([
                'code' => 0,
                'data' => $comment,
            ]);
        } catch (ValidationException $e) {
            return response()->json([
                'code' => ErrorCode::FAILED_TO_CREATE_THE_COMMENT,
                'message' => ErrorMessage::FAILED_TO_CREATE_THE_COMMENT,
            ]);
        } catch (QueryException $e) {
            return response()->json([
                'code' => ErrorCode::FAILED_TO_CREATE_THE_COMMENT,
                // 'message' => ErrorMessage::FAILED_TO_CREATE_THE_COMMENT,
                'message' => $e->getMessage(),
            ]);
        }

migration

/**
     * Run the migrations.
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->uuid('uuid');
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('post_id')->unsigned();
            $table->text('markdown');
            $table->text('html');
            $table->string('status');
            $table->timestamps();
            $table->softDeletes();

            $table->engine = 'InnoDB';
        });
    }

补充:

主要是其他表也有 uuid 这个表,比如 Post 表,就没有问题。

我把 uuid 字段删除后,又报下面的错误:

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `comments` (`markdown`, `updated_at`, `created_at`) values (Hello, 2016-11-24 15:29:11, 2016-11-24 15:29:11))
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(4)
PHPzhong

数据库字段给个默认值吧

迷茫
->default($value) 为此字段指定「默认」值

->nullable()    此字段允许写入 NULL 值
Ty80

把表的结构发出来看一下。0 0
表名:table_name

desc table_name

Peter_Zhu

以后mysql版本限制会越来越严格,最好还是加上默认值吧

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template