【发布时间】:2021-08-13 11:16:19
【问题描述】:
我最近为 wallet 下载并安装了一个包,我在其中发布了迁移,并且这些迁移在某些表中具有 JSON 列。
迁移:
public function up(): void
{
Schema::create($this->table(), function (Blueprint $table) {
$table->bigIncrements('id');
$table->morphs('payable');
$table->enum('type', ['deposit', 'withdraw'])->index();
$table->decimal('amount', 64, 0);
$table->boolean('confirmed');
$this->json($table, 'meta')->nullable();
$table->uuid('uuid')->unique();
$table->timestamps();
$table->index(['payable_type', 'payable_id', 'type'], 'payable_type_ind');
$table->index(['payable_type', 'payable_id', 'confirmed'], 'payable_confirmed_ind');
$table->index(['payable_type', 'payable_id', 'type', 'confirmed'], 'payable_type_confirmed_ind');
});
}
public function json(Blueprint $table, string $column): ColumnDefinition
{
$conn = DB::connection();
if ($conn instanceof MySqlConnection || $conn instanceof PostgresConnection) {
$pdo = $conn->getPdo();
try {
$sql = 'SELECT JSON_EXTRACT(\'[10, 20, [30, 40]]\', \'$[1]\');';
$prepare = $pdo->prepare($sql);
$prepare->fetch();
} catch (\Throwable $throwable) {
return $table->text($column);
}
}
return $table->json($column);
}
protected function table(): string
{
return (new Transaction())->getTable();
}
public function down(): void
{
Schema::drop($this->table());
}
此迁移在本地 xampp 服务器中运行良好,但我不明白为什么我的实时服务器中出现错误。以下是我尝试运行迁移时遇到的错误。
我尝试使用 $table->json('meta')->nullable(); 这也不起作用,尽管我认为这不是一个合适的做法,因为据我所知 $this->json() 调用 json 函数。
有些人还建议使用 Text 而不是 Json,但我觉得这不是一个解决方案。我们怎样才能解决这个问题?
【问题讨论】:
-
你检查过你安装的MySQL服务器版本是否支持JSON数据类型吗?
-
@matticustard 哦,是的,我认为这就是问题所在。我刚刚检查它不支持 Json 数据类型。谢谢
标签: laravel eloquent laravel-8 laravel-migrations