Laravel 中的 mariaDB JSON 支持

2022-01-15 00:00:00 json mariadb php laravel laravel-5.4

我正在尝试在 XAMP 中创建一个 json 数据库,在使用 phpmyAdmin 时它显示我正在使用 mariaDB,但在我的 xamp-control panel v3.2.2 中它显示正在运行 mySQL 在端口 3306 上.我正在使用 Laravel 5.4 框架来创建数据库,以下是我正在尝试执行的迁移:

I'm trying to create a json database in XAMP, while using the phpmyAdmin it showed me that I'm using mariaDB but in my xamp-control panel v3.2.2 it shows running mySQL on port 3306. I'm using Laravel 5.4 framework to create the database, following is my migration which I'm trying to execute:

Schema::connection('newPortal')->create('pages', function (Blueprint $table){
    $table->increments('id');
    $table->string('title');
    $table->string('slug')->unique()->index();
    $table->json('styles')->nullable();
    $table->json('content')->nullable();
    $table->json('scripts')->nullable();
    $table->softDeletes();
    $table->timestamps();
});

现在执行此操作时出现以下错误:

Now while executing this I'm getting following error:

SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;查看与您的 MariaDB 服务器版本相对应的手册,了解在 'json null, content json null, scripts json null, deleted_at 第 1 行的时间戳 null'(SQL:创建表 pages (id int unsigned not null auto_increment 主键,title varchar(191) notnull, slug varchar(191) not null, styles json null, content json null, scripts json null,deleted_at timestamp null, created_at timestamp null, updated_at timestamp null) 默认字符集 utf8mb4 collat​​e utf8mb4_unicode_ci)

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'json null, content json null, scripts json null, deleted_at timestamp null' at line 1 (SQL: create table pages (id int unsigned not null auto_increment primary key, title varchar(191) not null, slug varchar(191) not null, styles json null, content json null, scripts json null, deleted_at timestamp null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

即使我保持不为空,它也会抛出相同的错误.我想要 json 格式的数据,我检查了支持的版本,并根据文档 json 格式支持从版本 MariaDB 10.0.16. 开始,我正在使用 10.1.21-MariaDB

Even if I keep not null it throws the same error. I want to have json formatted data, I checked the supported version and as per the documentation json format support started from the version MariaDB 10.0.16. and I'm using 10.1.21-MariaDB

帮我解决这个问题.

推荐答案

MariaDB 10.2.7 版本开始;JSON 数据类型是 LONGTEXT 的别名.

Since MariaDB version 10.2.7; theJSON data-type is an alias for LONGTEXT.

如果您对 MariaDB 中的 JSON 数据类型有疑问,只需将其更改为 LONGTEXT.;-)

If you are having issues with the JSON data-type in MariaDB, simply just change it to LONGTEXT. ;-)

或者使用 这个包

相关文章