Added new tabel interiors_roomtypes (#521)
* migrations for the roomtypes * rename files * updated types * Added new tabel interiors_roomtypes * remove old migrations * fixed bug * correct numbers
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
-- Create a table called interiors_roomtypes
|
||||
-- This table should link the id in interiors table with a id from roomtypes table
|
||||
CREATE TABLE interiors_roomtypes (
|
||||
id SERIAL PRIMARY KEY,
|
||||
interior_id INTEGER NOT NULL,
|
||||
roomtype_id INTEGER NOT NULL,
|
||||
|
||||
-- Foreign key constraints with CASCADE DELETE
|
||||
CONSTRAINT fk_interiors_roomtypes_interiors
|
||||
FOREIGN KEY (interior_id) REFERENCES interiors(id) ON DELETE CASCADE,
|
||||
CONSTRAINT fk_interiors_roomtypes_roomtype
|
||||
FOREIGN KEY (roomtype_id) REFERENCES roomtypes(id) ON DELETE CASCADE,
|
||||
|
||||
-- Ensure unique combination of room and roomtype (prevent duplicates)
|
||||
UNIQUE(interior_id, roomtype_id)
|
||||
);
|
||||
|
||||
-- Create indexes for better query performance
|
||||
CREATE INDEX idx_interiors_roomtypes_interior_id ON interiors_roomtypes(interior_id);
|
||||
CREATE INDEX idx_interiors_roomtypes_roomtype_id ON interiors_roomtypes(roomtype_id);
|
||||
@@ -0,0 +1,40 @@
|
||||
-- Add interiors id to v_interior_image_urls
|
||||
|
||||
CREATE OR REPLACE VIEW v_interior_image_urls AS
|
||||
SELECT
|
||||
interiors.id interior_id,
|
||||
"product-printproducts".productid,
|
||||
interiors.print_id,
|
||||
"product-printproducts".groupid AS product_group,
|
||||
rooms.id AS room_id,
|
||||
rooms.name AS room_name,
|
||||
interiors.position,
|
||||
room_types.name AS room_type,
|
||||
CASE
|
||||
WHEN interiors.room_id IS null THEN
|
||||
'/interior-images/' || interiors.id || '.jpg'
|
||||
ELSE
|
||||
'/interiors/' || "product-printproducts".productid || '/' || rooms.s3_suffix
|
||||
END as uri,
|
||||
array_agg(DISTINCT roomtypes.name) FILTER (WHERE roomtypes.admin_specific = false) AS room_types
|
||||
FROM interiors
|
||||
JOIN "product-printproducts" ON "product-printproducts".printid = interiors.print_id
|
||||
JOIN "product-groups" ON "product-groups".groupid = "product-printproducts".groupid
|
||||
LEFT JOIN rooms ON interiors.room_id = rooms.id
|
||||
LEFT JOIN room_types ON room_types.id = rooms.room_type_id
|
||||
LEFT JOIN room_roomtypes ON room_roomtypes.room_id = rooms.id
|
||||
LEFT JOIN interiors_roomtypes ON interiors_roomtypes.interior_id = interiors.id
|
||||
LEFT JOIN roomtypes ON roomtypes.id = CASE
|
||||
WHEN interiors.room_id IS NULL THEN interiors_roomtypes.roomtype_id
|
||||
ELSE room_roomtypes.roomtype_id
|
||||
END
|
||||
GROUP BY
|
||||
interiors.id,
|
||||
"product-printproducts".productid,
|
||||
interiors.print_id,
|
||||
"product-printproducts".groupid,
|
||||
rooms.id,
|
||||
rooms.name,
|
||||
interiors.position,
|
||||
room_types.name,
|
||||
rooms.s3_suffix;
|
||||
Reference in New Issue
Block a user