Laravel 中的 mariaDB JSON 支持

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42425667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:24:30  来源:igfitidea点击:

mariaDB JSON support in Laravel

phpjsonlaravelmariadblaravel-5.4

提问by Nitish Kumar

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.2it 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:

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

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]: 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, contentjson null, scriptsjson null, deleted_attimestamp null' at line 1 (SQL: create table pages(idint unsigned not null auto_increment primary key, titlevarchar(191) not null, slugvarchar(191) not null, stylesjson null, contentjson null, scriptsjson null, deleted_attimestamp null, created_attimestamp null, updated_attimestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

SQLSTATE[42000]:语法错误或访问冲突:1064 你的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以了解在第 1 行(SQL:create table ( int unsigned not null auto_increment primary key, varchar(191))附近的“json null、contentjson null、scriptsjson null、deleted_attimestamp null”附近使用的正确语法) not null, varchar(191) not null, json null, json null, json null, timestamp null, timestamp null, timestamp null) 默认字符集 utf8mb4 collat​​e utf8mb4_unicode_ci)pagesidtitleslugstylescontentscriptsdeleted_atcreated_atupdated_at

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

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

Help me out in this.

帮我解决这个问题。

回答by ybr-nx

MariaDB has alias for JSON datatype since version 10.2.7

MariaDB 自 10.2.7 版起具有 JSON 数据类型的别名

Add MariaDB JSON to Laravel with this package

使用这个包将 MariaDB JSON 添加到 Laravel

回答by Rick James

Note that the 1064 complained about the datatype "json". Such is not (yet) implemented in MariaDB.

请注意,1064 抱怨数据类型“json”。这在 MariaDB 中(尚未)实现。

You can get close with Dynamic Columns, which at least has a way of fetching them into JSON syntax.

您可以接近Dynamic Columns,它至少有一种方法可以将它们提取到 JSON 语法中。

Another thing (probably what you are referring to) is CONNECTbeing able to have a JSON table type. (Not columntype.)

另一件事(可能是您所指的)是CONNECT能够拥有 JSON表类型。(不是类型。)

MySQL 5.7 has a datatype called JSON, plus a bunch of functions to manipulate such.

MySQL 5.7 有一个名为 的数据类型JSON,加上一堆函数来操作它。

回答by Techifylogic

Figured out a simple workaround (not recommended for production) -

想出了一个简单的解决方法(不推荐用于生产)-

As per mariadb version 10.1.32 and lower it seems like mariadb does not support json data type I am still unsure if it is available in version 10.2.7+.

根据 mariadb 10.1.32 及更低版本,似乎 mariadb 不支持 json 数据类型,我仍然不确定它是否在 10.2.7+ 版本中可用。

but here's a simple workaround to get through this.

但这里有一个简单的解决方法来解决这个问题。

change json data type into text and then run your migration again.

将 json 数据类型更改为文本,然后再次运行迁移。

(https://user-images.githubusercontent.com/27993070/41234555-19c5d1d8-6dbf-11e8-9a4b-0644b03aecfc.png)

( https://user-images.githubusercontent.com/27993070/41234555-19c5d1d8-6dbf-11e8-9a4b-0644b03aecfc.png)

source- https://github.com/laravel/framework/issues/13622

来源- https://github.com/laravel/framework/issues/13622