jhartwell / Plsm

Elixir mix task to generate Ecto models from already existing tables
MIT License
265 stars 54 forks source link

Unique contraints cause duplicate fields when using postgres #104

Open jhartwell opened 4 years ago

jhartwell commented 4 years ago

If you have a column that is a primary key and also has a unique constraint that column will be exported twice in the output. We don't need to worry about the unique constraint so it will need to be ignored

tomjoro commented 3 years ago

I have traced the issue and have a fix for it (I can make a pull request). The issue is caused by this left join:

        LEFT JOIN pg_index i ON
            (pgc.oid = i.indrelid AND i.indkey[0] = a.attnum)

This causes two effects:

  1. Compound primary keys don't work
  2. If the field is repeated in another index you will get a row for both primary_key: true and primary_key: false (which now generates the field twice).

The solution I have is just to query the primary key(s) in another query otherwise the SQL gets too complicated.