sort images in database

1

Good, I have a table products and I want each product to have one or several images. so create a table images with a foreign key to products.

something like this:

create table producto(
  id_producto serial,
  nombre character varying(20),
  .... 
);

create table imagen(
  id_imagen serial,
  id_producto integer,
  nombre character varying(100),
  foreign key(id_producto) references producto(id_producto)
  ...
);

So far I think I'm doing fine, the problem is that I want that at the moment of creating a product, it has a main image (ideally that can be ordered), but if I put a field as principal boolean in the table of images and then someone adds an image and defines it as main, I will have 2 main images, so I may double my records by making a query that calls only the main one.

I do not know if I explain myself well, but I do not see how to be clearer.

The only thing that occurs to me is to validate this in the form, but I would like to know if I have any other option.

I would also like the images to be sorted. with a field posicion integer or something like that.

How do pages work like mercadolibre in this sense?

    
asked by Rodrigo Martín 24.06.2016 в 06:44
source

3 answers

1

If I have not misunderstood what you want to do is that only one of the images linked to a specific product is marked as main.

As you mention, this can be done prior to the insertion of each image outside of PostgreSQL, for example, every time an image is marked as "main" by placing all existing ones linked to the product unmarked with:

 UPDATE imagen SET principal = false WHERE id_producto = {id del producto implicado}

But if you want to do everything in PostgreSQL, one way would be by trigger, you have the official documentation in link .

It would be similar, you should only check inside the trigger if the values that are being inserted / updating change "main" and in case they do change with a UPDATE similar to the previous "main" values linked to the product before doing the new update / insert.

Regarding the sorting, if you have an integer field you could sort the records by that field without major problem, but the logic to keep a "main" would be more complicated, imagine that you would only have the whole field and the one that has value 1 it would be the "main"; the update logic update in that case (could also be done with trigger).

    
answered by 24.06.2016 / 09:52
source
2

So that a product only has a main image, what occurs to me is that in the product table you create the mainImage field and thus you ensure that you always have only one main image, for ordering the images, with a field order should be enough.

    
answered by 24.06.2016 в 07:00
2

You can say that your product has a main image from the list of images.

create table producto(
  id_producto serial,
  nombre character varying(20),
  imagen_principal serial,
  constraint fk_imagen_producto foreign key (imagen_principal) references imagen(id_imagen)
);

create table imagen(
  id_imagen serial,
  id_producto integer,
  nombre character varying(100),
  foreign key(id_producto) references producto(id_producto)
);

When you insert a new product you first insert the image you will have.

insert into imagen values (@idimagen,@idproducto,'nombre');

then insert the product with the id of its main image.

insert into producto values(@idproducto,'nombre',@idimagen);

To add normal images of the product, it is the same

insert into imagen values  (@idimagen,@idproducto,'nombre');

To find the product with its main image, you can search for it by its id

select p.id_producto,p.nombre,i.nombre
from producto p inner join imagen i
on p.imagen_principal = i.id_imagen
where p.id_producto = @idProducto;

To search all your images including the main one

select id_imagen, nombre from imagen where id_producto = @idproducto;

To search all your images without the main

select i.id_imagen, i.nombre 
from imagen i inner join producto p
on i.id_producto = p.id_producto
where p.id_producto = @idproducto
and p.imagen_principal != i.id_imagen;
    
answered by 24.06.2016 в 08:10