Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

create_table with explicit :primary_key attribute

#496

SchemaStatements#create_table generates incorrect statement when :primary_key is specified. It works correctly in Rails 1.2.6

>> c.create_table 'testings', :primary_key => 'code' do |t|
?>   t.string 'code', :limit => 2
>>   t.string 'name', :limit => 10
>> end
ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(2), `name` varchar(10) DEFAULT NULL NULL) ENGINE=InnoDB' at line 1: CREATE TABLE `testings` (`code` int(11) DEFAULT NULL auto_increment PRIMARY KEY(2), `name` varchar(10) DEFAULT NULL NULL) ENGINE=InnoDB
	from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:147:in `log'
	from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/mysql_adapter.rb:299:in `execute'
	from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:108:in `create_table'
	from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/mysql_adapter.rb:434:in `create_table'
	from (irb):4

Reported by datanoise · June 27th, 2008 @ 03:19 AM

State: duplicate
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. James Le Cuirot
    James Le Cuirot

    What you've tried to do doesn't quite make sense. You want your primary key to be a string? I'm not sure if Rails supports this. In any case, it isn't possible to do this with migrations. The :primary_key option for create_table is simply for giving a different name for the column. You're not supposed to give an alternative definition for that column as well. This does highlight the issue I was going to raise when I stumbled across this ticket though. It is currently not possible to choose a limit for the primary key either. It is hardcoded to int(11).

    August 15th, 2008 @ 11:04 PM

  2. Tony Pitale
    Tony Pitale

    It is not currently possible to use string keys with AR (and I doubt it ever will be). Ran into that a few months ago with a legacy db.

    August 18th, 2008 @ 09:31 PM

  3. Tarmo Tänav
    Tarmo Tänav
    • State changed from new to duplicate
    • Tag changed from activerecord to activerecord

    #876 seems to try to address this

    September 18th, 2008 @ 05:07 AM