I'm starting with PostgreSQL views, since they are useful for my use case and will provide better performance than functions.
(This isn't relevant, but I'm using Django 1.7 on Heroku Postgres—just in case).
I have already created a view and can query it fine. I'd like to write a Django wrapper around the view so I can treat it like a table, and query and write to it accordingly. I've been reviewing the
Postgres docs on INSERT and UPDATE for views but to be honest I find their docs so hard to read I can barely parse through what they're saying.
Let's say I have the following view:
CREATE OR REPLACE VIEW links AS
SELECT
listing.id AS listing_id,
CONCAT('/i-', industry.slug, '-j-', listing.slug, '/') AS link,
'https://www.example.com' || CONCAT(industry.slug, '-SEP-', listing.slug, '/') AS full_link,
listing.slug AS listing_slug,
industry.slug AS industry_slug
FROM listing
INNER JOIN company ON company.id = listing.company_id
INNER JOIN industry ON industry.id = company.industry_id
Here, I'm using industry.slug and listing.slug to build links. I'd like to be able to update those two fields from this view, as such:
UPDATE links
SET listing_slug = 'my-new-slug'
WHERE listing_id = 5;
How do I create the rules to do this properly?
DO INSTEADrule or trigger. Rules are complicated and hard to get right, so I'd use a trigger.