Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL jsonb field in ActiveRecord belongs_to association

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'"
like image 755
Eyal B Avatar asked Oct 01 '15 13:10

Eyal B


1 Answers

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

like image 50
klin Avatar answered Oct 26 '22 12:10

klin