Greetings railsers -
I’m trying to add to a collection through HABTM, but the sql
insert is trying to insert a PK rather than letting mysql produce the
auto_increment’ed PK.
@medication_dose holds a validated, saved model
@medication_dose.medication_frequencies << MedicationFrequency.find
(:all)
The above bails with,
Mysql::Error: #23000Duplicate entry '1' for key 1: INSERT INTO
medication_dose_frequencies (medication_frequency_id
, id
,
medication_dose_id
) VALUES (1, 1, 354)
The problem is that the primary key (‘id’) is duplicated. This is
happening because the sql generated by rails is not utilizing is not
using the auto_increment (verified as a mysql insert will work manually)
I suspect that HABTM is grabbing the id column from
MedicationFrequency and using it in the generation for the sql insert.
Is this normal behavior? Perhaps I’m adding to the collection
incorrectly?
If this looks like a but, is their a long winded way to express
this functionality?
Thanx for any advice.
cheers,
Jodi
Rails 1.1.2
ruby 1.8.4 (2005-12-24) [i686-darwin8.6.1]
activerecord (1.14.2)
mysql 4.1.18
class MedicationDose < ActiveRecord::Base
has_and_belongs_to_many :medication_frequencies, :join_table
=> ‘medication_dose_frequencies’
end
class MedicationDoseFrequency < ActiveRecord::Base
belongs_to :medication_dose
belongs_to :medication_frequency
end
class MedicationFrequency < ActiveRecord::Base
has_many :medication_dose_frequencies
end
DDL
CREATE TABLE medication_doses
(
id
int(11) NOT NULL auto_increment,
medication_id
int(11) default NULL,
amount
varchar(20) default NULL,
kind
int(11) default NULL,
PRIMARY KEY (id
)
)
CREATE TABLE medication_dose_frequencies
(
id
int(11) NOT NULL auto_increment,
medication_frequency_id
int(11) default NULL,
medication_dose_id
int(11) default NULL,
PRIMARY KEY (id
)
)
CREATE TABLE medication_frequencies
(
id
int(11) NOT NULL auto_increment,
name
varchar(10) default NULL,
kind
int(11) default NULL,
sort
int(11) default NULL,
PRIMARY KEY (id
)
)