我刚刚使用Rails迁移创建了一个新表,然后创建了一个我在其关系中添加的模型。
我重新启动了我的控制台,MySQL控制台显示了project_dev数据库中的表。
当我尝试在Ruby控制台中创建新记录时,我得到:
Mysql::Error: Table 'project_dev.trace_users' doesn't exist
from C:/Sanj/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/act
ive_record/connection_adapters/mysql_adapter.rb:287:in 'query'
from C:/Sanj/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/act
ive_record/connection_adapters/mysql_adapter.rb:287:in 'execute'
from C:/Sanj/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/act
ive_record/connection_adapters/mysql_adapter.rb:438:in 'columns'
from C:/Sanj/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/act
ive_record/base.rb:679:in 'columns'
from C:/Sanj/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/act
ive_record/persistence.rb:285:in 'attributes_from_column_definition'
from C:/Sanj/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/act
ive_record/locking/optimistic.rb:62:in 'attributes_from_column_definition'
from C:/Sanj/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.1/lib/act
ive_record/base.rb:1396:in 'initialize'
from (irb):2:in 'new'
from (irb):2
from C:/Sanj/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.1/lib/rails/c
ommands/console.rb:44:in 'start'
from C:/Sanj/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.1/lib/rails/c
ommands/console.rb:8:in 'start'
from C:/Sanj/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.1/lib/rails/c
ommands.rb:23:in '<top (required)>'
from script/rails:6:in 'require'
from script/rails:6:in '<main>'
答案 0 :(得分:2)
我昨天似乎比其他人更愿意承认 - 迁移表是以单数形式而不是复数形式创建的 - 这导致表trace_user而不是trace_users。
@Ben - 我调用了表,因为系统中有一些跟踪表记录了不同类型的事件,我更喜欢将所有跟踪表放在一起 - 恕我直言这有助于使表更容易理解理解整个系统。
答案 1 :(得分:1)
你有一个User模型,然后添加了一个Trace模型,对吗?你做的一切都正确,除了你在Trace中添加了has_many用户,你现在必须创建关联表(通过另一个迁移)。考虑使关联方向正确(用户has_many跟踪?),以便表格为user_traces,这听起来更好。