On Feb 21, 2:09 pm, cult hero <[email protected]> wrote: > I dunno if I've found a bug or... what. > > I have the following migration: > > create_table :assets do > primary_key :id > foreign_key :type_id, :types, :null => false > String :name, :length => 255, :null => false > String :slug, :length => 255, :null => false > DateTime :date_created, :null => false > DateTime :date_modified, :null => false > Fixnum :precedence, :default => 1, :null => false > text :description, :null => false > text :notes, :null => false > end > > The types table is already created and working fine. Here's the SQL > this migration generates: > > CREATE TABLE `assets` (`id` integer PRIMARY KEY AUTO_INCREMENT, > `type_id` integer NOT NULL, FOREIGN KEY (`type_id`) REFERENCES > `types`, `name` varchar(255) NOT NULL, `slug` varchar(255) NOT NULL, > `date_created` datetime NOT NULL, `date_modified` datetime NOT NULL, > `precedence` integer NOT NULL DEFAULT 1, `description` text NOT NULL, > `notes` text NOT NULL) ENGINE=InnoDB > > Unfortunately, that produces the following error: > > ERROR 1005 (HY000): Can't create table './database/assets.frm' (errno: > 150) > > The syntax should be: > > CREATE TABLE `assets` (`id` integer PRIMARY KEY AUTO_INCREMENT, > `type_id` integer NOT NULL, FOREIGN KEY (`type_id`) REFERENCES `types` > (`id`), `name` varchar(255) NOT NULL, `slug` varchar(255) NOT NULL, > `date_created` datetime NOT NULL, `date_modified` datetime NOT NULL, > `precedence` integer NOT NULL DEFAULT 1, `description` text NOT NULL, > `notes` text NOT NULL) ENGINE=InnoDB > > The difference is hard to notice, but "REFERENCES `types`" becomes > "REFERENCES `types` (`id`)". Am I doing something wrong in my syntax > or is this a bug?
This isn't a bug. The SQL standard specifies that you can use a table in REFERENCES without specifying column(s), and it will assume you are referencing the primary key of that table. If MySQL doesn't allow that, then you need to use the :key option to foreign_key to specify the primary key. If you want to send in a patch to the shared MySQL adapter to lookup the primary key of the table if a :key option is not specified, I'd consider it. Jeremy --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sequel-talk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sequel-talk?hl=en -~----------~----~----~----~------~----~------~--~---
