steve.gooberman-hill
2010-Mar-11 14:51 UTC
acts_as_tree test fails : duplicate entry # for key #
Hi, I''ve set up a model as follows class EquipmentPrice < ActiveRecord::Base acts_as_tree :order => "id" ... end which has the following db migration class CreateEquipmentPrices < ActiveRecord::Migration def self.up create_table :equipment_prices do |t| t.integer :id t.string :manufacturer t.string :model t.string :equipment_type t.integer :capacity t.integer :string_size t.string :cost_structure t.string :currency t.decimal :price_each t.decimal :price_bulk t.integer :bulk_quantity t.integer :parent_id t.timestamps end end def self.down drop_table :equipment_prices end end But when I try and run the default test, I get an error message stating ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry ''1'' for key 1: INSERT INTO `equipment_prices` (`id`, `manufacturer`, `model`, `equipment_type`, `capacity`, `string_size`, `cost_structure`, `currency`, `price_each`, `price_bulk`, `bulk_quantity`, `parent_id`, `created_at`, `updated_at`) VALUES (1, NULL, NULL, ''PV_subsystem'', NULL, NULL, NULL, ''USD'', 0.0, 0.0, 1, 0, ''2010-03-11 12:14:05'', ''2010-03-11 12:14:05'') I have read in an earlier post that this is because I have parent_id set as the primary key for the table. But when I look at the mysql table definition I see mysql> describe equipment_prices; +----------------+---------------+------+-----+--------- +----------------+ | Field | Type | Null | Key | Default | Extra | +----------------+---------------+------+-----+--------- +----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | manufacturer | varchar(255) | YES | | NULL | | | model | varchar(255) | YES | | NULL | | | equipment_type | varchar(255) | YES | | NULL | | | capacity | int(11) | YES | | NULL | | | string_size | int(11) | YES | | NULL | | | cost_structure | varchar(255) | YES | | NULL | | | currency | varchar(255) | YES | | NULL | | | price_each | decimal(10,0) | YES | | NULL | | | price_bulk | decimal(10,0) | YES | | NULL | | | bulk_quantity | int(11) | YES | | NULL | | | parent_id | int(11) | YES | | NULL | | | created_at | datetime | YES | | NULL | | | updated_at | datetime | YES | | NULL | | +----------------+---------------+------+-----+--------- +----------------+ 14 rows in set (0.00 sec) which appears to show that id is the primary key, as I would expect. What am I doing wrong here please? Thanks Steve -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
steve.gooberman-hill
2010-Mar-12 12:18 UTC
Re: acts_as_tree test fails : duplicate entry # for key #
resolved - it would appear that MySQL treats an int of 0 = NULL. , so my first fixture (the root of the tree) which had an id of 0 in the fixtures file, was being taken as NULL (stated as not allowed in the table definition), so set to id=1 by MySQL, then causing the 2nd fixture (id=1 in the fixtures file) to appear as a duplicate. On Mar 11, 2:51 pm, "steve.gooberman-hill" <steve.gooberman-h...@power- oasis.com> wrote:> Hi, > I''ve set up a model as follows > > class EquipmentPrice < ActiveRecord::Base > > acts_as_tree :order => "id" > > ... > end > > which has the following db migration > class CreateEquipmentPrices < ActiveRecord::Migration > def self.up > create_table :equipment_prices do |t| > t.integer :id > t.string :manufacturer > t.string :model > t.string :equipment_type > t.integer :capacity > t.integer :string_size > t.string :cost_structure > t.string :currency > t.decimal :price_each > t.decimal :price_bulk > t.integer :bulk_quantity > t.integer :parent_id > > t.timestamps > end > end > > def self.down > drop_table :equipment_prices > end > end > > But when I try and run the default test, I get an error message > stating > ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry ''1'' for > key 1: INSERT INTO `equipment_prices` (`id`, `manufacturer`, `model`, > `equipment_type`, `capacity`, `string_size`, `cost_structure`, > `currency`, `price_each`, `price_bulk`, `bulk_quantity`, `parent_id`, > `created_at`, `updated_at`) VALUES (1, NULL, NULL, ''PV_subsystem'', > NULL, NULL, NULL, ''USD'', 0.0, 0.0, 1, 0, ''2010-03-11 12:14:05'', > ''2010-03-11 12:14:05'') > > I have read in an earlier post that this is because I have parent_id > set as the primary key for the table. But when I look at the mysql > table definition I see > > mysql> describe equipment_prices; > +----------------+---------------+------+-----+--------- > +----------------+ > | Field | Type | Null | Key | Default | > Extra | > +----------------+---------------+------+-----+--------- > +----------------+ > | id | int(11) | NO | PRI | NULL | > auto_increment | > | manufacturer | varchar(255) | YES | | NULL > | | > | model | varchar(255) | YES | | NULL > | | > | equipment_type | varchar(255) | YES | | NULL > | | > | capacity | int(11) | YES | | NULL > | | > | string_size | int(11) | YES | | NULL > | | > | cost_structure | varchar(255) | YES | | NULL > | | > | currency | varchar(255) | YES | | NULL > | | > | price_each | decimal(10,0) | YES | | NULL > | | > | price_bulk | decimal(10,0) | YES | | NULL > | | > | bulk_quantity | int(11) | YES | | NULL > | | > | parent_id | int(11) | YES | | NULL > | | > | created_at | datetime | YES | | NULL > | | > | updated_at | datetime | YES | | NULL > | | > +----------------+---------------+------+-----+--------- > +----------------+ > 14 rows in set (0.00 sec) > > which appears to show that id is the primary key, as I would expect. > > What am I doing wrong here please? > > Thanks > > Steve-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.