【问题标题】:Laravel Migration: Error in Json column on Hosting Server but not on Local Xampp ServerLaravel 迁移:托管服务器上的 Json 列错误,但本地 Xampp 服务器上没有
【发布时间】: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


【解决方案1】:

请检查您的 MySQL 版本是否为 5.6 或低于功能 json 将不起作用。 您应该使用 MySQL 5.7 或更高版本来使用 json 函数。或者在 MySQL 5.6 或更低版本中使用“text”作为列。

【讨论】:

  • 是的,你是对的。我认为这就是问题所在。我当前的 SQL 版本不支持 JSON 数据类型。非常感谢,它有帮助。
  • 太棒了。您能否为它投票,因为它也可能对其他人有所帮助。
猜你喜欢
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
  • 2020-01-31
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
相关资源
最近更新 更多