spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.78k stars 1.08k forks source link

uuid Data Type Compatibility Issue in Media Library Package Migration with MariaDB #3594

Closed Gonzalo2683 closed 6 months ago

Gonzalo2683 commented 6 months ago

How to Reproduce the Problem

The problem occurs when using the new mariadb connection, but the error does not occur when using mysql. with -> DB_CONNECTION="mysql" // no error with -> DB_CONNECTION="mariadb" // migration error

Problem Summary

When attempting to run the migration for the Laravel Media Library package on a MariaDB database, a SQL syntax error occurs. The error message indicates a problem near line 1 of the SQL statement used to create the media table.

Technical Details

The issue is caused by the definition of the uuid column in the migration. The Media Library package defines this column as a uuid data type that allows null values (nullable()).

 Illuminate\Database\QueryException

  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 'null, `collection_name` varchar(255) not null, `name` varchar(255) not null, ...' at line 1 (Connection: mariadb, SQL: create table `media` (`id` bigint unsigned not null auto_increment primary key, `model_type` varchar(255) not null, `model_id` bigint unsigned not null, `uuid` uuid null, `collection_name` varchar(255) not null, `name` varchar(255) not null, `file_name` varchar(255) not null, `mime_type` varchar(255) null, `disk` varchar(255) not null, `conversions_disk` varchar(255) null, `size` bigint unsigned not null, `manipulations` json not null, `custom_properties` json not null, `generated_conversions` json not null, `responsive_images` json not null, `order_column` int unsigned null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
patinthehat commented 6 months ago

@Gonzalo2683 Could you provide your PHP, Laravel, spatie/laravel-medialibrary, and MariaDB version numbers? I couldn't reproduce this issue using a fresh Laravel 11 project, this package v11.4, and MariaDB 11 (along with the Laravel mariadb driver.)

With MariaDB 11, the uuid column with a uuid type is created without issue:

image

Gonzalo2683 commented 6 months ago

Mariadb

MariaDB [db]> select VERSION();
+---------------------------------------------+
| VERSION()                                   |
+---------------------------------------------+
| 10.4.33-MariaDB-1:10.4.33+maria~ubu2004-log |
+---------------------------------------------+
1 row in set (0.000 sec)

Spatie package

composer show spatie/laravel-medialibrary
name     : spatie/laravel-medialibrary
descrip. : Associate files with Eloquent models
keywords : cms, conversion, downloads, images, laravel, laravel-medialibrary, media, spatie
versions : * 11.4.7
released : 2024-04-24, this week
type     : library
license  : MIT License (MIT) (OSI approved) https://spdx.org/licenses/MIT.html#licenseText
homepage : https://github.com/spatie/laravel-medialibrary
source   : [git] https://github.com/spatie/laravel-medialibrary.git 85f5f8284f827b8f2b426943e357e3df117ad1f5
dist     : [zip] https://api.github.com/repos/spatie/laravel-medialibrary/zipball/85f5f8284f827b8f2b426943e357e3df117ad1f5 85f5f8284f827b8f2b426943e357e3df117ad1f5
path     : /var/www/html/vendor/spatie/laravel-medialibrary
names    : spatie/laravel-medialibrary

Laravel y others

Laravel Version ............................................................................ 11.5.0
PHP Version ................................................................................. 8.2.18
Composer Version ........................................................................ 2.7.4
patinthehat commented 6 months ago

@Gonzalo2683 The issue appears to be with MariaDB versions below 10.7. According to the article linked below, 10.7 was when uuid data types were introduced - I would suggest using the mysql Laravel driver if you're unable to upgrade.

Since this is not an issue with the package, I'm going to close the issue. Thanks for the quick follow-up! :+1:

Reference: https://mariadb.org/10-7-preview-feature-uuid-data-type/