Question

I have a table fluxo, and fluxo have many fluxo_acao, but a entry in fluxo_acao can have three types, and each type have they particularities. to acomplish this, i created more three tables(fluxo_acao_mensagem, fluxo_acao_pergunta and fluxo_acao_escolher_fila), connected to fluxo_acao.

The problem is i can't select any of the three tables, from fluxo, because first i need to check wich type is and them i do the query.

I need a better approach in how to acomplish this relation or connect fluxo_acao to the three tables. Even why i need create another two more. and this is the perfect time to change.

fluxo fluxo_acao other three tables

someone just told me Inheritance may help me. i'm searching how.

Était-ce utile?

La solution

So I can't see exactly what your primary keys are, but it sounds like your issue isn't with subtypes themselves, but more likely the choice of primary key and path dependence.

So if fluxo has a primary key id_fluxo, and the relation to fluxo_acao is one to many AND duplicates aren't too tightly controlled, you can set up your primary key as:

  1. id_fluxo_acao - bad, just a row pointer
  2. (id_fluxo, id_fluxo_acao) - okay, still a row pointer but no notion of what is unique
  3. (id_fluxo, ordem) (flow, order?) - better, but per your comment it could update frequently and would require updates to the related subtype.
  4. (id_fluxo, timestamp) - best, provides uniqueness and supplies valuable metadata

Then the primary key of each subtype will contain id_fluxo and you can join directly from fluxo to any of the subtypes.

Your comment:

But lets say that i want to select all the entries in the three tables from fluxo how can i do this with joins. After addind this other pk in fluxo_acao

This you can do easily enough with a simple query that can be hardened into a view later:

SELECT
  fluxo.id_fluxo
 ,fluxo.descricao
 ,fluxo.id_status
 ,fluxo.data_criacao
 ,fluxo.id_empresa
 ,fluxo.tipo
 ,fluxo_acao.tipo AS acao_tipo
 ,fluxo_acao.timestamp
 ,fluxo_acao.ordem
 /* columns from each subtype */
FROM
  fluxo fluxo
INNER JOIN
  fluxo_acao fluxo_acao
    ON fluxo_acao.id_fluxo = fluxo.id_fluxo
LEFT JOIN
  fluxo_acao_mensagem fluxo_acao_mensagem
    ON fluxo_acao_mensagem.id_fluxo = fluxo_acao.id_fluxo
        AND fluxo_acao_mensagem.timestamp = fluxo_acao.timestamp /* Not sure what the Portuguese would be for "created at" or "CreateTimestamp" */
LEFT JOIN
  fluxo_acao_pergunta  fluxo_acao_pergunta 
    ON fluxo_acao_pergunta.id_fluxo = fluxo_acao.id_fluxo
        AND fluxo_acao_pergunta .timestamp = fluxo_acao.timestamp
LEFT JOIN
  fluxo_acao_escolher_fila  fluxo_acao_escolher_fila
    ON fluxo_acao_escolher_fila.id_fluxo = fluxo_acao.id_fluxo
        AND fluxo_acao_escolher_fila.timestamp = fluxo_acao.timestamp

If there is a need for only one of the subtypes (and don't need the column ordem) you can just query the individual table directly.

Licencié sous: CC-BY-SA avec attribution
Non affilié à dba.stackexchange
scroll top