When using SchemaUpdate() to export a mapped class with auto-incremented id to a PostgreSQL server the following exception is thrown:
“ERROR: 42P07: relation “seq_wuf” already exists”.
The exception is thrown because the table and sequence really exist in the server, but I would expect for SchemaUpdate() to ignore the already existing sequence.
Mapping table
public class CategoryMap : ClassMap<Category>
{
public static string tableName = "miao";
public CategoryMap()
{
SchemaAction.Export();
Table(tableName);
Id(x => x.Id).GeneratedBy.Sequence("SEQ_" + tableName);
Map(x => x.Name).Column("Category")
.CustomType("String")
.Access.Property()
.Generated.Never()
//.CustomSqlType("nvarchar(50)") // <----
.Not.Nullable()
.Length(50); ;
Map(x => x.Description);
}
}
Export Schema:
FluentConfiguration config = Fluently.Configure();
config
.Database(PostgreSQLConfiguration
.Standard
.ConnectionString(connStringPosgtgres))
.Mappings(m => m.FluentMappings.Add(typeof (CategoryMap)))
.ExposeConfiguration(UpdateSchema)
.BuildConfiguration();
SchemaUpdate()
private static void CreateSchema(Configuration cfg)
{
var schemaExport = new SchemaExport(cfg);
schemaExport.Drop(false, true);
schemaExport.Create(false, true);
}
Thanks,
Yuval
I found out why this happens.
Basically, while ScemaUpdate can create the auto ID sequence, it can’t check if the sequence exists in the DB, and therefore tries to recreate an existing object prompting the error message.
This code is used by NHibernate to check for the sequance (src: https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Dialect/Schema/AbstractDataBaseSchema.cs).
I tried toying with it for a while and also couldn’t retrieve sequnces from it.
Yuval.