首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >generated_conversions中的Laravel媒体库错误

generated_conversions中的Laravel媒体库错误
EN

Stack Overflow用户
提问于 2021-04-18 04:23:23
回答 1查看 203关注 0票数 1

我收到来自Media Library包的根错误。

代码语言:javascript
复制
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'generated_conversions' in 'field list' (SQL: insert into `media` (...

经过研究,我发现6天前有一个从v8到v9的升级,要求在介质表中添加一个JSON列generated_conversions。

下面是建议的迁移文件

代码语言:javascript
复制
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class AddGeneratedConversionsToMediaTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        if ( ! Schema::hasColumn( 'media', 'generated_conversions' ) ) {
            Schema::table( 'media', function ( Blueprint $table ) {
                $table->json( 'generated_conversions' );
            });
        }
        
        Media::query()
            ->whereNull('generated_conversions')
            ->orWhere('generated_conversions', '')
            ->orWhereRaw("JSON_TYPE(generated_conversions) = 'NULL'")
            ->update([
                'generated_conversions' => DB::raw('custom_properties->"$.generated_conversions"'),
            // OPTIONAL: Remove the generated conversions from the custom_properties field as well:
            // 'custom_properties'     => DB::raw("JSON_REMOVE(custom_properties, '$.generated_conversions')")
            ]);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down() {
        /* Restore the 'generated_conversions' field in the 'custom_properties' column if you removed them in this migration
        Media::query()
            ->whereRaw("JSON_TYPE(generated_conversions) != 'NULL'")
            ->update([
                'custom_properties' => DB::raw("JSON_SET(custom_properties, '$.generated_conversions', generated_conversions)")
            ]);
        */
    
        Schema::table( 'media', function ( Blueprint $table ) {
            $table->dropColumn( 'generated_conversions' );
        });
    }
}

但是当我运行php artisan migrate:fresh --seed时,我得到了一个错误

代码语言:javascript
复制
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 '>"$.generated_conversions", `media`.`updated_at` = ? where `generated_conversion' at line 1 (SQL: update `media` set `generated_conversions` = custom_properties->"$.generated_conversions", `media`.`updated_at` = 2021-04-17 20:22:02 where `generated_conversions` is null or `generated_conversions` =  or JSON_TYPE(generated_conversions) = 'NULL')

有人知道这个文件的问题吗?谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-10-10 15:51:10

您需要遵循https://github.com/spatie/laravel-medialibrary/blob/main/UPGRADING.md提供的升级指南

代码语言:javascript
复制
## From v8 to v9

- add a `json` column `generated_conversions` to the `media` table (take a look at the default migration for the exact definition). You should copy the values you now have in the `generated_conversions` key of the `custom_properties` column to `generated_conversions`
- You can create this migration by running `php artisan make:migration AddGeneratedConversionsToMediaTable`.
- Here is the content that should be in the migration file
```php

使用Migrations\Database\Migrations\Migration;

使用Illuminate\Database\Schema\Blueprint;

使用Illuminate\Support\Facades\DB;

使用Illuminate\Support\Facades\Schema;

使用Spatie\MediaLibrary\MediaCollections\Models\Media;

类AddGeneratedConversionsToMediaTable扩展了迁移{

代码语言:javascript
复制
/**
代码语言:javascript
复制
 * Run the migrations.
 *
 * @return void
 */
public function up() {
    if ( ! Schema::hasColumn( 'media', 'generated_conversions' ) ) {
        Schema::table( 'media', function ( Blueprint $table ) {
            $table->json( 'generated_conversions' );
        } );
    }
代码语言:javascript
复制
    Media::query()
代码语言:javascript
复制
        ->where(function ($query) {
代码语言:javascript
复制
            $query->whereNull('generated_conversions')
代码语言:javascript
复制
                ->orWhere('generated_conversions', '')
代码语言:javascript
复制
                ->orWhereRaw("JSON_TYPE(generated_conversions) = 'NULL'");
代码语言:javascript
复制
        })
代码语言:javascript
复制
        ->whereRaw("JSON_LENGTH(custom_properties) > 0")
代码语言:javascript
复制
        ->update([
代码语言:javascript
复制
            'generated_conversions' => DB::raw('custom_properties->"$.generated_conversions"'),
代码语言:javascript
复制
            // OPTIONAL: Remove the generated conversions from the custom_properties field as well:
代码语言:javascript
复制
            // 'custom_properties'     => DB::raw("JSON_REMOVE(custom_properties, '$.generated_conversions')")
代码语言:javascript
复制
        ]);
代码语言:javascript
复制
}
代码语言:javascript
复制
/**
代码语言:javascript
复制
 * Reverse the migrations.
 *
 * @return void
 */
public function down() {
    /* Restore the 'generated_conversions' field in the 'custom_properties' column if you removed them in this migration
    Media::query()
            ->whereRaw("JSON_TYPE(generated_conversions) != 'NULL'")
            ->update([
                'custom_properties' => DB::raw("JSON_SET(custom_properties, '$.generated_conversions', generated_conversions)")
            ]);
    */
代码语言:javascript
复制
    Schema::table( 'media', function ( Blueprint $table ) {
代码语言:javascript
复制
        $table->dropColumn( 'generated_conversions' );
代码语言:javascript
复制
    } );
代码语言:javascript
复制
}

}

代码语言:javascript
复制
- rename `conversion_file_namer` key in the `media-library` config to `file_namer`. This will support both the conversions and responsive images from now on. More info [in our docs](https://spatie.be/docs/laravel-medialibrary/v9/advanced-usage/naming-generated-files).
- You will also need to change the value of this configuration key as the previous class was removed, the new default value is `Spatie\MediaLibrary\Support\FileNamer\DefaultFileNamer::class`
- in several releases of v8 config options were added. We recommend going over your config file in `config/media-library.php` and add any options that are present in the default config file that ships with this package.

```javascript
代码语言:javascript
复制
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67142630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档