Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method `add_foreign_key' #116

Closed
vipankumar87 opened this issue Jul 15, 2013 · 10 comments
Closed

undefined method `add_foreign_key' #116

vipankumar87 opened this issue Jul 15, 2013 · 10 comments

Comments

@vipankumar87
Copy link

I have install this module as gem install foreigner and i use following code

class AddTaskEmployee < ActiveRecord::Migration
  def up
      add_foreign_key(:task_employes, :tasks, options: 'ON UPDATE DEFERRED')
  end
  class TaskEmploye < ActiveRecord::Base
    belongs_to :tasks
    belongs_to :employees
  end
  def down
  end
end

when i execute command rake db:migrate then i got this error

C:\Sites\projects>rake db:migrate
==  AddTaskEmployee: migrating ================================================
-- add_foreign_key(:task_employes, :tasks, {:options=>"ON UPDATE DEFERRED"})
rake aborted!
An error has occurred, all later migrations canceled:

undefined method `add_foreign_key' for #<AddTaskEmployee:0x3a2cdc8>C:/Sites/proj
ects/db/migrate/20130715061416_add_task_employee.rb:3:in `up'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
@matthuhiggins
Copy link
Owner

What version of Rails and which database adapter?

@dammitBrandon
Copy link

Matt I am encountering the same issue and I am running a sinatra application server that is designed to mimic the rails framework, its pretty stripped down, the application framework is designed with a subset of the rails functionality (i.e. core functionality, migrations, generation of migration files and models and so forth), I am somewhat new to this framework in experience but I have background experience with other languages and am hoping that you can provide some insight on how I can mitigate this issue, I have read all the documentation and attempted to follow the examples with no luck. I have looked at the Issues log also and looked for a resolution, same results, I really am excited to use this gem in my simple project and hopefully you can take a look at my repo and give me some insight. At this point anything will be helpful, checkout the foreigner branch specifically the errors.txt file it gives a full stack trace that I have pretty much followed all the way through.. thanks in advance..

regards
repo link:
https://github.com/dammitBrandon/craigslist_faker

@vipankumar87
Copy link
Author

i am using Rails 3.2.13 and mysql2 (0.3.11 x86-mingw32)

@matthuhiggins
Copy link
Owner

😦 I'll look Sunday night.

@matthuhiggins
Copy link
Owner

Is there an initializer where you can put this code?

ActiveSupport.on_load :active_record do
  Foreigner.load
end

@bmarcaur
Copy link

I am in a similar situation. Rails 3.2.13 with mysql2 0.3.11. I added your initializer and it didnt make a difference. Unfortunately I am on an older version of foreigner 1.2.1

@matthuhiggins
Copy link
Owner

@vipankumar1987 Did you add foreigner to your Gemfile first? It seems like you installed the gem without requiring it first.

@hparra
Copy link

hparra commented Apr 1, 2014

Related: if anyone is trying to use Foreigner with active_record_migrations then see rosenfeld/active_record_migrations#5

@matthuhiggins Any comment on that?

@coolbrg
Copy link

coolbrg commented May 30, 2014

I am also getting this issue.
Here is my details:

# models
class User < ActiveRecord::Base
    has_many :devices, dependent: :delete_all
end
class Device < ActiveRecord::Base
    belongs_to :user
end

# migration script
class CreateDevices < ActiveRecord::Migration
    def up
        create_table(:devices, primary_key: :deviceid) do |t|
            t.string :devicecode, limit: 5, default: nil
            t.string :devicemacaddr, limit: 20, default: nil
            t.string :devicename, limit: 20, default: nil
            t.string :deviceos, limit: 20, default: nil
            t.references :users
        end

        add_index :devices, :users_id
        change_column :devices, :deviceid, :integer, limit: 11, auto_increment: true
        add_foreign_key(:devices, :users, :column => 'users_id', dependent: :delete)
    end

    def down
        remove_index :devices, :users_id
        drop_table :devices
    end
end

Let me know where I could be wrong.
Full error stack here https://gist.github.com/budhrg/e667a03d0fb34f23bfd3.

@pjammer
Copy link

pjammer commented Dec 15, 2015

If you get here from google, and are using flipper, just add gem "foreigner" to your Gemflie.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants