SchemaExporter: Allow changing the constraint/primary key names in the mappings

Description

Nowadays NH is generating constraints and primary keys using some complex names, as if they were random.
For instance, if I create a <bag> it produces:

alter table User add constraint FKAEA4EA243D347249
foreign key (Column)
references .....

And the primary key name is never present, thus MSSQL uses an internal random name:

create table User
( Id int not null, Name NVARCHAR(80) not null,
primary key (Id))

I would like to be able to change, by using the mappings, those names.
In the first example:
alter table User add constraint FK_Users_Roles

And in the second one:
CONSTRAINT [PK_User_Id] primary key (Id)

This is very useful to change some settings when the SchemeExporter is used, to add a full-text index, change some cascade or fill-factor options, and so on...

The workaround is to make a generation, view the generated script and copy the name being used.

  •  

    •  

      • I'm using NH 2.1.0 GA, MSSQL 2008

Environment

None

Activity

Show:

Oskar Berggren December 13, 2012 at 3:51 PM

I've just reopened NH-1165, but I'm not planning to work on it personally. Maybe you want to have a look at uploading your fixed Table.cs, or even submit a complete pull request.

Zvjezdan Tomičević July 30, 2012 at 7:39 AM

Patch proposed by can produce some problems if one is quoting database object names through conventions. In firebird it can lead to FK_"Order". I took care of this specific case in Table.cs, but maybe it needs a better look.

Zvjezdan Tomičević July 27, 2012 at 11:24 AM

This is great feature. Invaluable for efficient UNQ field infratrstructure, like emails and usernames. Based on this data in exception we can return appropriate error messages. Any possibilities to apply this soonish?

Michael Hallett June 6, 2012 at 8:43 PM

What's the status of this? There was a patch proposed in that wasn't applied. From what I can tell there still isn't a way to control the name of the primary key and unique constraints.

Stephane Verlet January 29, 2010 at 4:24 PM

You can specify the FK name in your mapping using the foreign-key attribute.
It does not work for PK or Unique key see

Details

Assignee

Reporter

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created September 3, 2009 at 1:31 PM
Updated December 13, 2012 at 3:51 PM
Who's Looking?