Search code examples
sqlpostgresqluniontypeormunion-all

selecting enum columns in union all postgres


i have problem figuring out how to set up my query. what i want to do select two tables without relations in one query. after researching i've found out about union all but the problem is in some cases i have type or status columns which is enum and i'm getting error while i'm trying to select them:

ERROR: UNION could not convert type money_transfer_type_enum to invoice_type_enum

i think it's not the best practice to select two totally different tables in a query but for some reasons i need it so another question is: if union all is not going to help me handling this is there anyway else? thx in advance


Solution

  • Not sure what the types of the columns are, but you can simply cast both columns to the same type, something like

    SELECT money_transfer_type_enum::VARCHAR
    FROM table1
    
    UNION ALL
    
    SELECT invoice_type_enum::VARCHAR
    FROM table2
    

    Varchar is a catch all for this type of incompatibility, but it comes with some costs, tho. You might wanna see if casting both columns to INTEGER might solve the issue as well