ActiveRecord中的PostgreSQL jsonb字段belongs_to关联 [英] PostgreSQL jsonb field in ActiveRecord belongs_to association

查看:46
本文介绍了ActiveRecord中的PostgreSQL jsonb字段belongs_to关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在belongs_to 关联中使用jsonb 字段作为foreign_key 吗?类似的东西:

Can I use a jsonb field as a foreign_key in a belongs_to association? Something along the lines of:

belongs_to :product, class_name: "Product",  foreign_key: "data ->'product_id'"

推荐答案

引用只能在列(或列组)之间定义,正如 文档:

References can be defined only between columns (or groups of columns), as it is stated in the documentation:

FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn[, ... ] ) ]

FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]

这篇关于ActiveRecord中的PostgreSQL jsonb字段belongs_to关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆