简体   繁体   中英

rake db:migrate with existing database

I'm trying to create models for an existing database. I have read-only access to this database. I've generated my models, but when I run rake db:migrate it wants to 'create' those tables. Is there a way to satisfy rails need for migrating without actually creating these tables (since they already exist)?

If your models and tables already line up to Rails's naming scheme - User model => users table etc, and your models inherit from ActiveRecord::Base , then you don't need to run a migration at all (and can't anyway since migrations by definition change your database and you have read-only access).

If the table names don't match up to the model names, you can either change your model names, or set self.table_name= in your model. For example, if you have a User model but the table is called accounts , you can do this:

class User < ActiveRecord::Base
  self.table = 'accounts'

  # other stuff here
end

Read here for more info: http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-table_name-3D

Also, you should delete your migration files. If you want to see the schema in db/schema.rb , you can do a schema dump by running rake db:schema:dump . That should generate the file, assuming your settings are correct in config/database.yml .

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