Skip to content

rake db:schema:dump ignores type of non-numeric primary key #14169

Closed
lsimoneau opened this Issue Feb 23, 2014 · 7 comments

7 participants

@lsimoneau

When running rake db:schema:dump on a legacy schema with a table like the following:

CREATE TABLE `hashes` (
  `recordcount` tinyint(4) NOT NULL,
  `timecreated` datetime NOT NULL,
  `sha` varchar(40) NOT NULL DEFAULT '',
  PRIMARY KEY (`sha`),
  KEY `timecreated` (`timecreated`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Rails produces the following schema information:

create_table "hashes", primary_key: "sha", force: true do |t|
  t.integer "recordcount", limit 1, null: false
  t.datetime "timecreated", null: false
end

The string type of the sha field is lost, so when re-creating a database from the schema we wind up with an auto-incrementing integer field instead of a varchar.

Interestingly, on another similar table where the hash key is part of a compound primary key, the information is preserved and the table is re-created correctly.

@arthurnn
Ruby on Rails member

Which version of rails are you seeing this problem?
thanks

@lsimoneau

I've tried 4.0.3 and 4.1.0.rc1

@robin850
Ruby on Rails member

(Here is a gist that reproduces the issue if it can help to debug ; this is still present on master)

@lsimoneau lsimoneau added the stale label May 27, 2014
@rails-bot

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@dankohn
dankohn commented Jul 2, 2014

You can recreate the issue in 4-1-stable and master with this simple migration in a new Rails project, where we're trying to create a non-numeric id column for storing UUID primary keys:

class Example < ActiveRecord::Migration
  def change
    create_table :example_table, id: false do |t|
    t.string :id, limit: 36, null: false, primary_key: true
    end
  end
end

This produces schema.rb:

ActiveRecord::Schema.define(version: 20140702192829) do
  create_table "example_table", id: false, force: true do |t|
    t.string "id", limit: 36
  end
end

It should produce:

ActiveRecord::Schema.define(version: 20140702192829) do
  create_table "example_table", id: false, force: true do |t|
    t.string "id", limit: 36, null: false, primary_key: true
  end
end

I believe the problem is in this if statement which only prints primary_key when the column is not called id.

@sgrif sgrif self-assigned this Jul 2, 2014
@sgrif
Ruby on Rails member
sgrif commented Jul 2, 2014

This is closely related to #15968. My PR for that on master needs a bit more work, it shouldn't be too terribly difficult to come up with a general purpose solution that handles this better.

@sgrif sgrif removed the stale label Jul 2, 2014
@rails-bot rails-bot added the stale label Nov 19, 2014
@rails-bot

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@senny senny added attached PR and removed stale labels Jan 2, 2015
@jochakovsky jochakovsky referenced this issue in studentinsights/studentinsights Apr 10, 2016
Closed

`rake db:setup` is broken #319

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.