Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def get_schema_info(klass, header, options = {})
attrs = []
attrs << "default(#{quote(col.default)})" unless col.default.nil?
attrs << "not null" unless col.null
attrs << "primary key" if col.name.to_sym == klass.primary_key.to_sym
attrs << "primary key" if klass.primary_key && col.name.to_sym == klass.primary_key.to_sym

col_type = (col.type || col.sql_type).to_s
if col_type == "decimal"
Expand Down
20 changes: 19 additions & 1 deletion spec/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def mock_class(table_name, primary_key, columns)
options = {
:connection => mock("Conn", :indexes => []),
:table_name => table_name,
:primary_key => primary_key.to_s,
:primary_key => primary_key && primary_key.to_s,
:column_names => columns.map { |col| col.name.to_s },
:columns => columns
}
Expand Down Expand Up @@ -52,6 +52,24 @@ def mock_column(name, type, options={})
# name :string(50) not null
#

EOS
end

it "should get schema info even if the primary key is not set" do
klass = mock_class(:users, nil, [
mock_column(:id, :integer),
mock_column(:name, :string, :limit => 50)
])

AnnotateModels.get_schema_info(klass, "Schema Info").should eql(<<-EOS)
# Schema Info
#
# Table name: users
#
# id :integer not null
# name :string(50) not null
#

EOS
end

Expand Down