# Raw SQL in Migrations?

**URL:** <https://discuss.rubyonrails.org/t/raw-sql-in-migrations/20249>\
**Category:** rubyonrails-talk\
**Created:** [November 9, 2007, 7:31pm UTC](https://discuss.rubyonrails.org/t/raw-sql-in-migrations/20249 "2007-11-09T19:31:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greg\_Willits](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@Greg\_Willits](https://discuss.rubyonrails.org/u/Greg_Willits)\
**Post date:** [November 9, 2007, 7:31pm UTC](https://discuss.rubyonrails.org/t/raw-sql-in-migrations/20249/1 "2007-11-09T19:31:15Z")

</div>

After reading AWDWR and searching the API, it appears the Rails' implementation of Migrations has no way to execute a raw SQL statement. True?

-- gw

---

<div class="post-metadata">

**Author:** ![Jodi1](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@Jodi1](https://discuss.rubyonrails.org/u/Jodi1)\
**Post date:** [November 9, 2007, 7:37pm UTC](https://discuss.rubyonrails.org/t/raw-sql-in-migrations/20249/2 "2007-11-09T19:37:07Z")

</div>

Greg -

---

<div class="post-metadata">

**Author:** ![Will\_Sobel](https://avatars.discourse-cdn.com/v4/letter/w/50afbb/32.png) [@Will\_Sobel](https://discuss.rubyonrails.org/u/Will_Sobel)\
**Post date:** [November 9, 2007, 7:37pm UTC](https://discuss.rubyonrails.org/t/raw-sql-in-migrations/20249/3 "2007-11-09T19:37:36Z")

</div>

> After reading AWDWR and searching the API, it appears the Rails' implementation of Migrations has no way to execute a raw SQL statement. True?

Simple, just use execute:

class MyMigration \< ActiveRecord::Migration &nbsp;&nbsp;def self.up &nbsp;&nbsp;&nbsp;&nbsp;execute "alter table bar add constraint fk\_bar\_foo foreign key (foo\_id) references foo(id)" &nbsp;&nbsp;end

&nbsp;&nbsp;def self.down &nbsp;&nbsp;&nbsp;&nbsp;# ... &nbsp;&nbsp;end end

---

<div class="post-metadata">

**Author:** ![Greg\_Willits](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@Greg\_Willits](https://discuss.rubyonrails.org/u/Greg_Willits)\
**Post date:** [November 9, 2007, 7:39pm UTC](https://discuss.rubyonrails.org/t/raw-sql-in-migrations/20249/4 "2007-11-09T19:39:19Z")

</div>

Doh! Found it (in AWDWR not in the API docs). Funny, it uses the same execute command that I opted for when I wrote a migrations framework for Lasso 😛

-- gw
