# Raw SQL in Migrations?

**URL:** <https://www.ruby-forum.com/t/raw-sql-in-migrations/119737>\
**Category:** Rails\
**Created:** [November 9, 2007, 8:32pm UTC](https://www.ruby-forum.com/t/raw-sql-in-migrations/119737 "2007-11-09T20:32:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 9, 2007, 8:32pm UTC](https://www.ruby-forum.com/t/raw-sql-in-migrations/119737/1 "2007-11-09T20:32:03Z")

</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:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 9, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/raw-sql-in-migrations/119737/2 "2007-11-09T20:37:57Z")

</div>

Greg -

On 9-Nov-07, at 2:31 PM, Greg W. wrote:

> 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

not true -

execute “insert into easy\_as\_pie …”

cheers,  
Jodi

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 9, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/raw-sql-in-migrations/119737/3 "2007-11-09T20:38:33Z")

</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  
def self.up  
execute “alter table bar add constraint fk\_bar\_foo foreign key  
(foo\_id) references foo(id)”  
end

def self.down  
# …  
end  
end

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 9, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/raw-sql-in-migrations/119737/4 "2007-11-09T20:42:38Z")

</div>

On Nov 9, 2007, at 11:31 AM, Greg W. wrote:

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

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
