MySQL Reserved Words Checker for Laravel Migrations on PhpStorm

January 15, 2020


In this article we are going to setup a kind of "typo checker" on PhpStorm to look for MySQL reserved words being used as column names in your Laravel migration files. Although it's fine to name columns whatever you like, there is one small issue when writing raw SQL queries against the database when you do use reserved words as column names.

The problem when querying column names with reserved words

In our scenario we have the settings database described as follows:

MariaDB [dummy_db]> describe settings;
| Field         | Type             | Null | Key | Default | Extra          |
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| key           | varchar(191)     | NO   | UNI | NULL    |                |
| value         | longtext         | YES  |     | NULL    |                |
| description   | varchar(191)     | YES  |     | NULL    |                |
6 rows in set (0.04 sec)

Notice that we are using a column named "key" and that's fine, our Database Management System (DBMS) did not complain about it. However, we use that same word for commands like ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol;.

Now let's suppose we want to retrieve the data in our settings table filtering by some specific key. To achieve that we can run the query bellow, which blows up, because it doesn't know what "key" is.

MariaDB [dummy_db]> select * from settings where key like '%primary_color%';
ERROR 1064 (42000): 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 'key like '%primary_color%'' at line 1

Of course, to make it work all we have to do is escape key with backticks like the snippet bellow, but that's a little bit annoying. When using resources like Object Relational Mapper (ORM) it will take care of that for you automatically, so you don't have to worry about it, but every time you find yourself in situations when you have to query directly in the database, you must remember to escape the necessary fields.

MariaDB [dummy_db]> select * from settings where `key` like '%primary_color%';
| id | key           | value   | description |
| 46 | primary_color | #122560 | NULL        |
1 row in set (0.01 sec)

Since this is not something prohibitive, we are going to setup a small checker on PhpStorm to remind us when using reserved words. Let's dive in!

Setting up the PhpStorm checker

  1. Open PhpStorm and go to Settings → Editor → Inspections → General → Structural search inspection and click in the plus icon to add a new one and select Add search template…;

    Structural search inspection configuration.

  2. In the Search template text area type $table->$type$('$column$') and select the Search target as "column". Also, make sure to uncheck the Match_case option;

    Search template

  3. Click or select the $column$ portion inside the text area and then click on Add filter and choose Text;
  4. Paste the list of reserved words bellow, make sure the Words option is checked and then hit OK;

    Pasting search rules

  5. You will be prompted to give a name for the template. You can name it however you like, but personally, I like to give a meaningful name so it's easily searchable when needed. So I called "MySQL reserved words checker".
  6. And that's it! One last thing I like to tweak is the severity level of the warning. I like to put it as a TYPO level, so I can see it highlighted in the code as the image bellow.

    Example of a check in a migration file

Hope you like it. It's something strictly cosmetic but it is very handy to me.