简体   繁体   中英

Rails 6, Schema file not matching migration files after switching Git branch

Rails 6, development with sqlite3.

My schema.rb has a file that shouldn't be there: no migration files (On current git branch) says it should be generated. By it's name I can see it's from an earlier branch that I abandoned, and wen't back to try a different approach to building my rails app.

To double check: I get a name error when I try to access the table in the Rails Console, so it's only there in the schema file, but not in the Database itself.

Can I force rails to run and or confirm that current schema is matching the migration files and if not matching, would run the migration?

Edit/Update: I need to clarify that I have 6 migration files, that I went over to make sure none of them were from the earlier abandoned branch.

(admins is the table at issue) ActiveRecord::Base.connection.tables in the rails commandline generates: ["schema_migrations", "ar_internal_metadata", "events", "admins", "details"] When I do Event I get the columns name and type. But when I do Admin I get

Traceback (most recent call last):
        1: from (irb):3
NameError (uninitialized constant Admin)

So the issue is: How do I correctly reset the database to

The schema.rb file is auto-generated from the current state of the database, so just run rails db:migrate to re-generate schema.rb file

running rails db:migrate:reset removed seem to have fixed the issue: My schema file does not have the 'admins' in it, and it does not show up when running ActiveRecord::Base.connection.tables in rails commandline.

I believe this was caused by Git, as it has the DB in the files: /db/*.sqlite3 and that 'admins' was created on a branch I abandoned earlier and never merged. So it was kept in the database, but the migration files were removed when I went back and created a new branch earlier on the timeline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM