spatie / laravel-medialibrary

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

Unable to upload file due to "Invalid object name 'media'" error #3024

Closed mburkeSencon closed 1 year ago

mburkeSencon commented 2 years ago

Discussed in https://github.com/spatie/laravel-medialibrary/discussions/3017

Originally posted by **mburkeSencon** September 1, 2022 This may be a bug, but I've not seen any references to this error so I'll write it like a bug report and it can be moved if required. **Background** I have upgraded a Laravel project from 6 through to 9, which also meant upgrading Spatie Activity Log and Media Library incrementally. The server runs a new version of Ubuntu (Ubuntu 22), PHP 8.1 and the database is MSSQL. **What I did** Part of testing involved uploading a file to part of the software. This software was initially written and developed in Laravel 6. I took two files (a 7MB PDF file, and a 2b text file) and tried to upload these individually to the system. **What I expected to happen** I expected the file to upload like it did before, pre-upgrade. **What happened** A whole world of pain and anguish descended upon me. I mentioned two files were used. All of this started with a 7MB PDF file. Using the 7MB file returned an initial error of: > laravel.ERROR: The current request does not have a file in a key named `media` Then performing: `dd($request->files->get('media')->getErrorMessage())` within the controller responsible for uploading the file returned (initially) that the file had only been partially uploaded. That's all the error I could get at that time. I since found elsewhere that using a smaller file could make a difference in terms of troubleshooting, so I then decided to upload a 2 byte file. This returned a different error altogether. > SQLSTATE[42S02]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Invalid object name 'media'. The table exists. It's at this point I'm stuck. I'm not sure if it's a bug or not, but I know for myself personally I've spent all day with this and haven't got anywhere with it.
angeljqv commented 2 years ago

Duplicate of #3017

mburkeSencon commented 2 years ago

It's a duplicate because I started it in the Q&A section and reasoned that it's possibly a bug and would be better off raising it as an issue. Especially as it's been 6 days of not being able to upload anything to my software.

Anyway, I've looked in to it myself today in order to help progress something and I've identified what I believe to be the issue.

In the src/MediaCollections/Models/Media.php, Line 48, we have:

protected $table = 'media';

I've been playing with this specifically today and notice that if I changed this to protected $table = 'DatabaseName.dbo.media' my error resolves itself. To be specific, it will only work if I provide the name of the database as well as the table names. Using dbo.media still results in the error. So to me the package isn't accessing the Database being used, nor is it able to handle the dbo part of the naming convention of MSSQL. AFAIK anyway.

I could add dbo.media to this line myself and be done with it, but like I said originally this all worked before the upgrades so I'm surprised to see this package being less flexible regarding database use. But it surprises me more that the package doesn't seem to know what database it's connected to?

sebastiandedeyne commented 2 years ago

Hi @mburkeSencon,

I dug around a bit and this looks a lot like a bunch of reports I found of people having issues with Laravel MSSQL driver.

Some reports I came across:

The first person had the exact same issue with a relation. The second mentions Laravel requiring additional config from MSSQL, not sure if that helps?

My odbc.ini file had some entries that were missing the Database = dbname line, so Laravel could see no default schema.

Either way, unfortunately this sounds like a deep database adapter issue (that might have been introduced by upgrading Laravel), and not something related to this package.

mburkeSencon commented 2 years ago

I disagree with it being an issue of the database adapter. If it was an issue I would expect other packages that require use of the database to be falling down with this issue. I've not had that happen, the issue has been specific to this package and how it handles multiple databases.

I had to move on from it in the end anyway, as my boss wasn't particularly impressed with the lack of progress of the issue and having a broken site after an upgrade.

I would urge someone though to just maybe look at how the Activity Log package you guys do interacts with the database, as we're running that after the upgrades and we've not encountered the issue. It really has been specific to the media library package.

patinthehat commented 1 year ago

@mburkeSencon Due to the numerous sources that indicate that this is likely related to a deeper database adapter problem, I am closing this issue. Feel free to open a new issue (with example unit tests failing) if you feel that this is not the case.