This project is archived and is in readonly mode.

#496 ✓duplicate
datanoise

create_table with explicit :primary_key attribute

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

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

Comments and changes to this ticket

  • James Le Cuirot

    James Le Cuirot August 15th, 2008 @ 11:04 PM

    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).

  • Tony Pitale

    Tony Pitale August 18th, 2008 @ 09:31 PM

    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.

  • Tarmo Tänav

    Tarmo Tänav September 18th, 2008 @ 05:07 AM

    • State changed from “new” to “duplicate”
    • Tag changed from activerecord to activerecord

    #876 seems to try to address this

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages