1

I must admit I'm still pretty new to NHibernate, so please go easy on me.

I have 2 POCOs, a Log and a UserProfile. If it is not inherently clear, the log entry can reference a user profile, and a user profile can have many log entries associated with it. The mapping files look like this:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="C3.DataModel.Generated"
                   namespace="C3.DataModel">
    <class name="Log" table="Logs">
        <id name="LogId">
            <generator class="guid" />
        </id>
        <property name="TimeStamp" column="TimeStamp" index="ixLogTimeStamp" not-null="false" />
        <property name="Thread" length="255" column="Thread" not-null="false" />
        <property name="Severity" column="Severity" />
        <property name="Source" length="255" not-null="false" column="Source" />
        <property name="Message" length="4000" not-null="false" column="Message" />
        <property name="Exception" length="4000" column="Exception" not-null="true" />
        <many-to-one name="UserProfile" not-null="false" class="UserProfile" foreign-key="UserID" column="UserID" />
    </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="C3.DataModel.Generated"
                   namespace="C3.DataModel">
    <class name="UserProfile" table="UserProfiles">
        <id name="UserID">
            <generator class="assigned" />
        </id>
        <property name="GivenName" length="40" column="GivenName" />
        <property name="MiddleName" length="40" column="MiddleName" not-null="false" />
        <property name="FamilyName" length="40" column="FamilyName" />
        <property name="UserName" length="250" index="ixUserName" unique="true" />
        <property name="NickName" length="40" column="NickName" />
        <property name="Address1" length="50" column="Address1" />
        <property name="Address2" length="50" column="Address2" />
        <property name="City" length="50" column="City" />
        <property name="State" length="2" column="State" />
        <property name="ZipCode" length="10" column="ZipCode" />
        <property name="Bio" length="2000" column="Bio" not-null="false" />
        <property name="BirthDate" not-null="false" column="BirthDate" />
        <property name="LinkToAvatar" length="250" column="LinkToAvatar" not-null="false" />
        <property name="LinkToWebpage" length="250" column="LinkToWebPage" not-null="false" />
        <bag name="ActivityLogs" cascade="none" table="Logs" >
            <key column="LogId" />
            <one-to-many class="Log"/>
        </bag>
    </class>
</hibernate-mapping>

When I'm trying to build my schema against a PostgreSQL database, I receive the following error:

NHibernate.HibernateException: ERROR: 42804: foreign key constraint "fkf9c1212b275a3569" cannot be implemented ---> Npgsql.NpgsqlException: ERROR: 42804: foreign key constraint "fkf9c1212b275a3569" cannot be implemented
   at Npgsql.NpgsqlState.<ProcessBackendResponses_Ver_3>d__a.MoveNext()
   at Npgsql.ForwardsOnlyDataReader.GetNextResponseObject()
   at Npgsql.ForwardsOnlyDataReader.GetNextRowDescription()
   at Npgsql.ForwardsOnlyDataReader.NextResult()
   at Npgsql.ForwardsOnlyDataReader..ctor(IEnumerable`1 dataEnumeration, CommandBehavior behavior, NpgsqlCommand command, NotificationThreadBlock threadBlock, Boolean synchOnReadError)
   at Npgsql.NpgsqlCommand.GetReader(CommandBehavior cb)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery()
   at NHibernate.Tool.hbm2ddl.SchemaExport.ExecuteSql(IDbCommand cmd, String sql)
   at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolean export, Boolean throwOnError, TextWriter exportOutput, IDbCommand statement, String sql)
   at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolean export, Boolean justDrop, IDbConnection connection, TextWriter exportOutput)
   at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolean export, Boolean justDrop)
   --- End of inner exception stack trace ---
   at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolean export, Boolean justDrop)
   at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Boolean script, Boolean export, Boolean justDrop)
   at NHibernate.Tool.hbm2ddl.SchemaExport.Create(Boolean script, Boolean export)
   at C3.DataModel.Repositories.NHibernateHelper.CreateSchema() in C:\projects\C3\C3.DataModel.Generated\Repositories\NHibernateHelper.cs:line 41

The SQL it's trying to run at the time of death is:

alter table Logs add constraint FKF9C1212B275A3569 foreign key (LogId) references UserProfiles

...which is clearly not what I was expecting it to do, but I don't know where to fix it. Tell me what I'm doing wrong first, and get some quick rep. If you can explain it so I can understand where I went wrong, I'd be most appreciative.

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254
  • You Log map contains and both the `column` and `foreign-keys` are named UserId. Is this correct? – Rippo Apr 19 '12 at 08:10
  • I tried that several different ways, (without noting a foreign key at all, without noting the column...) and it did not seem to make a difference. – Jeremy Holovacs Apr 19 '12 at 11:10

2 Answers2

0

if you do not want the foreign key then get rid of the following line from your mapping:

<key column="LogId" />

In your question you have not specified what you expect it do.

Baz1nga
  • 15,485
  • 3
  • 35
  • 61
0

The answer was simple; I wish the error reflected what was truly wrong.

I was trying to set up a bidirectional relationship by defining it in the mappings for both; this was both unnecessary and incorrect. I removed the

   <bag name="ActivityLogs" cascade="none" table="Logs" >
        <key column="LogId" />
        <one-to-many class="Log"/>
   </bag>

element from my UserProfile configuration, and the error went away.

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254