Rails ActiveRecord:没有主键的旧表显示nil结果? [英] Rails ActiveRecord: legacy table without primary key shows nil for result?
问题描述
我有一个Rails应用程序,它将坐在旧数据库的顶部,有一些我不得不处理的丑陋的表。一个是与 features
相关的 feature_attributes
表。问题是这个 feature_attributes
表没有主键。我不会认为这将是一个问题,但显然是。我有我的模型名称不同于表名称,但我使用 set_table_name
指定正确的名称。
I've got a Rails app that'll be sitting on top of a legacy database with some ugly tables that I'm having to deal with. One is a feature_attributes
table related to features
. Problem is that this feature_attributes
table doesn't have a primary key. I wouldn't think that'd be a problem, but evidently it is. I've got my model name which is different from the table name, but I'm using set_table_name
to specify the right one.
class Feature < ActiveRecord::Base
has_many :feature_attributes
end
class FeatureAttribute < ActiveRecord::Base
set_table_name 'feature_attribute'
belongs_to :feature
end
一旦我加载了一个我知道的与 feature_attributes
相关的功能,并调用 feature.feature_attributes
nil
。事实上,即使 FeatureAttribute.first
给我 nil
。 FeatureAttribute.any?
的结果会返回 false
。我担心ActiveRecord不从表中读取任何数据,因为没有主键。这是怎么回事?
Once I load a feature that I know has related feature_attributes
and call feature.feature_attributes
on it, I get nil
. Matter of fact, even FeatureAttribute.first
gives me nil
. The result of FeatureAttribute.any?
returns false
. I'm worried that ActiveRecord isn't reading any of the data from the table because there isn't a primary key. Is that what's going on here?
feature_attribute
表有以下列。
feature_id
attribute_name
attribute_value
created_date
modified_date
帮助!
我还要注意,直接运行生成的SQL MySQL服务器实际上得到我想要的行。
I'll also note that running the generated SQL directly against the MySQL server actually gets me the rows I want.
SELECT `feature_attribute`.* FROM `feature_attribute` WHERE `feature_attribute`.`feature_id` = 24;
EDIT :很抱歉。下次我将学习检查我的 database.yml
。显然,我正在从一个测试数据库读取具有相同的功能表,但 feature_attribute
表是完全空的。
EDIT: I am so sorry. Next time I'll learn to check my database.yml
. Evidently I was reading from a test database that had an identical feature table, but the feature_attribute
table was totally empty.
我觉得自己是个白痴。谢谢大家的帮助;我会投票给你们所有的烦恼。我真的喜欢大家的答案。 (我可以自己投票吗?:))
I feel like a moron. Thanks for your help, everyone; I'm up voting you all for your troubles. I did like just about everybody's answer. (Can I down vote myself? :))
推荐答案
尝试设置主键:
class FeatureAttribute < ActiveRecord::Base
set_table_name 'feature_attribute'
set_primary_key 'feature_id'
belongs_to :feature
end
UPDATE
我认为你的问题在别处。我只是测试和ActiveRecords工作正常与没有主键的表:
I think your problem lies anywhere else. I just tested and ActiveRecords works fine with tables without a primary key:
对于一个简单的表:
class CreateThings < ActiveRecord::Migration
def change
create_table :things, :id => false do |t|
t.string :name
t.timestamps
end
end
end
在控制台:
Loading development environment (Rails 3.1.1)
irb(main):001:0> Thing.create(:name=>'A name for the thing')
(0.1ms) BEGIN
SQL (0.3ms) INSERT INTO `things` (`created_at`, `name`, `updated_at`) VALUES ('2011-11-02 16:33:48', 'A name for the thing', '2011-11-02 16:33:48')
(40.3ms) COMMIT
=> #<Thing name: "A name for the thing", created_at: "2011-11-02 16:33:48", updated_at: "2011-11-02 16:33:48">
irb(main):002:0> Thing.first
Thing Load (0.7ms) SELECT `things`.* FROM `things` LIMIT 1
=> #<Thing name: "A name for the thing", created_at: "2011-11-02 16:33:48", updated_at: "2011-11-02 16:33:48">
irb(main):003:0>
UPDATE 2
不太好:
UPDATE 2 Not very fine:
irb(main):003:0> Thing.create(:name=>'Another thing')
(0.2ms) BEGIN
SQL (0.4ms) INSERT INTO `things` (`created_at`, `name`, `updated_at`) VALUES ('2011-11-02 16:40:59', 'Another thing', '2011-11-02 16:40:59')
(35.4ms) COMMIT
=> #<Thing name: "Another thing", created_at: "2011-11-02 16:40:59", updated_at: "2011-11-02 16:40:59">
irb(main):004:0> Thing.first
Thing Load (0.5ms) SELECT `things`.* FROM `things` LIMIT 1
=> #<Thing name: "A name for the thing", created_at: "2011-11-02 16:33:48", updated_at: "2011-11-02 16:33:48">
irb(main):005:0> Thing.last
Thing Load (11.8ms) SELECT `things`.* FROM `things` ORDER BY `things`.`` DESC LIMIT 1
Mysql2::Error: Unknown column 'things.' in 'order clause': SELECT `things`.* FROM `things` ORDER BY `things`.`` DESC LIMIT 1
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'things.' in 'order clause': SELECT `things`.* FROM `things` ORDER BY `things`.`` DESC LIMIT 1
这篇关于Rails ActiveRecord:没有主键的旧表显示nil结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!