Laravel-medialibrary: Media Eloquent model class name is pluralized to medias

Created on 8 May 2020  ·  35Comments  ·  Source: spatie/laravel-medialibrary

I recognized a bug triggered by a suddenly pluralized model name media to medias which causes the query builder to run into an exception.

[2020-05-08 18:24:56] development.ERROR: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mee2.medias' doesn't exist

We are using Laravel 5.5.49 with version 6.9.2 of the medialibrary.

It was working today but stopped through a breaking change somewhere. I'll look further for the cause now.

Adding

protected $table = 'media';

in the model class fixes the breaking database query of course.

Most helpful comment

Meanwhile I've also tagged 8.2.5 of the medialibrary which explicitly set the table name to be used to media. So the latest version of the package should work.

Thanks for having createing those PRs to fix the root of the problem @patrickbrouwers

All 35 comments

The same bug
But I have the same repo at 2 pc's. At one of them I have this error...

Adding media as uncountable word in Laravel's Illuminate\Support\Pluralizer works aswell if I remove the $table override in the Media model class.

I don't understand yet where this breaking change came from.

The same bug.
For now u can fix it if use custom media model. In medialibrary.php change parameter 'media_model' set your custom model.
For example

class CustomMedia extends \Spatie\MediaLibrary\Models\Media
{
    protected $table = 'media';
}

That would be my workaround but we don't use a derivated media model yet.

I think this is related to the latest release of Inflector: https://github.com/doctrine/inflector/compare/1.3.1...1.4.0

It wasn't happening to me yesterday, but after updating packages today I have this error.

It's indeed related to Inflector, I've opened a PR to fix the breaking change: https://github.com/doctrine/inflector/pull/142

Thanks for the PRs @patrickbrouwers

Meanwhile I've also tagged 8.2.5 of the medialibrary which explicitly set the table name to be used to media. So the latest version of the package should work.

Thanks for having createing those PRs to fix the root of the problem @patrickbrouwers

Can you "backport" this fix into 6.9 aswell please @freekmurze

I'll do that after the weekend, if the PRs to inflictor are not merged by then.

Alright. I have a workaround by using a derived model class over the weekend and for the future.

But other people on 6.9 will run into this without an own model class.

Have a nice weekend!

@freekmurze would you be so kind as to accept my PR to the v7 and tag a v7 release? #1865

Off/on topic.

The correct table name and pluralisation of media, should still be media.

The Laravel issue suggested the plural of media is medium? which is incorrect - so this should me “media”

Right??

Indeed. The plural of medium is media but medium is not the plural of media.

As you've read: some users are expecting this instead of fixing their software.

Head -> Table.

@OwenMelbz I think Taylor said that by mistake and meant that the singular of media is medium.

I'm having this issue with medialibrary v7.19.3

Is there a way of telling laravel how to pluralize words?

some lang provider?
some custom langprovider?
something in appserviceprovider?

@eduardoarandah you can add a custom plural rule to Doctrine Inflector: https://www.doctrine-project.org/projects/doctrine-inflector/en/1.3/index.html#rules

That's really good to know, thanks.

Laravel/Doctrine even pluralizes equipment to equipments.

I'll just create an DoctrineProvider now. Problem solved.

TEMPORARY FIX:

This loads the last version of inflector without the bug:

composer require doctrine/inflector:1.3.1

thanks @mikemand

Also:

echo "\n\n# TODO: \n\n- Remove temporary fix: inflector 1.3.1" >> README.md

I just created a provider in the core package of our application and it works again.

So for this we are independent of what Laravel, Doctrine or whoever is doing here.

Would be nice if the root issue(s) are gonna be fixed so we don't have to work around this in application level.

@patrickbrouwers @eduardoarandah adding Inflector rules in a provider even breaks more.

Just added a provider which in does a

Inflector::rules('plural', [
    'media'         => 'media',
]);

in it's boot method.

Media stays media now but simple countable things aren't working anymore like value -> values, user -> users, car -> cars and break the whole application now, not only the parts where media library is used.

So I'll create a derivated model class with $table variable and refactor the whole application.

Annoying.

@synthor

You should mark it as uninflected:

Inflector::rules('plural', [
    'uninflected' => ['media'],
]);

i fixed by downgrading doctrine/inflector

"doctrine/inflector": "v1.3.0",

@synthor

You should mark it as uninflected:

Inflector::rules('plural', [
    'uninflected' => ['media'],
]);

this way will be deprecated in inflector 2.0, what should we do with new version?

You just need this rule until 1.4.1 is tagged.

@patrickbrouwers Thanks that works as expected. I mean I've tried that.

@matro-skin Calling Illuminate\Support\Str::pluralize('media') in Tinker gives me a deprecated notice with Laravel 5.5.

You just have to upgrade Laravel/Doctrine or stick with the notice. Depends on what you are doing/want to achieve.

I think it's better to move to config medialibrary.php

Inflector fix is going to be released later today

Inflector fix is going to be released later today

Good news, but sounds better to be independent and try to avoid future surprises. I mean to add table attribute to medialibrary config

So what happens when doctrine/inflexor fix is merged and 1.4.1 release done?

Happened 6 hours ago, but I still get the error on my deployments

Run an composer update.

I got doctrine/inflector 1.4.1 during a deployment today and it worked without any other change.

Closing this as it is fixed. @ssmusoke make sure you have 1.4.1 of inflictor pulled in.

@freekmurze yeah that was the issue, composer.lock file had the broken dependency

Was this page helpful?
0 / 5 - 0 ratings