> ## Content Index
> Fetch the complete content index at: https://til.codes/llms.txt
> Use this file to discover other available public pages before exploring further.

# Creating tables and problems with primary key in Rails
- URL: https://til.codes/creating-tables-and-problems-with-primary-key-in-rails/
- Published: 2015-11-13T01:21:55.000Z
- Updated: 2015-11-13T01:22:53.000Z
- Author: Manu Ajith
- Tags: rails, mysql, #Import 2024-07-13 21:01

If you run into the following error when running a migration  
in your legacy Rails app with MySQL 5.7 or above

```
rake aborted!
"Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL:"

```

the fix is by monkey patching the MySQL Adapter using the following code.

```
# lib/patches/abastract_mysql_adapter.rb
class ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
    NATIVE_DATABASE_TYPES[:primary_key] = 'int(11) auto_increment PRIMARY KEY'
end

```

and then require it from your `environment.rb`

```
require File.expand_path('../../lib/patches/abstract_mysql_adapter', __FILE__)

```

This issue is fixed in the latest version of Rails, but your legacy apps would need to be patched manually.

If you are interested in learning more about change that caused this issue, you can refer [this](http://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-3.html?ref=til.codes) link