Closed
Description
I have the following tables:
CREATE TABLE locations (
state VARCHAR(2) NOT NULL,
zip VARCHAR(5) NOT NULL,
id INT8 NOT NULL,
name VARCHAR(255) NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (state ASC, zip ASC, id ASC),
FAMILY "primary" (state, zip, id, name)
)
CREATE TABLE addresses (
id UUID NOT NULL DEFAULT gen_random_uuid(),
line_1 VARCHAR(255) NOT NULL,
line_2 VARCHAR(255) NULL,
line_3 VARCHAR(255) NULL,
city VARCHAR(255) NOT NULL,
state VARCHAR(2) NOT NULL,
zip VARCHAR(5) NOT NULL,
inserted_at TIMESTAMP(0) NOT NULL,
updated_at TIMESTAMP(0) NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
FAMILY "primary" (id, line_1, line_2, line_3, city, state, zip, inserted_at, updated_at)
)
CREATE TABLE locations_addresses (
state VARCHAR(2) NOT NULL,
zip VARCHAR(5) NOT NULL,
id INT8 NOT NULL,
address_id UUID NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (state ASC, zip ASC, id ASC, address_id ASC),
CONSTRAINT fk_locations FOREIGN KEY (state, zip, id) REFERENCES locations(state, zip, id),
CONSTRAINT fk_addresses FOREIGN KEY (address_id) REFERENCES addresses(id),
INDEX locations_addresses_auto_index_fk_addresses (address_id ASC),
FAMILY "primary" (state, zip, id, address_id)
) INTERLEAVE IN PARENT locations (state, zip, id)
I need to be able to designate a many_to_many relationship where one side of the relationship is a composite foreign key. In my Location
schema, I expected to be able to define the following:
many_to_many :addresses, Address,
join_through: "locations_addresses",
join_keys: [location_id: [:state, :zip, :id], address_id: :id]
but this returns the following error:
** (ArgumentError) many_to_many :addresses expect :join_keys to be a keyword list with two entries, the first being how the join table should reach the current schema and the second how the join table should reach the associated schema. For example: [location_id: :id, address_id: :id]
Rationale
I'm attempting to use CockroachDB's parent interleaving functionality which requires that interleaving tables use composite primary keys to establish a hierarchical relationship. More on that here:
https://www.cockroachlabs.com/docs/stable/interleave-in-parent.html
Metadata
Metadata
Assignees
Labels
No labels