2017-02-03 61 views
2

运行此代码:使用JSON列雄辩模型 'UNION ALL SELECT' SQL语法错误

App\ExampleJson::create(["name" => "example"]); 

在此模式:

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class ExampleJson extends Model 
{ 
    public $timestamps = false; 

    protected $fillable = [ 
     "name", 
     "jsontest", 
    ]; 

    protected $casts = [ 
     "jsontest" => "array", 
    ]; 

    protected $attributes = [ 
     "jsontest" => [], 
    ]; 
} 

创造了这个迁移:

use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateExampleJsonsTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('example_jsons', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string("name"); 
      $table->json("jsontest")->default("{}"); 
     }); 
    } 

    public function down() 
    { 
     Schema::dropIfExists('example_jsons'); 
    } 
} 

它给我以下例外:

Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1 near ")": syntax error (SQL: insert into "example_jsons"() select union all select)'

我对我的数据库和Laravel 5.4使用SQLite。

是什么导致了这种情况,我该如何解决这个问题?

回答

0

原来,$attributes似乎是由石膏到阵列之前被处理,将其更改为一个JSON字符串似乎解决它:

protected $attributes = [ 
    "jsontest" => "{}", 
]; 

我不能完全肯定这是发生了什么事,但我不再收到错误,数据正确保存到数据库。