Control the name of the Primary Key and Unique Constraint Names

Adam's Avatar

Adam

20 Aug, 2010 06:59 PM via web

I'm looking for a way to control how the name of the primary key and unique key constraints are name. Not the properties/columns, but the name of the actual key. I'm using the export option of NH to regenerate my database for my unit tests, but the key names in question are always different. I'd like to be able to use schema comparisson tools to look at changes between what I'm currently working on and what is currently in production, and the key names changing all the time makes it hard.

Here is an example of a unique key name: UQDepartme737584F646F66FB7. The last part of the name is the part that always changes. I'd like to be able to create a convention so that I can specify something like UQ_TableName_ColumnName, but I'm not having any luck in figuring out which convention to use, or if this is even possible.

Thanks

Adam

  1. Support Staff 2 Posted by Paul Batum on 21 Aug, 2010 02:53 PM

    Paul Batum's Avatar

    To the best of my knowledge, NHibernate doesn't support this so we have no
    way to support it in Fluent NH.

    On Sat, Aug 21, 2010 at 5:01 AM, Adam <
    ***@tenderapp.com<tender%***@tenderapp.com>
    > wrote:

  2. Support Staff 3 Posted by James Gregory on 21 Aug, 2010 05:26 PM

    James Gregory's Avatar

    You can specify the unique key name using UniqueKey, but Paul's right about
    the primary key name; that's a limitation of NHibernate, and until they fix
    it we can't do anything about it. I don't think they will be fixing it
    either, last I heard it was some deep architectural issue.

    On Sat, Aug 21, 2010 at 3:55 PM, Paul Batum <
    ***@tenderapp.com<tender%***@tenderapp.com>
    > wrote:

  3. Support Staff 4 Posted by Paul Batum on 22 Aug, 2010 01:23 AM

    Paul Batum's Avatar

    Ahh didn't know that unique keys were possible. Thanks for the correction
    James.

    On Sun, Aug 22, 2010 at 3:28 AM, James Gregory <
    ***@tenderapp.com<tender%***@tenderapp.com>
    > wrote:

  4. 5 Posted by Dave on 03 Sep, 2010 05:09 PM

    Dave's Avatar

    Just to clarify...

    Foreign keys can be given custom names from one of the following methods:

        public void Apply(IOneToManyCollectionInstance instance)
        {
            var keyName = String.Format("FK_{0}_{1}", instance.Member.Name, instance.EntityType.Name);
            instance.Key.ForeignKey(keyName);
        }
    
        public void Apply(IManyToOneInstance instance)
        {
            var keyName = String.Format("FK_{0}_{1}", instance.EntityType.Name, instance.Property.PropertyType.Name);
            instance.ForeignKey(keyName);
        }
    }

    The code about would convert random key constraint names into explicit constraint names, for example:
    FK3903425823405 turns into FK_TableName_ColumnName.

    Is this thread stating that there is no similar mechanism in place to convert:
    PK5349058340093485, to
    PK_TableName

    Sorry for being verbose and/or redundant, but confirmation on this would save a few hours of Google-ing.

  5. Support Staff 6 Posted by James Gregory on 03 Sep, 2010 05:25 PM

    James Gregory's Avatar

    Correct.

    Foreign key constraint names are customisable. Primary key constraint names are not.

    This is an NHibernate limitation, not a Fluent NHibernate.

  6. 7 Posted by PrzemasG on 07 Sep, 2010 06:27 PM

    PrzemasG's Avatar

    James, Adam was talking about UNIQUE keys, not FOREIGN keys...
    As I know, Fluen doesn't allow to name unique keys, am I right?

  7. 8 Posted by dario-g on 18 Nov, 2010 03:32 PM

    dario-g's Avatar

    EntityType is always null (FNH 1.1) :(

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    Insert the next number in this sequence: 10, 11, 12, 13, 14, ??