ed_is_my_name
ed_is_my_name

Reputation: 631

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)?

Upvotes: 1

Views: 1650

Answers (1)

flanger001
flanger001

Reputation: 777

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.

Upvotes: 1

Related Questions