Dumping schema

All,

It looks like rake db:schema:dump does not dump primary keys.
I have some legacy table that don’t follow the convention ie pk = id
and
none of the primary keys for these tables are getting dumped…

Seems strange as this should be readily available from the db. I am
using
MySQL. Is anybody else seeing that ?

Incidentally how does one specify a primary key using migrations ?

Thanks to all

De Railed <[email protected]…> writes:

MySQL. Is anybody else seeing that ?

Incidentally how does one specify a primary key using migrations ?

Thanks to all

Migration:
class CreateMyTables < ActiveRecord::Migration
def self.up
create_table :my_tables, :primary_key => :guid do |t|
t.column :name, :string
end
change_column :my_tables, :guid, :string
end

def self.down
drop_table :my_tables
end
end

Results:
shiny:~/rails/keys damon$ rake migrate
(in /Users/damon/rails/keys)
== CreateMyTables: migrating

– create_table(:my_tables, {:primary_key=>:guid})
-> 0.3686s
– change_column(:my_tables, :guid, :string)
-> 0.6069s
== CreateMyTables: migrated (0.9870s)

shiny:~/rails/keys damon$ cat db/schema.rb

This file is autogenerated. Instead of editing this file, please use

the

migrations feature of ActiveRecord to incrementally modify your

database, and

then regenerate this schema definition.

ActiveRecord::Schema.define(:version => 1) do

create_table “my_tables”, :id => false, :force => true do |t|
t.column “guid”, :string, :default => “”, :null => false
t.column “name”, :string
end

end
shiny:~/rails/keys damon$ rake db:structure:dump
(in /Users/damon/rails/keys)
shiny:~/rails/keys damon$ cat db/development_structure.sql
CREATE TABLE my_tables (
guid varchar(255) NOT NULL default ‘’,
name varchar(255) default NULL,
PRIMARY KEY (guid)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE schema_info (
version int(11) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Notice the difference between the two dumps. Structure dump has the
correct
primary key. Schema dump does not. Essentially, you’re being punished
for not
using ‘id’. :slight_smile: What’s wrong with you? :wink:

Actually, the schema dumper (schema_dumper.rb) has support for non-id
primary
keys with these lines:


if @connection.respond_to?(:pk_and_sequence_for)
pk, pk_seq = @connection.pk_and_sequence_for(table)
end
pk ||= ‘id’

      tbl.print "  create_table #{table.inspect}"
      if columns.detect { |c| c.name == pk }
        if pk != 'id'
          tbl.print %Q(, :primary_key => "#{pk}")
        end
      else
        tbl.print ", :id => false"

However, the mysql adapter does not have this method, so ‘:id => false’
winds up
in the dump. It seems like you could patch the mysql adapter to support
that
method and it should work. If you get something that works, you could
submit it
as a patch.

More details about using migrations can be found in my RailsConf
presentation at
http://damonclinkscales.com/slides/agilemigrations-railsconf.pdf

Best,
-damon

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs