Framework: Undefined property: stdClass::$column_name when generating a model

Created on 20 Jul 2017  路  8Comments  路  Source: laravel/framework

  • Laravel Version: 5.1
  • PHP Version: 7.1
  • Database Driver & Version: mysqlnd 5.0.12-dev

Description:

The MySqlProcessor assumes that the result from MySQL contains a lowercase column called column_name.

On my installation, if I run the query from compileColumnListing of MySqlGrammar

select * from information_schema.tables where table_schema = ? and table_name = ?

The result contains the column COLUMN_NAME in uppercase.

I don't really know where this is changed. In the driver? Maybe it makes sense to change the query in MySqlGrammar to ensure the correct case of the result:

select column_name as "column_name" from information_schema.tables where table_schema = ? and table_name = ?

Steps To Reproduce:

If I try to create a new model using the builder-plugin for OctoberCMS, I get the following error:

"Undefined property: stdClass::$column_name" on line 18 of /var/www/public/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/MySqlProcessor.php

There is already an issue in that repo: https://github.com/rainlab/builder-plugin/issues/161

Most helpful comment

In the meantime if someone wants to automate patching this in vendor/ the command I used is

sed -i 's/select column_name from information_schema/select column_name as `column_name` from information_schema/g' 'vendor/laravel/framework/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php'

I've put this as a post-update-cmd hook in composer.json to apply it automatically.

All 8 comments

In Laradock:
laradock1
In Debian Mysql Server
debian-mysql-server

Also encountered this in Laravel 5.4.28. Environment is PHP 7 and MySQL 8.0 using Laradock.

The fix suggested above by @metaodi worked.

Changed the query found in Illuminate\Database\Schema\Grammars\MySqlGrammar@compileColumnListing:

select column_name from information_schema.columns ...

...to...

select column_name as `column_name` from information_schema.columns ...

@hyubs Thanks! It works!

PHP 7.0.21, MySQL 8.0.1-dmr
Error: Undefined property: stdClass::$column_name
Example: Schema::connection('mysql2')->hasColumn('pharmacy', 'id')

Is there a solution to this problem that doesn't involve patching something in /vendor pulled in by composer? Otherwise the patch is going to have to be applied every time the project dependencies are pulled in.

@themsaid why did you close this? Shouldn't the fix mentioned above by @hyubs be implemented?

@metaodi feel free to open a PR

Opened a PR #21037

In the meantime if someone wants to automate patching this in vendor/ the command I used is

sed -i 's/select column_name from information_schema/select column_name as `column_name` from information_schema/g' 'vendor/laravel/framework/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php'

I've put this as a post-update-cmd hook in composer.json to apply it automatically.

Was this page helpful?
0 / 5 - 0 ratings