- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在尝试通过以下方式创建与 rails has_many 的多对多关系:但我需要使用不同的列来创建关系,而不是使用模型主键 (id)。这是我的模型(顺便说一句,我使用的是 Rails 4):
class Food < ActiveRecord::Base
validates :NDB_No, uniqueness: true
validates :NDB_No, :FdGrp_Cd, :Long_Desc, :Shrt_Desc, presence: true
has_many :langual_factor_associations, primary_key: 'NDB_No', foreign_key: 'NDB_No'
has_many :langual_factor_descriptions, through: :langual_factor_associations, primary_key: 'NDB_No', foreign_key: 'NDB_No'
end
class LangualFactorAssociation < ActiveRecord::Base
validates :NDB_No, :Factor_Code, presence: true
belongs_to :food, foreign_key: 'NDB_No'
belongs_to :langual_factor_description, foreign_key: 'Factor_Code'
end
class LangualFactorDescription < ActiveRecord::Base
validates :Factor_Code, uniqueness: true
validates :Factor_Code, :Description, presence: true
has_many :langual_factor_associations, primary_key: 'Factor_Code', foreign_key: 'Factor_Code'
has_many :foods, through: :langual_factor_associations, primary_key: 'Factor_Code', foreign_key: 'Factor_Code'
end
与 LangualFactorAssociation 的 has_many 关联适用于 Food 和 LangualFactorDescription。但是 has_many through: Food 和 LangualFactorDescription 之间的关联不起作用。这是我在尝试访问 Food.LangualFactorDescriptions 时遇到的错误:
Food::should create the proper relations to the LangualFactorDescription
model#test_0002_must create the proper associations:
ActiveRecord::StatementInvalid: PG::Error: ERROR: operator does not exist: integer = character varying
LINE 1: ...sociations" ON "langual_factor_descriptions"."id" = "langual...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "langual_factor_descriptions".* FROM "langual_factor_descriptions" INNER JOIN "langual_factor_associations" ON "langual_factor_descriptions"."id" = "langual_factor_associations"."Factor_Code" WHERE "langual_factor_associations"."NDB_No" = $1 ORDER BY "langual_factor_descriptions"."id" ASC LIMIT 1
test/models/food_test.rb:172:in `block (3 levels) in <top (required)>'
我认为问题出在 ON "langual_factor_descriptions"."id"= "langual_factor_associations"."Factor_Code" 查询的这一部分。我认为设置 primary_key 和/或 foreign_key 选项可以解决这个问题,但事实并非如此。事实上,如果我从模型中删除它们,然后就这样保留
has_many :langual_factor_descriptions, through: :langual_factor_associations
rails 产生完全相同的查询,所以在我看来设置这些选项没有任何作用。我在这里错过了什么吗?关于如何告诉 Rails 不要查找 langual_factor_descriptions.id 而是查找 langual_factor_descriptions.Factor_Code,有什么想法吗?
以下是我阅读过的关于该主题的一些最相关的资源: http://guides.rubyonrails.org/association_basics.html
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
has_many :through with :primary_key on join table not working (这几乎是我遇到的问题,但我不确定这是正确的解决方案)
Rails has_many association with multiple keys
最佳答案
我想我解决了这个问题。这是代码:
class Food < ActiveRecord::Base
validates :NDB_No, uniqueness: true
validates :NDB_No, :FdGrp_Cd, :Long_Desc, :Shrt_Desc, presence: true
has_many :langual_factor_associations, primary_key: 'NDB_No', foreign_key: 'NDB_No'
has_many :langual_factors, through: :langual_factor_associations
end
class LangualFactorAssociation < ActiveRecord::Base
validates :NDB_No, :Factor_Code, presence: true
belongs_to :food, primary_key: 'NDB_No', foreign_key: 'NDB_No'
belongs_to :langual_factor, primary_key: 'Factor_Code', foreign_key: 'Factor_Code'
end
class LangualFactor < ActiveRecord::Base
validates :Factor_Code, uniqueness: true
validates :Factor_Code, :Description, presence: true
has_many :langual_factor_associations, primary_key: 'Factor_Code', foreign_key: 'Factor_Code'
has_many :foods, through: :langual_factor_associations
end
请注意,我没有在 has_many through: 关联中使用 foreign_key 或 primary_key 选项,并且不需要 self.primary_key
。
此外,这里还有一些对我有帮助的其他有用链接:
http://railsforum.com/viewtopic.php?id=36186
http://guides.rubyonrails.org/v2.3.11/active_record_querying.html
尽管问题的解决是巧合,但我相信这些链接提供了相关信息。
关于ruby-on-rails - Rails has_many :through with :primary_key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17666690/
我正在开发一个使用 rails 的代码库,之前的开发人员已经在 postgres 中创建了一些相互引用的模型。然而,他们使用 activerecord 的 primary_key 字段,即使数据库没有
在 .net 中创建 Edmx 时存在一个问题,即在创建数据库的 Edmx 时,仅添加具有主键的那些表和 View 。 这个问题很容易解决,但只是在 Table 或 View 中创建一个列主键,但我没
我读到,UUID 作为主键的插入性能取决于数据库中存在多少条记录。记录数量越多,插入记录所需的时间就越长 https://kccoder.com/mysql/uuid-vs-int-insert-pe
我有一个简单的 SQLAlchemy 模型,其中有一列用于 ID: id = Column(Integer, primary_key=True) 这将使 id 从 0 开始,并自动递增 1。如
我在 symfony 4 中的一个项目中工作,数据库在 postgresql 中。有两种更新数据库的方法: 方法 1. 通过带有普通 sql 插入的 python 脚本 方法2.通过symfony表单
我正在使用公共(public)事件 gem 在我的应用程序上创建事件流。为此,我有这个设置。 我已经为我的应用设置了一个友谊模型, create_table "friendships", force:
我有一个基于 WordPress 3.7.1 构建的网站。使用插件我进行数据库备份,但是当我尝试在开发服务器上导入该数据库时,我收到此错误: ERROR 1062 (23000) at line 94
我正在尝试在 Diesel 中使用 #[primarykey()] 宏,但出现未知错误。根据我的发现,添加 #![feature(primary_key)] 应该可以解决问题,但事实并非如此。 lib
我正在尝试创建一个使用 UUID_SHORT() 作为主键的表。我有一个触发器,可以在您执行插入操作时插入一个值。我无法让 sqlalchemy 将列识别为 primary_key 而不会提示不提供默
我们正在迁移我们的 Oracle 数据库并对其进行必要的更改,一个主要更改是我们将 UUIDField 添加为所有模型的主键(对客户端隐藏),并且(尝试添加) 一个常规的 AutoField。 我们发
我正在尝试通过以下方式创建与 rails has_many 的多对多关系:但我需要使用不同的列来创建关系,而不是使用模型主键 (id)。这是我的模型(顺便说一句,我使用的是 Rails 4): cla
我看到已经回答了一个非常相似的问题,讨论了同样的问题,但我似乎无法理解。下面是我在存储过程中的代码的副本。我需要以某种方式将第二个插入语句插入从第一个生成的 CompanyID 到第二个语句中。我很感
我知道外键用于指定子类上何时有不同的列名(不同于父子的类名)。我知道主键和外键是什么,并且多次阅读关于关联的 Rails 文档,但我无法弄清楚主键选项的用途。 1) 但是 primary_key 选项
您好,我正在测试Django身份验证和嵌套用户数据。我为用户创建了一个简单的MyProfil模型。我想测试制作自定义的id并将primary_key=True设置为id = models.UUIDFi
我知道外键用于指定子类上何时有不同的列名(不同于父子的类名)。我知道主键和外键是什么,并且多次阅读关于关联的 Rails 文档,但我无法弄清楚主键选项的用途。 1) 但是 primary_key 选项
我试图在模型中保存大量对象,因此我决定通过这种方式使用bulk_create: while True: batch = list(islice(records, 100)) if no
我有一个表,其中有一列包含另一个表中一行的 ID。但是,当表 A 被填充时,表 B 可能有也可能没有为表 A 准备好的行。 我的问题是,是否可以让 mysql 阻止输入无效值,但可以输入 NULL?还
我正在尝试在我的 helpdesk/models.py 中创建一个自定义的 primary_key,我将用它来跟踪我们的服务台票证。我正在为我们的办公室编写一个小型滴答系统。 也许有更好的方法?现在我
我需要添加一个不是主键的 autoinc 字段。我正在将使用自动递增字段的非常大的生产数据库迁移到 models.UUIDField .我一直在进行分段迁移,现在我的所有关系都与这两种字段类型重复。我
我的一个查询遇到了问题,有时速度很慢。 SELECT * FROM table WHERE primary_key IN (1,2,...) 对于大约 100 个条目,有时甚至需要 5 秒。显然我们在
我是一名优秀的程序员,十分优秀!