# Code works in Console but crashes in migration

**URL:** <https://www.ruby-forum.com/t/code-works-in-console-but-crashes-in-migration/158467>\
**Category:** Rails\
**Created:** [January 23, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/code-works-in-console-but-crashes-in-migration/158467 "2009-01-23T17:45:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [January 23, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/code-works-in-console-but-crashes-in-migration/158467/1 "2009-01-23T17:45:35Z")

</div>

I am using a habtm relationship between two tables and the migration  
fails when updating the join table. However, it works when doing it  
step by step in the console!

Here is the code:  
class User \< ActiveRecord::Base  
acts\_as\_authentic  
belongs\_to :role  
has\_and\_belongs\_to\_many :dim\_teams, :association\_foreign\_key  
=\> :team\_id  
end

class DimTeam \< ActiveRecord::Base

set\_primary\_key “team\_id”

has\_and\_belongs\_to\_many :users, :foreign\_key =\> “team\_id”

end

class AddUserData \< ActiveRecord::Migration  
def self.up

```
role = Role.find_by_role('User')
team1 = DimTeam.find_by_team_name('Essex')
user = User.new
user.login = "snapes"
user.email = "removed_email_address@domain.invalid"
user.password = "password"
user.password_confirmation = "password"
user.role = role
user.save
user = User.find_by_login("snapes")
user.dim_teams << team1

```

end

def self.down  
end  
end

When I run the code step by step in the console, it works. The final  
user.dim\_teams \<\< team1 line produces the following output in  
development.log:  
SQL (2.8ms) INSERT INTO [dim\_teams\_users] ([team\_id], [user\_id])  
VALUES (13, 2)

When I run it as a rake task, I get  
SQL (0.0ms)[0m [0mDBI::DatabaseError: (102) [unixODBC][FreeTDS][SQL  
Server]Incorrect syntax near ‘)’.: INSERT INTO [dim\_teams\_users] ()  
VALUES ()[0m

Can anybody explain why the difference to me please?  
thanks

---

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [January 24, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/code-works-in-console-but-crashes-in-migration/158467/2 "2009-01-24T17:18:28Z")

</div>

Maybe team1 is nil when it’s being run in the migration? I would check  
it in debugger or add some logging statements just before that.

Any chance the migrations are being run against a different environment,  
and thus a different database?

dwh

---

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [January 26, 2009, 10:28am UTC](https://www.ruby-forum.com/t/code-works-in-console-but-crashes-in-migration/158467/3 "2009-01-26T10:28:12Z")

</div>

Useful idea about using logging statements. It’s definitely the  
correct environment though…  
Martin
