I don’t know why a new table was created with _20210402
Not sure what’s happening here, any ideas?
Hey @brlafreniere!
Is there a chance you could create a GitHub repo with a sample application with steps on how to reproduce this bug?
If that’s not possible, could you describe how you created the migrations or what command(s) you ran? Is this error reproducible? As in, if you reset (roll back db for example) your changes, can this same error be reproduced or is it a one off?
I dug in a little deeper, and I think Rails is doing some database inspection.
I think it’s creating this inv_products_XYZ table because I’m actually loading data into this database from a prior pg_dump.
After I run a rake db:migrate
it seems to create that table. Nowhere in my code is there a migration which is creating this table, it just somehow gets into my schema.rb
file. I think via inspection/introspection or whatever. Rails must see the table exists already, and adds the corresponding Ruby code in schema.rb
to create that table.
I can’t provide code in this case, since it’s a proprietary codebase.
That is correct. schema.rb
has nothing to do with migrations (despite the superficial connections).
It is always a record of the schema of the database. If you use tools other than migrations (e.g. the pg*
command line tools) to make changes to the database schema they will always be reflected in schema.rb
after you run migrations. This isn’t becuase of the migrations, but because there is a task triggered after the migrations task runs which translates the complete DB schema into schema.rb
.
This is not entirely true. Rails provides the following configuration options which link migrations to schema.rb:
I am also confused by Rails behavior when it does dump to include local database changes that are not part of migrations in the current branch. For instance, there may be changes to the database that have come from a different branch. Rails picks these changes up and modifies schema.rb causing a headache of management of this file.
It is because it is looking at the live connection to the DB. Whatever is in the DB at the time it connects will be dumped into the schema.rb. It doesn’t matter at all what code is in the branch. The real DB schema isn’t branched, so unless you are very careful about dropping the DB and recreating it when you switch branches, it is a headache you have to manage with appropriate painkillers.