Skip to content

sql: pg_catalog.pg_attrdef.adbin formats enum values with physical representation instead of logical representation #53687

@rafiss

Description

@rafiss

After enabling enum support, the ActiveRecord test PostgresqlEnumTest#test_enum_defaults is failing.

Here is the code: https://github.com/rails/rails/blob/b221a4dc43368a1b6f00476f7c5f6047c5c7eea4/activerecord/test/cases/adapters/postgresql/enum_test.rb#L41

It fails with

Failure:
PostgresqlEnumTest#test_enum_defaults [/usr/local/lib/ruby/gems/2.7.0/bundler/gems/rails-c8e3aa01c3c9/activerecord/test/cases/adapters/postgresql/enum_test.rb:45]:
Expected: "happy"
  Actual: nil

The SQL is:

> CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy')

> CREATE TABLE postgresql_enums (id INT8 NOT NULL PRIMARY KEY DEFAULT unique_rowid(), current_mood mood)

> ALTER TABLE postgresql_enums ADD COLUMN good_mood mood DEFAULT 'happy'

> SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod, c.collname, col_description(a.attrelid, a.attnum) AS comment FROM pg_attribute AS a LEFT JOIN pg_attrdef AS d ON (a.attrelid = d.adrelid) AND (a.attnum = d.adnum) LEFT JOIN pg_type AS t ON a.atttypid = t.oid LEFT JOIN pg_collation AS c ON (a.attcollation = c.oid) AND (a.attcollation != t.typcollation) WHERE ((a.attrelid = 'postgresql_enums'::REGCLASS) AND (a.attnum > 0)) AND (NOT a.attisdropped) ORDER BY a.attnum;
    attname    |     format_type      |  pg_get_expr   | attnotnull | atttypid | atttypmod | collname | comment
---------------+----------------------+----------------+------------+----------+-----------+----------+----------
  id           | bigint               | unique_rowid() |    true    |       20 |        -1 | NULL     | NULL
  current_mood | unknown (OID=102123) | NULL           |   false    |   102123 |        -1 | NULL     | NULL
  good_mood    | unknown (OID=102123) | b'\xc0'        |   false    |   102123 |        -1 | NULL     | NULL
(3 rows)

>  SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, t.typtype, t.typbasetype FROM pg_type AS t WHERE t.oid::INT8 IN (102123,);
   oid   | typname | typelem | typdelim | typinput | typtype | typbasetype
---------+---------+---------+----------+----------+---------+--------------
  102123 | mood    |       0 | ,        | enum_in  | e       |           0
(1 row)

So it looks like the issue is with pg_get_expr(d.adbin, d.adrelid)

Metadata

Metadata

Assignees

Labels

A-sql-datatypesSQL column types usable in table descriptors.C-bugCode not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions