ActiveRecord missing some support for PostgreSQL ENUMs #7814

Closed
joevandyk opened this Issue Oct 1, 2012 · 14 comments

Projects

None yet

8 participants

@joevandyk
Contributor

Bugs I've noticed:

  • If a enum type contains the phrase 'int' in it (i.e. if the type is named "certainty"), AR thinks it's an integer.
  • The type method for the column is nil, I think it should be string?
  • Defaults for columns of an enum type don't work

Failing tests for these:

For 3.2 stable: joevandyk@4374e11

For master: joevandyk@3cf3ea0

https://github.com/RISCfuture/enum_type/blob/master/lib/enum_type/extensions.rb is some code to set the default value for enum columns.

@joevandyk
Contributor

Here is a blog post about some of the bugs above, he dug into the AR code a bit: http://www.jongyulin.com/2012/08/rails-digging-into-activerecord-and-postgresql-enums/

@steveklabnik
Member
@senny
Member
senny commented Oct 2, 2012

I opened a PR for a refactoring of he type detection for PG. I think I can fix the first and second error there: #7575

@JonRowe
Contributor
JonRowe commented Apr 21, 2013

Did you ever come up with an alternate solution after deciding #7575 wasn't viable @senny?

@senny
Member
senny commented Apr 22, 2013

@JonRowe I did not. Thanks for bringing this one up again. I'll take a look in the upcoming days.

@senny
Member
senny commented Apr 28, 2013

@JonRowe I don't have enough time at the moment to dig deeper into this one. Feel free to ping me if you want to work on something.

@JonRowe
Contributor
JonRowe commented Apr 30, 2013

Alas my (lack of) knowledge about the internals of ActiveRecord mean this is probably not going to be attempted by me, I was just helping triage issues :/

@eljojo
eljojo commented May 15, 2013

is there any update for this?

In 4.0.0.beta1 I was getting Unkown OID errors, while in 4.0.0.rc1 I get some kind of AR error that I cannot really explain.

@MSch
Contributor
MSch commented Jun 23, 2013

Experiencing the same error. Could we still fix this in 4.0 or is that out of the scope for the release?

@senny
Member
senny commented Jun 23, 2013

@MSch I don't think it's gonna happen before 4.0. It's on my todo-list but it will take some time to come around to it. If anyone wants to take a stab at it, feel free to ping me.

@steveklabnik
Member

It will not be in 4.0.0, because it's not a regression.

@croaky croaky referenced this issue in DockYard/postgres_ext Sep 11, 2013
Open

support enum type fields #89

@gmanley
gmanley commented Sep 29, 2013

@eljojo @MSch Not sure if you guys found a solution but if you're just looking to get rid of the warnings you need add a custom data type. I wrote up a small post with more info: http://gray.fm/2013/09/17/unknown-oid-with-rails-and-postgresql. In your case just replace the citext in the example code with enum.

@rsutphin
Contributor

A part of this issue is a regression vs. 3.2.16:

The type method for the column is nil, I think it should be string?

I wrote a reproduction script showing a simple model w/ENUM attribute that works in 3.2.16 and does not work in 4.0.0, 4.0.1, or 4.0.2.


More detail:

Returning nil for the column type breaks PostgreSQLAdapter::Quoting#quote:

undefined method `to_sym' for nil:NilClass
{{ GEM_HOME }}/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:671:in `type_to_sql'
{{ GEM_HOME }}/gems/activerecord-4.0.2/lib/active_record/connection_adapters/postgresql/schema_statements.rb:467:in `type_to_sql'
{{ GEM_HOME }}/gems/activerecord-4.0.2/lib/active_record/connection_adapters/postgresql/quoting.rb:21:in `quote'
{{ GEM_HOME }}/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:296:in `block in insert_fixture'

In real life, I'm seeing this exception during fixture loading after upgrading to 4.0.2 in a test suite that works under 3.2.16. (I've verified that the particular value it's trying to quote is for one of the enum-typed columns.)

The first place it happens in this app is during fixture insertion, but since quoting is a generic facility I'd expect it to break non-fixture-using apps also (cf. #10420).

@rsutphin
Contributor

I've traced through this issue some more. While the exception I noted is because the column type is nil for enums, having the column type nil for enums is not a change vs. Rails 3.2. The change is that, as of 4b4c8bd, #quote assumes that the column type is not nil.

@senny senny added the PostgreSQL label Feb 4, 2014
@senny senny added this to the 4.2.0 milestone Mar 26, 2014
@senny senny added a commit to senny/rails that referenced this issue Mar 26, 2014
@senny senny support default values for PG enum columns. #7814. 9c4c2f4
@senny senny added a commit to senny/rails that referenced this issue Mar 28, 2014
@senny senny PostgreSQL determine `Column#type` through corresponding OID. #7814
I ran the whole test suite and compared the old to the new types.
Following is the list of types that did change with this patch:

```
DIFFERENT TYPE FOR mood: NEW: enum, BEFORE:
DIFFERENT TYPE FOR floatrange: NEW: floatrange, BEFORE: float
```

The `floatrange` is a custom type. The old type `float` was simply a coincidence
form the name `floatrange` and our type-guessing.
fd30771
@senny senny added a commit to senny/rails that referenced this issue Mar 28, 2014
@senny senny PostgreSQL determine `Column#type` through corresponding OID. #7814
I ran the whole test suite and compared the old to the new types.
Following is the list of types that did change with this patch:

```
DIFFERENT TYPE FOR mood: NEW: enum, BEFORE:
DIFFERENT TYPE FOR floatrange: NEW: floatrange, BEFORE: float
```

The `floatrange` is a custom type. The old type `float` was simply a coincidence
form the name `floatrange` and our type-guessing.
2f83c07
@senny senny added a commit to senny/rails that referenced this issue Mar 28, 2014
@senny senny PostgreSQL determine `Column#type` through corresponding OID. #7814
I ran the whole test suite and compared the old to the new types.
Following is the list of types that did change with this patch:

```
DIFFERENT TYPE FOR mood: NEW: enum, BEFORE:
DIFFERENT TYPE FOR floatrange: NEW: floatrange, BEFORE: float
```

The `floatrange` is a custom type. The old type `float` was simply a coincidence
form the name `floatrange` and our type-guessing.
e71a97a
@senny senny added a commit to senny/rails that referenced this issue Mar 31, 2014
@senny senny PostgreSQL determine `Column#type` through corresponding OID. #7814
I ran the whole test suite and compared the old to the new types.
Following is the list of types that did change with this patch:

```
DIFFERENT TYPE FOR mood: NEW: enum, BEFORE:
DIFFERENT TYPE FOR floatrange: NEW: floatrange, BEFORE: float
```

The `floatrange` is a custom type. The old type `float` was simply a coincidence
form the name `floatrange` and our type-guessing.
446fc17
@senny senny added a commit to senny/rails that referenced this issue Apr 1, 2014
@senny senny PostgreSQL determine `Column#type` through corresponding OID. #7814
I ran the whole test suite and compared the old to the new types.
Following is the list of types that did change with this patch:

```
DIFFERENT TYPE FOR mood: NEW: enum, BEFORE:
DIFFERENT TYPE FOR floatrange: NEW: floatrange, BEFORE: float
```

The `floatrange` is a custom type. The old type `float` was simply a coincidence
form the name `floatrange` and our type-guessing.
4d344bb
@tgxworld tgxworld added a commit to tgxworld/rails that referenced this issue Apr 3, 2014
@senny @tgxworld senny + tgxworld PostgreSQL determine `Column#type` through corresponding OID. #7814
I ran the whole test suite and compared the old to the new types.
Following is the list of types that did change with this patch:

```
DIFFERENT TYPE FOR mood: NEW: enum, BEFORE:
DIFFERENT TYPE FOR floatrange: NEW: floatrange, BEFORE: float
```

The `floatrange` is a custom type. The old type `float` was simply a coincidence
form the name `floatrange` and our type-guessing.
89811a6
@jefflai2 jefflai2 added a commit to jefflai2/rails that referenced this issue Apr 24, 2014
@jefflai2 jefflai2 PostgreSQL Timestamps always map to `:datetime`.
The PG Adapter should use `:datetime` consistently instead of mapping
mispellings to `:timestamp`.

See #7814
22d652a
@jefflai2 jefflai2 added a commit to jefflai2/rails that referenced this issue Apr 24, 2014
@jefflai2 jefflai2 PostgreSQL Timestamps always map to `:datetime`.
The PG Adapter should use `:datetime` consistently instead of mapping
mispellings to `:timestamp`.

See #7814
e9d95ca
@senny senny added a commit that closed this issue May 30, 2014
@senny senny pg, support default values for enum types. Closes #7814.
This is an intermediate solution. It is related to the refactoring @sgrif
is making and will change in the future.
b9eeb03
@senny senny closed this in b9eeb03 May 30, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment