Database constraint names and hash collisions

Description

namespace NHibernate.Mapping
{
...
public class Table : IRelationalModel
{
...
public string UniqueColumnString(IEnumerable iterator, string referencedEntityName)
{
...
return (name.GetHashCode().ToString("X") + result.GetHashCode().ToString("X")); // Ouch!
}
}
}

We've just hit a problem on this line where we have two constraints on the same table.

Because they're on the same table, name is the same.
And we have found a hash collision on result, due to weakness in how result is being generated.

Adding hashes together for string hashes isn't safe.

In our case,
"BluewireTechnologies.Core.Framework.DynamicTypes2.Albatross.ITestManyA".GetHashCode()+"itestmanyaid".GetHashCode()
Is equal to:
"BluewireTechnologies.Core.Framework.DynamicTypes2.Albatross.ITestManyB".GetHashCode()+"itestmanybid".GetHashCode()

On V2 runtime.

Environment

None

Activity

Show:

Fabio Maulo July 22, 2008 at 2:14 PM

sometime I found the configuration:
ATABLE
referencedEntityName ="BluewireTechnologies.Core.Framework.DynamicTypes2.Albatross.ITestManyA"
on column "itestmanyaid"

referencedEntityName ="BluewireTechnologies.Core.Framework.DynamicTypes2.Albatross.ITestManyB"
on column "itestmanybid"

The two reference create the same name.
I'm working on test.

Fabio Maulo July 22, 2008 at 2:03 PM

I need your help to recreate a real case.
In that line the field "name" is the name of the table and "BluewireTechnologies.Core.Framework.DynamicTypes2.Albatross.ITestManyA" is not a valid table name.
At the same time the string "itestmanyaid" is never used directly because the variable "result" is the integer resulting by sum of hashCode of each columnName of the FK.

Resuming
"ITestManyA".GetHashCode()+"itestmanyaid".GetHashCode()..GetHashCode()
Is NOT equal to:
"ITestManyB".GetHashCode()+"itestmanybid".GetHashCode()..GetHashCode()

I would like to have a real failing test.
Thanks.

Alun Harford July 21, 2008 at 2:05 AM

Note that names of DB contraints are using the generated hashes, so hash collisions cause constraints of the same name to be generated. Databases don't like that much.

Fixed

Details

Assignee

Reporter

Components

Fix versions

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created July 21, 2008 at 2:02 AM
Updated July 22, 2008 at 2:37 PM
Resolved July 22, 2008 at 2:37 PM
Who's Looking?