nhibernate-core icon indicating copy to clipboard operation
nhibernate-core copied to clipboard

NH-3427 - Configuration.GenerateSchemaCreationScript doesn't quote column names no matter hbm2ddl.keywords

Open nhibernate-bot opened this issue 8 years ago • 0 comments

Antonio Anzivino created an issue:

I have found a potential bug when exporting the DDL generated by a Fluent NHibernate mapping.

First, FNH has a potential issue when mapping collections to lists (i.e. AsList() mapping) because it hardcodes the Index column name to order elements in the list.

When NHibernate is configured with hbm2ddl.keywords = auto-quote AND hbm2ddl.auto != none | validate then the session factory automatically runs a SQL script with Index columns correctly quoted.

The bug happens when I want to export the DDL using Configuration.GenerateSchemaCreationScript.

Provided the following model (which can be replaced with a generic Entity class with a parent-child relationship)

public class ProductCategory
{
    public virtual long Id { get; set; }

    public virtual string Name { get; set; }

    public virtual ProductCategory Parent { get; set; }

    public virtual IList<ProductCategory> Children { get; set; }
}

and provided the following FNH mapping:

internal class ProductCategoryMapping : ClassMap<ProductCategory>
{
    public ProductCategoryMapping()
    {
        Id(x => x.Id).GeneratedBy.Increment();

        Map(x => x.Name).Not.Nullable();
        References(x => x.Parent)
            .Cascade.None();

        HasMany(x => x.Children)
            .LazyLoad()
            .Cascade.None()
            .Inverse()
            .AsList();
    }
}

Using the GenerateSchemaCreationScript with hbm2ddl.keywords = auto-quote and hbm2ddl.auto = none produces the following incorrect statement with MySQL 5 dialect

create table `ProductCategory` (
   Id BIGINT not null,
   Name VARCHAR(255) not null,
   Parent_id BIGINT,
   Index INTEGER,
   primary key (Id))

Unquoted index is wrong in MySQL syntax.

The problem only occurs when I use GenerateSchemaCreationScript and not if I use GenerateSchemaUpdateScript with an active connection.

The Index column name is hardcoded in FNH and cannot be changed.

I think the above sample code should suffice. Small test case on the way

nhibernate-bot avatar Oct 12 '17 23:10 nhibernate-bot