max wrote:
1) constraint-name was chosen since that is what is used in most sql dialects for the name of foreign keys, primary key, unique constraints, check constraints etc.
Constraint-name is perfect - don't change a thing! MySQL uses a separate constraint name and foreign key name. The constraint name has to be unique within the database so it's automatically assigned the tablename_ibfk_1 where the "1" is a sequence number incremented for each new constraint. If I drop and recreate a table in development, but merely add a new column in test, then the sequence numbers could be different in different environments and issuing a command to change a constraint in one environment will change a *different* constraint in another environment. This would be really bad.
To avoid this, I use the following template to declare a foreign key with MySQL InnoDB (the default database, MYISAM ignores the "on-delete cascade" clause):
Code:
CREATE TABLE process (
...
company_id BIGINT UNSIGNED NOT NULL,
constraint `process_fk_company` FOREIGN KEY (company_id)
REFERENCES company(id) on delete cascade,
...
) ENGINE=InnoDB;
That way I'm sure that the constraint name (which is what I have to reference in the Hibernate file) will not change. My confusion arose from having to sometimes refer to `process_fk_company` as a constraint (when adding a constraint) and sometimes as a foreign key (when dropping a constraint) in MySQL. That's not Hibernate's issue, though a little note to MySQL users in the reverse-engineering documentation for <foreign-key> would be helpful. The note might be something like:
5.2.4.3. <foreign-key>
...
(1) constraint name:
...
MySQL users: The constraint name is not the same as the foreign key name. To find what constraint name is used for a given foreign key, use the SHOW CREATE TABLE command. Constraint names in mysql look like `tablename_ibfk_1`.If you point me to the proper place to request such a change I can work on getting it added... If you think it's a good idea.
max wrote:
2) We don't try and create a new constraint, we just interpret a non-matching foreign-key as something you then want to have as foreign-key...usecase is for older mysql or systems run by DBA's that thinks foreign keys are bad - here being able to state what constraints is actually there even though the db says they are not becomes usefull.
Oh, OK. So hibernate.reveng thinks I'm trying to declare a foreign key relationship in the mapping file then it finds a foreign key in the database and shows both in the generated file. Makes sense. I can't imagine why someone would want to use a "database" without foreign keys, but I believe it. I've been forced to do worse things in my career.
Thanks so much for reading my posts so carefully and taking the time to clarify these issues.