How would I model a product to have multiple image URLs and have only one as the default? Background: I'm using Postgres with Golang GORM library by jinzhu

Here is a basic representation of what I'm looking for, however I dont think it would be right to have a circular reference like this.  Is there a better way to achive what I'm looking for?  One idea of mine is to omit the primary_pictures_id and add a boolean field to the images table to flag a picture as the default (just not sure yet on how to enforce only one to be true)

products
    id
    name
    price
    primary_picture_id FK:images(id)

images
    id
    url
    products_id FK:products(id)
Yes, your idea can work. Add an is_primary boolean column to images:

ALTER TABLE images
ADD is_primary boolean NOT NULL;


and enforce the "No more than one primary picture per product" rule with a partial index:

CREATE UNIQUE INDEX images_primary_image_constraint
ON images (product_id)
WHERE is_primary;


The above index will ensure uniqueness of product_id among rows where is_primary is set. If you try to specify a second primary image for some product, the uniqueness will immediately be violated and the operation will fail.