【问题标题】:Migration in LaravelLaravel 中的迁移
【发布时间】:2019-05-17 16:44:07
【问题描述】:

我在迁移表时遇到错误。 错误说

SQLSTATE[42000]:语法错误或访问冲突:1064 你有一个 SQL 语法错误;检查与您对应的手册 MariaDB 服务器版本,用于在 'json null 附近使用正确的语法, start_date 日期不为空,end_date 日期不为空,status 枚举('' 在第 1 行 (SQL: 创建表 modules (id int unsigned not null auto_increment 主键,title varchar(191) 不为空, description text not null, image blob null, resources json null, start_date 日期不为空,end_date 日期不为空,status enum('pending', 'start', 'completed') not null 默认 'pending', user_id int 不为空,created_at 时间戳为空,updated_at timestamp null) 默认字符集 utf8mb4 collat​​e 'utf8mb4_unicode_ci')

我的表有以下值:

public function up()
    {
        Schema::create('modules', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('description');
            $table->binary('image')->nullable();
            $table->json('resources')->nullable();
            $table->date('start_date');
            $table->date('end_date');
           $table->ENUM('status',['pending','start','completed'])->default('pending');
            $table->integer('user_id');
            $table->timestamps();
        });
    }

这是我的模型代码:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

    class modules extends Model
    {
        protected $fillable = [
            'title', 'description','image','resources', 'start_date','end_date','status','user_id',
        ];
    }

谁能解决这个问题??

【问题讨论】:

  • 发布您的型号代码
  • 我已经发过了,现在你能帮我吗
  • 检查您的 MariaDB 版本。从 MariaDB 文档中,“在 MariaDB 10.2.7 中添加了 JSON 别名。这样做是为了可以在从 MySQL 到 MariaDB 的基于语句的复制中使用 JSON 列,并使 MariaDB 可以从 MySQL 读取 mysqldumps。” mariadb.com/kb/en/library/json-data-type
  • 您使用的是什么版本的 MariaDB?
  • 还将您的 Laravel 版本添加到您的问题中。

标签: mysql laravel


【解决方案1】:

在 Laravel 中,$table-&gt;json() 方法将尝试在数据库中创建一个实际的 JSON 字段。但是,直到 MySQL 5.7.8 才将 JSON 字段添加到 MySQL。

因此,如果您使用的是5.7.8 之前的 MySQL 版本,您只需将其创建为文本字段

MariaDB 新版本支持 JSON。 (Alpha 版本。Maria 不建议将其用于生产服务器。仅用于测试。)

MariaDB 10.1 不支持 JSON

【讨论】:

    【解决方案2】:

    maria db早期版本不支持json字段类型,见链接,https://mariadb.com/resources/blog/json-with-mariadb-10-2/

    请验证您的 mariadb 版本。

    【讨论】:

      【解决方案3】:

      添加$casts 类属性可能会解决您的问题。

      class modules extends Model
      {
          protected $casts = [
              'resources' => 'array',
          ];
      }
      

      Here 可能会对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2019-12-27
        • 2021-02-28
        • 2018-04-18
        • 2018-01-10
        • 1970-01-01
        • 2020-07-17
        • 2012-11-12
        相关资源
        最近更新 更多