Why is rails 5 adding nextval method in schema file?
This is a bit long of an answer, so I've broken it into sections. Buckle up!
My theory
My guess is that your development database does contain the lessons_id_seq
sequence, and that its definition of flightlessons.id
is set to depend on it (i.e., exactly what Rails is putting into your schema file).
How and why? You likely renamed the lessons
table to flightlessons
at some point in the past, but that rename didn't change the sequence that the table depended on -- and since schema.rb
does not record sequences, the lessons_id_seq
sequence does not get copied to your test database, and thus you get this error.
To verify my theory, run rails db
and try the following commands:
\d lessons_id_seq
This should return the definition of that sequence. Then, try:
\d flightlessons
And look at the definition of the id
column. I expect it to include DEFAULT nextval('lessons_id_seq')
.
Fixes
The easiest way to fix this is to switch to using structure.sql
instead of schema.rb
(see the docs). This will carry over the exact state of your database and avoid any interference or interpretation by Rails, which is what's causing your current issue. I always recommend structure.sql
for production systems.
However, you can also go into your development database and change the sequence name:
ALTER SEQUENCE lessons_id_seq RENAME TO flightlessons_id_seq;
ALTER TABLE flightlessons ALTER COLUMN id SET DEFAULT nextval('flightlessons_id_seq');
This would be a terrible idea on a production system, but if your issue is just local, it should rectify your current database state with your schema.rb
and thus address your current problem. You may wish to encode that into a migration, if you want rails db:drop db:create db:migrate
to work on a fresh app.
Why now?
The behavior where Rails is dumping out the default
value for your table's primary key may very well be new in Rails 5. Previously, Rails may have just trusted that your ID column had a sane default, and ignored whatever value it actually saw. But I haven't done the research to see if that's true or not.