我正在尝试在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();
});
现在执行此操作时,我遇到以下错误:
SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查与您的MariaDB服务器版本对应的手册,以便在第1行使用'json null,
content
json null,scripts
json null,deleted_at
timestamp null'附近使用正确的语法(SQL :create tablepages
(id
int unsigned not null auto_increment primary key,title
varchar(191)not null,slug
varchar(191)not null,{{1} } json null,styles
json null,content
json null,scripts
timestamp null,deleted_at
timestamp null,created_at
timestamp null)默认字符集utf8mb4 collate utf8mb4_unicode_ci )
即使我保持不为null,它也会抛出相同的错误。我想要json格式的数据,我检查了支持的版本,并根据文档json格式支持从版本updated_at
开始,我正在使用MariaDB 10.0.16.
帮助我解决这个问题。
答案 0 :(得分:6)
MariaDB自版本10.2.7起具有JSON数据类型的别名
将MariaDB JSON添加到Laravel答案 1 :(得分:4)
请注意,1064抱怨数据类型为“json”。这些(尚未)在MariaDB中实现。
你可以接近Dynamic Columns,至少有一种方法可以将它们提取到JSON语法中。
另一件事(可能是你所指的)是CONNECT
能够拥有JSON 表类型。 (不是列类型。)
MySQL 5.7有一个名为JSON
的数据类型,加上一系列操作它们的函数。
答案 2 :(得分:1)
通过使用 Composer 运行此命令为 Laravel 添加 MariaDB JSON 支持:
composer require ybr-nx/laravel-mariadb
如果您使用的是 Larvel 5.3 和 5.4,请执行以下两项:
config/app.php
中包含 MariaDBServiceProvider
线路供应商:'providers' => [
// other exist providers
YbrNX\MariaDB\MariaDBServiceProvider::class,
]
'defaultconnection' => [
'driver' => 'mariadb',
添加包完成,然后您可以使用功能。
在迁移中:
$table->json('field') //CHECK (JSON_VALID(field))
$table->json('field')->nullable() //CHECK (field IS NULL OR JSON_VALID(field))
对于查询构建器:
$query->where('somejson->something->somethingelse', 2)
DB::table('sometable')->select('sometable.somedata', 'sometable.somejson->somedata as somejsondata')
此外,JSON_SET() 在 MariaDB 中的工作方式与在 MySQL 5.7 中一样:
DB::table('sometable')->where('somejson->somedata', $id)->update(['somejson->otherdata' => 'newvalue']);
<块引用>
注意 1:MariaDB 从 10.2.7 版本开始有一个 JSON 数据类型的别名
注 2:MariaDB < 10.2.8 JSON_EXTRACT() 行为中存在错误 功能。它已在 MariaDB 10.2.8 中修复
答案 3 :(得分:0)
找到了一个简单的解决方法(不建议用于生产环境)-
按照mariadb 10.1.32及更低版本,似乎mariadb不支持json数据类型,我仍然不确定10.2.7+版本中是否可用。
但是这里有一个简单的解决方法。
将json数据类型更改为文本,然后再次运行迁移。
(https://user-images.githubusercontent.com/27993070/41234555-19c5d1d8-6dbf-11e8-9a4b-0644b03aecfc.png)