ActiveRecord中的PostgreSQL jsonb字段belongs_to关联

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 [, ... ] ) ]