nhibernate / nhibernate-core

NHibernate Object Relational Mapper
https://nhibernate.info
GNU Lesser General Public License v2.1
2.12k stars 924 forks source link

NH-3733 - MySQL Schema Update Doesn't Work #1331

Open nhibernate-bot opened 6 years ago

nhibernate-bot commented 6 years ago

ergatech created an issue:

I can update schema use SchemaUpdate on SQLSever 2012 but I can not update schema on MySQL with same code. I get Exception "'CONSTRAINT_NAME' colum .....".

I need schema name on mapping object. Example in attachment.


Oskar Berggren added a comment — : What is the error? I think we can at least expect the full error message in the issue report. What is the claimed bug (what does NH do wrong)? My time is limited and I don't want to spend it having to guess what you want.


ergatech added a comment — : Dear Oscar;

I created a test case for you and this case in attachment. But no problem for me I can write detail in here.

NH Usage :

var config = Fluently.Configure()
                .Database(MySQLConfiguration.Standard.ConnectionString("Server=127.0.0.1;Port=3306;Uid=root;Pwd =xxxx")
                .Dialect<MySQL5Dialect>()
                .Driver<MySqlDataDriver>()
                .ShowSql()
                .FormatSql())
                .Mappings(x=>x.FluentMappings.AddFromAssemblyOf<Contact>())
                .BuildConfiguration();

            var updater = new SchemaUpdate(config);
            updater.Execute(true,true);

I have two database (Master and Detail). I mapping objects with Schema (FluentNhibernate with ClassMap class)

public class ContactMap : ClassMap<Contact>
{
    public ContactMap()
    {
        Table("test_tb_contact");
        Schema("nhibernate_test");
        Id(x => x.ID, "ID").GeneratedBy.Identity();
        Map(x => x.FirstName, "FirstName");
        Map(x => x.LastName, "LastName");
        Map(x => x.Gender, "Gender");
        Map(x => x.d1, "d1");
        Map(x => x.d2, "d2");
        Map(x => x.d3, "d3");
    }
}

My generated CreateSQL

create table nhibernate_test.test_tb_contact (
       ID BIGINT NOT NULL AUTO_INCREMENT,
       FirstName VARCHAR(255),
       LastName VARCHAR(255),
       Gender VARCHAR(255),
       d1 VARCHAR(255),
       d2 VARCHAR(255),
       d3 VARCHAR(255),
       primary key (ID)
    )

After I added a new field d4

My Mapping :

public class ContactMap : ClassMap<Contact>
{
    public ContactMap()
    {
        Table("test_tb_contact");
        Schema("nhibernate_test");
        Id(x => x.ID, "ID").GeneratedBy.Identity();
        Map(x => x.FirstName, "FirstName");
        Map(x => x.LastName, "LastName");
        Map(x => x.Gender, "Gender");
        Map(x => x.d1, "d1");
        Map(x => x.d2, "d2");
        Map(x => x.d3, "d3");
        Map(x => x.d4, "d4");
    }
}

NHibernate not update schema and Console write "'CONSTRAINT_NAME' sütunu, tablosuna ait değil." this message is turkish. Maybe english translate "'CONSTRAINT_NAME' column, does not belong this table." but Nhibernate must do generate ALTER Script.

If you run example in attachment you can see this error.

hazzik commented 6 years ago

I believe this could be related to the "Turkish i" problem.

baristutal commented 6 years ago
public string[] ChangeLog()
        {
            try
            {
                var cfg = Config();

                var dialect = NHibernate.Dialect.Dialect.GetDialect(cfg.Properties);

                string[] schemaUpdateScript;
                using (var conn = new MySql.Data.MySqlClient.MySqlConnection(cfg.GetProperty("connection.connection_string")))
                {
                    conn.Open();
                    schemaUpdateScript = cfg.GenerateSchemaUpdateScript(dialect,
                                              new NHibernate.Tool.hbm2ddl.DatabaseMetadata(conn, dialect, false));
                }

                return schemaUpdateScript;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
mcansener commented 2 years ago

It is an old issue but I have solved it. Hope someone else would find this beneficial. I had the same issue recently and I have changed the region / language settings to United States and it worked like a charm. It is an issue with the turkish characters I guess like you guys mentioned.