1

I know this has been asked many times before but I have already been searching for an answer for the last 3 hours on stackoverflow with no success, none of the solutions work in my case. My model relationships are working fine on localhost but as soon as push my changes to production server (with the same config), relationships between models stop working.

Here is my File.php model relationship with Language.php model

public function language()
{
  return $this->belongsTo('App\Language', 'language_id', 'id');
}

I have even specified the keys by which to link them.

Here's where the problem occurs. On my local machine, everything works great with this code

$files = File::orderBy('updated_at', 'DESC')->get();
return $files[0]->language;

I get the following result enter image description here

Now when I do the same on production server, I get empty result. You might say that the problem is with records on database, well it's not. Here's a proof: File record from production database enter image description here

And here's the languages table enter image description here

Data types of foreign key column (language_id and id column in languages table data types are both unsigned bigIntegers with length of 20).

It's not just a problem with languages model, I have some other models linked with the same logic and none of them work in production, despite them working on local host.

By the way, in phpmyadmin on localhost I can select values based on foreign key connection, however on server I cannot do that:

Localhost: enter image description here

Server:

enter image description here

Any help would be much appreciated

EDIT: I have found that there was an issue with table engine, had to be set as innoDB (check the answers), so that fixes the database issue. However, I still have problems with Laravel recognizing these relationships.

Matrix
  • 437
  • 5
  • 18
  • $files is a collection..... returning $files[0] is not 100% OK. You should do it this way....$files->first()->language. – nikistag Jun 06 '20 at 15:02
  • QuickTip: install clockwork -> enable it on production(temporarily) -> see what queries are executed behind the scenes(both local/production) -> execute these queries against the both database servers -> (if the queries are same, check tables - if queries are different try to find the difference) – Ersoy Jun 06 '20 at 15:04
  • @nikistag tried both ways, no difference. But thanks for the tip – Matrix Jun 06 '20 at 15:09
  • Did you import full DB or run migrations then populate DB? It is possible that you had an error on your foreign keys and as a production server it was hidden. – nikistag Jun 06 '20 at 15:20
  • @nikistag I did the migrations and populated later on. So perhaps that might be possible. I will try to clear whole database and then re-migrate everything back on. – Matrix Jun 06 '20 at 15:41
  • are you using windows and your server is linux? – OMR Jun 06 '20 at 16:20
  • @OMR yes, I use windows, while server is Linux. – Matrix Jun 07 '20 at 15:31
  • your 'app' folder with capital letter, or small letter? – OMR Jun 07 '20 at 15:36

1 Answers1

0

For anyone with similar issue, check that your tables are created with InnoDB engine and not MyISAM or any other. In laravel you can specify this in app/config/database.php. Just change the engine parameter to 'InnoDB' instead of null under MySQL configuration.

Laravel & InnoDB

This fixed the issue with database, laravel side is still broken

Matrix
  • 437
  • 5
  • 18