Skip to content

Docs on Fixing Postgres Default Value Mismatches Is Incorrect #1596

@prikhi

Description

@prikhi

Sometimes you'll write a model file that has a default attr like default=FALSE and get constant no-op migration suggestions because persistent is expecting your model definition to be default=false.

In the Quasi module docs(https://hackage-content.haskell.org/package/persistent-2.16.0.0/docs/Database-Persist-Quasi.html), there is a little blurb about using \d+ in psql to get the proper value to put in default= to make these go away.

But this is not always accurate, apparently psql does a little bit of cleanup when rendering the defaults. The real way is to mimic persistent's query(

getColumns getter def cols = do
let sqlv = T.concat
[ "SELECT "
, "column_name "
, ",is_nullable "
, ",COALESCE(domain_name, udt_name)" -- See DOMAINS below
, ",column_default "
, ",generation_expression "
, ",numeric_precision "
, ",numeric_scale "
, ",character_maximum_length "
, "FROM information_schema.columns "
, "WHERE table_catalog=current_database() "
, "AND table_schema=current_schema() "
, "AND table_name=? "
]
) and pull the value from there:

SELECT 
  column_default
FROM 
  information_schema.columns
WHERE
  table_name='YOUR_TABLE_NAME'
AND
  column_name='YOUR_COLUMN_NAME'

One example is psql showing a default of now() + '120 days'::interval while persistent expects an attr of default="(now() + '120 days'::interval)".

Would be nice to mention this other query in the persistent docs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions