# Migration foreign key

**URL:** <https://www.ruby-forum.com/t/migration-foreign-key/109978>\
**Category:** Rails\
**Created:** [July 20, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/migration-foreign-key/109978 "2007-07-20T21:24:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DBC\_User](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@DBC\_User](https://www.ruby-forum.com/u/DBC_User)\
**Post date:** [July 20, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/migration-foreign-key/109978/1 "2007-07-20T21:24:00Z")

</div>

Hi All,

I am trying add a foreign key using db:migrate and getting error. I  
didn’t any documentation on the syntax, if some one has a link, please  
send it to me if not, please have a look at the following and tell me  
what am I doing wrong?

class name \< ActiveRecord:Migration  
def self.up  
create\_table :master …  
end

```
  add_index :fav, ["lid"], :name => "fk_lid"

  create_table:child ....
  end

  add_constraint "fk_lid" foreign_key ( "lid" ) references :child

```

(“id”)  
end

def self.down  
drop\_table :child  
drop\_table :master  
end  
end

---

<div class="post-metadata">

**Author:** ![DBC\_User](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@DBC\_User](https://www.ruby-forum.com/u/DBC_User)\
**Post date:** [July 20, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/migration-foreign-key/109978/2 "2007-07-20T22:21:12Z")

</div>

On Jul 20, 2:23 pm, DBC User [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> def self.down  
> drop\_table :child  
> drop\_table :master  
> end  
> end

Here is the link that had a good example  
[http://www.robbyonrails.com/articles/2005/11/11/rails-migrations-and-postgresql-constraints](http://www.robbyonrails.com/articles/2005/11/11/rails-migrations-and-postgresql-constraints)
