Creation of tables with cardinality 1: N

2

I have two tables, PRODUTOS and INSUMOS . To create a precise product of various inputs, I would like to know how to proceed with this relationship in MySQL. Should I create a table called COMPOSICAO and work on it only with foreign key, this would be a form, are there other?

How do I proceed with this type of grating in Laravel?

    
asked by anonymous 30.08.2018 / 20:26

2 answers

1

Normally you need an auxiliary table of association or tie or intersection of products with inputs (this is called associative entity), where it will make the connection between them. I suppose it's this composition you're talking about, but I'm not sure I do not have the details.

In essence you will have the product code and input code on each line and it will already be the primary key. It is common to have a secondary key with the inverse input-output. I do not know if this is what the foreign key means.

Specifically in Laravel I do not know what to do, but I imagine that I should have to create a table like that in his model. I know that some will tend to want to make a graph of objects in memory. For me this is a mistake, replicate the model of the database and be happier.

    
30.08.2018 / 20:40
-1

If an input is not repeated in more than one product, you can place a foreign key in the Inputs table,

Insumos (*id*, nome, idproduto)
Produto (*id*, nome)

So you make sure that no other product will have that input.

Otherwise, you are correct. You should have an auxiliary table of type Composition,

Composição (*idproduto, idinsumo*)
    
06.09.2018 / 16:15