简体   繁体   English

迁移混乱后,Rails 3“测试真相”单元测试失败

[英]Rails 3 'test the truth' unit test failing after migrations mess-up

Finished tests in 0.074964s, 13.3398 tests/s, 0.0000 assertions/s. 以0.074964s,13.3398测试/秒,0.0000断言/秒的速度完成测试。

  1) Error:
test_the_truth(UserTest):
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'user_id' in 'field list': INSERT INTO `tickets` (`status`, `assignee`, `created_at`, `updated_at`, `id`, `project_id`, `user_id`) VALUES ('MyString', 1, '2013-02-22 12:40:19', '2013-02-22 12:40:19', 980190962, 64810937, 64810937)
    /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
    /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'

I generated/scaffolded models and everything. 我生成/搭建了模型以及所有内容。 The peculiar part is that I don't remember having a user_id column in the table. 奇怪的是,我不记得在表中有一个user_id列。 Furthermore, schema.db and all migrations do not have anything like this as well. 此外,schema.db和所有迁移也没有这样的功能。 I do not have such string (user_id) in my workspace How does rails my column should be named like that? 我的工作空间中没有这样的字符串(user_id),应该如何命名我的列?

Apparently there had been such thing as a column in the database but it got fixed in some weird messy way. 显然,数据库中存在诸如列之类的东西,但是它以某种怪异的混乱方式修复了。

PS If I disable the fixtures related to this table, it's all good. PS:如果我禁用与此表相关的固定装置,那一切都很好。

What is going on, from where does rails read this column name from, cause apparently it is not from my workspace and neither my db or the test db.So it probably stores it somewhere? 这是怎么回事,rails从哪里读取此列名,显然导致它不是来自我的工作空间,也不来自我的数据库或测试数据库,因此它可能存储在某个地方吗? Is there a way can I fix this mess? 有什么办法可以解决这个问题?

A minute after it I ask in SO, I find the answer myself, after hours gone to nothing before. 几分钟后,我便以空问告终。 This always happens to me. 这总是发生在我身上。

The answer is exactly what Rubyman commented - the fixtures should be modified to remove any legacy information! 答案正是Rubyman评论的内容-应该修改固定装置以删除所有旧信息! But it was a bit more subtle - I really did not have 'user_id' column/string anywhere in my fixtures, as I did not have it in my workspace. 但这有点微妙-我的装置中确实没有'user_id'列/字符串,因为我的工作区中没有。 BUT, as I am using 'belongs_to' and 'has_many', it would not be named 'user_id' in the model and anywhere, it would be named just 'user'. 但是,由于我使用的是“ belongs_to”和“ has_many”,因此在模型中和任何地方都不会将其命名为“ user_id”,而只会将其命名为“ user”。 So I searched and found a 'user' field in the fixture and had to remove it in order to fix the erroneus 'user_id' column name generated in the SQL. 因此,我在固定装置中搜索并找到了一个“用户”字段,必须将其删除,以修复SQL中生成的错误“ user_id”列名称。

Hope this helps somebody. 希望这对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM