I am experimenting with two MySQL tables, the first with a natural
primary keys and the second with composite natural primary keys and a
part of the CPK as foreign key to the first table. Here my current
ruby code:
require 'dm-core'
require 'dm-constraints'
DataMapper::Logger.new( $stdout, :debug )
DataMapper.setup( :default, 'mysql://....')
...
class Appl
  include DataMapper::Resource
  property  :id,    String, :key => true
  has n, :tables
end # Appl
class Table
  include DataMapper::Resource
  property  :app,   String, :key => true, :index => true
  property  :id,     String, :key => true
  belongs_to  :appl, :child_key => [:app]
end # Table
DataMapper.auto_migrate!
...
As you can see, I try to convince DataMapper to use the property :app
from Table as foreign key to Appl
Here is the log from the auto_migrate! execution:
...
~ (0.003736) CREATE TABLE `appls` (`id` VARCHAR(50) NOT NULL, PRIMARY
KEY(`id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci
 ~ (0.000195) SHOW TABLES LIKE 'tables'
 ~ (0.011068) CREATE TABLE `tables` (`app` VARCHAR(50) NOT NULL, `id`
VARCHAR(50) NOT NULL, `appl_id` VARCHAR(50) NOT NULL, PRIMARY
KEY(`app`, `id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE
utf8_general_ci
 ~ (0.018187) CREATE INDEX `index_tables_app` ON `tables` (`app`)
 ~ (0.018503) CREATE INDEX `index_tables_appl` ON `tables` (`appl_id`)
 ~ (0.000628) SELECT COUNT(*) FROM
"information_schema"."table_constraints" WHERE "constraint_type" =
'FOREIGN KEY' AND "table_schema" = 'ddtest' AND "table_name" =
'tables' AND "constraint_name" = 'tables_appl_fk'
 ~ (0.021269) ALTER TABLE `tables` ADD CONSTRAINT `tables_appl_fk`
FOREIGN KEY (`app`) REFERENCES `appls` (`id`) ON DELETE NO ACTION ON
UPDATE NO ACTION
...

As you can see, the index for tables(app) gets generated as requested
in the model, and from later tests I can proof, that this is used for
the foreign key constraint.

But DataMapper generates automatically another index tables(appl_id) -
in the log above at 0.018503. As far as I understood it, this is to
support the one-to-many association. Anyway, it will have the same
info as the first index from 0.018187.

So my question: how should I declare the models to avoid this
additional index?
Kittekat

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" 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/datamapper?hl=en.

Reply via email to