Jocko
Jocko

Reputation: 537

When using my schema.rb inside a migration, I get an error regarding the ID field

DISCLAIMER: I am aware of the rake db:schema:load approach, but my organization has a long history of ignoring the schema.rb state in the git repository, and it's easier to take the migration approach, than it is to get an entire team to change their behaviors. So please assume I understand the approach exists, and that I have measured it viability. Please do not offer commentary on the way rails "should be done", because it's not an efficiently viable option for me at the moment.

Now that I got that out of my system.

I'm running:

Ruby-on-Rails: v5.2.4.3
Ruby:          v2.6.1p33

My code base uses a migration archiving approach, where periodically we condense all of the migrations into one migration that is effectively a snapshot of the schema.rb file, and then archive the existing migrations. This executes in our local and test environments but not in our production environment.

Recently I tried to use this approach, but when my tests executed I got a bunch of these errors:

Mysql2::Error: Field 'id' doesn't have a default value

For the sake of this question, its safe to assume that the ID fields on all of my tables are being generated using the Rails default approach, and I am not manually specifying id in my migrations.

The resulting schema.rb generates this type of output:

create_table "profiles", id: :integer, default: nil, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
  ...
end

There is no index declaration within that block for the ID field, so I assume Rails is assuming the ID field for me.

The issue appears to be that the schema.rb definition is not assuming the AUTO_INCREMENT default value for the ID field.

Anyone have any thoughts on why this is happening, or how to fix it?

Thanks in advance!

Upvotes: 1

Views: 53

Answers (0)

Related Questions