I am using Fluent NHibernate in a C# app.The app performs a SchemaUpdate(config).Execute(false, true) on startup.
This works fine with the exception that it re-creates indexes on foreign keys on every start. For example I have a table tblpmr which has the pk keyid, fk script_id, and fk number with an (automatically created) index for each.
Launch the app on an empty DB and the tables/keys/indexes are created fine. Launch the app a second time and in addition to the previously mentioned indexes I have script_id_2 and number_2, run the app a third time and I have indexes script_id_3 and number_3 etc.
Looking at the logs from NHibernate I have the following:
INFO|NHibernate.Dialect.Schema.ITableMetadata|table found: proscript.tblpmr
INFO|NHibernate.Dialect.Schema.ITableMetadata|columns: keyid, is_private, itemref, date, endtext, label, drug, qty, time, doctor, dispensed, cost, dendorse, xml, hostname, script_id, number
INFO|NHibernate.Dialect.Schema.ITableMetadata|foreign keys:
INFO|NHibernate.Dialect.Schema.ITableMetadata|indexes: primary, script_id, number
DEBUG|NHibernate.Tool.hbm2ddl.SchemaUpdate|alter table scriptList add index (patient_id), add constraint FK64CEF4C96630C63A foreign key (patient_id) references tblpatients (number)
DEBUG|NHibernate.Tool.hbm2ddl.SchemaUpdate|alter table scriptList add index (home), add constraint FK64CEF4C9F35E7604 foreign key (home) references tblhomes (hnumber)
DEBUG|NHibernate.Tool.hbm2ddl.SchemaUpdate|alter table scriptList add index (box), add constraint FK64CEF4C97558FD5E foreign key (box) references boxList (boxid)
DEBUG|NHibernate.Tool.hbm2ddl.SchemaUpdate|alter table scriptLog add index (script_id), add constraint FK1C14488375FD47E6 foreign key (script_id) references scriptList (script_id)
DEBUG|NHibernate.Tool.hbm2ddl.SchemaUpdate|alter table tblpatients add index (home), add constraint FKD51976E2F35E7604 foreign key (home) references tblhomes (hnumber)
DEBUG|NHibernate.Tool.hbm2ddl.SchemaUpdate|alter table tblpmr add index (script_id), add constraint FKF9AD750975FD47E6 foreign key (script_id) references scriptList (script_id)
DEBUG|NHibernate.Tool.hbm2ddl.SchemaUpdate|alter table tblpmr add index (number), add constraint FKF9AD750967947BA8 foreign key (number) references tblpatients (number)
It looks like it cannot find the existing foreign keys as part of the table metadata and as part of creating the new foreign keys it also adds an index for it (the duplicate).
I have tried using both MyISAM and InnoDB table types with no difference.
MySQL Server 5.0.51a-24+lenny5
MySQL Connector/NET 6.4.4
Fluent NHibernate 1.3.0.717
NHibernate 3.2.0.4000
I am fairly certain if it can ‘find’ the foreign keys then it will fix the problem but I have no idea why it cannot. I will upgrade mysql-server to whatever the current latest version is to see if that helps.
I found https://forum.hibernate.org/viewtopic.php?t=948998 but am unsure if it is related.
Appears to be an issue with MySQL 5.0 not providing (enough of) the information needed about foreign keys. Working fine now on MySQL 5.1.49-3 (Debian).
If only I had figured that out last week…
Note: The engine must be InnoDB on MySQL 5.1 (or newer I assume) using either MySQL 5.0 or MyISAM (even on 5.1) reproduces the issue.