-
Notifications
You must be signed in to change notification settings - Fork 598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Column defaults are displayed as String unexpectedly #762
Comments
@pocke interesting, thank you for a detailed report and investigation 👍. Does look like it also happens for sqlite per integration specs. |
@pocke if you have an instance of postgresql could you share what output you get from running these commands? klass = SomeModelName # Replace this
pp Hash[klass.columns.map.with_index { |m, i| [i, m] }]
klass.column_defaults irb(main):021:0> pp Hash[klass.columns.map.with_index { |m, i| [i, m] }]
{0=>
#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e9f90
@collation=nil,
@comment=nil,
@default=nil,
@default_function=nil,
@name="id",
@null=false,
@sql_type_metadata=
#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972f0f20
@limit=nil,
@precision=nil,
@scale=nil,
@sql_type="integer",
@type=:integer>,
@table_name="tasks">,
1=>
#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e9888
@collation=nil,
@comment=nil,
@default=nil,
@default_function=nil,
@name="content",
@null=true,
@sql_type_metadata=
#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972e98d8
@limit=nil,
@precision=nil,
@scale=nil,
@sql_type="varchar",
@type=:string>,
@table_name="tasks">,
2=>
#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e9748
@collation=nil,
@comment=nil,
@default="0",
@default_function=nil,
@name="count",
@null=true,
@sql_type_metadata=
#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972e9798
@limit=nil,
@precision=nil,
@scale=nil,
@sql_type="integer",
@type=:integer>,
@table_name="tasks">,
3=>
#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e9090
@collation=nil,
@comment=nil,
@default="0",
@default_function=nil,
@name="status",
@null=true,
@sql_type_metadata=
#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972e90e0
@limit=nil,
@precision=nil,
@scale=nil,
@sql_type="boolean",
@type=:boolean>,
@table_name="tasks">,
4=>
#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e89d8
@collation=nil,
@comment=nil,
@default=nil,
@default_function=nil,
@name="created_at",
@null=false,
@sql_type_metadata=
#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972e8a28
@limit=nil,
@precision=nil,
@scale=nil,
@sql_type="datetime",
@type=:datetime>,
@table_name="tasks">,
5=>
#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e8898
@collation=nil,
@comment=nil,
@default=nil,
@default_function=nil,
@name="updated_at",
@null=false,
@sql_type_metadata=
#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972e88e8
@limit=nil,
@precision=nil,
@scale=nil,
@sql_type="datetime",
@type=:datetime>,
@table_name="tasks">,
6=>
#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e8758
@collation=nil,
@comment=nil,
@default="0",
@default_function=nil,
@name="type_field",
@null=true,
@sql_type_metadata=
#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972e87a8
@limit=nil,
@precision=nil,
@scale=nil,
@sql_type="integer",
@type=:integer>,
@table_name="tasks">}
=> {0=>#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e9f90 @name="id", @table_name="tasks", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972f0f20 @sql_type="integer", @type=:integer, @limit=nil, @precision=nil, @scale=nil>, @null=false, @default=nil, @default_function=nil, @collation=nil, @comment=nil>, 1=>#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e9888 @name="content", @table_name="tasks", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972e98d8 @sql_type="varchar", @type=:string, @limit=nil, @precision=nil, @scale=nil>, @null=true, @default=nil, @default_function=nil, @collation=nil, @comment=nil>, 2=>#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e9748 @name="count", @table_name="tasks", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972e9798 @sql_type="integer", @type=:integer, @limit=nil, @precision=nil, @scale=nil>, @null=true, @default="0", @default_function=nil, @collation=nil, @comment=nil>, 3=>#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e9090 @name="status", @table_name="tasks", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972e90e0 @sql_type="boolean", @type=:boolean, @limit=nil, @precision=nil, @scale=nil>, @null=true, @default="0", @default_function=nil, @collation=nil, @comment=nil>, 4=>#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e89d8 @name="created_at", @table_name="tasks", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972e8a28 @sql_type="datetime", @type=:datetime, @limit=nil, @precision=nil, @scale=nil>, @null=false, @default=nil, @default_function=nil, @collation=nil, @comment=nil>, 5=>#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e8898 @name="updated_at", @table_name="tasks", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972e88e8 @sql_type="datetime", @type=:datetime, @limit=nil, @precision=nil, @scale=nil>, @null=false, @default=nil, @default_function=nil, @collation=nil, @comment=nil>, 6=>#<ActiveRecord::ConnectionAdapters::Column:0x00007f99972e8758 @name="type_field", @table_name="tasks", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007f99972e87a8 @sql_type="integer", @type=:integer, @limit=nil, @precision=nil, @scale=nil>, @null=true, @default="0", @default_function=nil, @collation=nil, @comment=nil>}
irb(main):022:0>
irb(main):023:0> klass.column_defaults
=> {"id"=>nil, "content"=>nil, "count"=>0, "status"=>false, "created_at"=>nil, "updated_at"=>nil, "type_field"=>"inactive"} |
My example: # Task.rb
class Task < ApplicationRecord
enum type_field: { inactive: 0, active: 1, archived: 2 }
end
# schema.rb
ActiveRecord::Schema.define(version: 2020_02_25_152254) do
create_table "tasks", force: :cascade do |t|
t.string "content"
t.integer "count", default: 0
t.boolean "status", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "type_field", default: 0
end
end |
Thanks for your investigation!
Sure! The following is the output with the same tasks table.
|
Also seeing it, and we're also using PG. Let me know if there's anything I can help/test :) |
Just gonna revert #677 for now and make a feature request for doing enums. |
@gingerlime @pocke can yall test this branch? #768 |
v3.1.0 has a bug which rewrites default annotations with erroneous quotes. ctran/annotate_models#762 This reverts commit dd4a04e.
This should be fixed in v3.1.1 which just got released. |
Thanks for releasing it! I've confirmed the problem has been fixed in v3.1.1. |
Same here! Thank you 👍 |
Great to hear -- I apologize for the delay, life has been a bit craazy recently. Going to close this out. Feel free to reopen. |
It was reported in ctran/annotate_models#762 that column defaults were broken. This reverts changes made in #677 to restore the expected behavior of column defaults. For the time being columns with associated enums won't be working.
Since v3.1.0, we got incorrect default values by annotate_models.
It is a part of our diff between v3.0.3 and v3.1.0.
And a part of schema.rb for the columns definitions.
I guess it depends on RDBMS because the spec is not wrong.
We use PostgreSQL.
Probably this pull request is related. #677
Commands
And I investigated the
ActiveRecord::ConnectionAdapters::PostgreSQLColumn#default
returned value, which is used by #677, andklass.column_defaults[column.name]
, which was used previously.Version
The text was updated successfully, but these errors were encountered: