Fluent NHibernate 3 uniquekey custom constraint name - fluent-nhibernate

I am trying to give a custom Unique Constraint Name as follows:
Map(x => x.Name).UniqueKey("MY_CONSTRAINT_NAME").Column("FUNCTION_NAME");
The Field is mapped with a unique constraint but the constraint name is self-managed and doesn't take the name I chose ("MY_CONSTRAINT_NAME")
Is this a BUG or am I using it incorrectly?

NHibernate itself does not allow you to supply a name for the unique key. https://nhibernate.jira.com/browse/NH-1955

Related

How do I make sure that all rows with the same foreign key have unique names in my PostgreSQL database?

I have a PostgreSQL database with two tables: Team and Project, in a one-to-many relationship.
Team has these columns:
id
name
Project has these:
id
name
team_id
I'd like to make sure that the projects within a team must have unique names.
Projects belonging to different teams should be able to share names without a problem, so UNIQUE doesn't really help.
It seems like I might be able to use a custom CHECK constraint to do this, but failing that, what would be a sensible, declarative way to implement it in javascript?
I'm using Prisma to interact with my database, and elsewhere in my app I'm using Yup to validate the schemas of objects. Perhaps I could combine these somehow?
You can create a unique constraint:
alter table project add constraint unq_project_teamid_name
unique (team_id, name);
You can also do this with a unique index:
create unique index unq_project_teamid_name on project(team_id, name);
You can use a compound unique key:
create table project (
id serial primary key,
name text,
team_id int references team(id),
unique (team_id, name)
);
Or if you want to add it to an existing table:
alter table project
add constraint cs_project_uniq_name_team
unique(team_id, name)
;

Hibernate Annotations Id for table with no primary key

I have a table that has no primary key, and one cannot be created either. I can construct a unique key using three columns of this table. Now hibernate demands an id for every annotated class, how do i satisfy this id with the unique Id I can create.
If it is an entity type then you should use a composite key. This can be done by moving primary key fields to separate class and mapping that in entity class with an #Id annotation.
See Mapping composite primary keys and foreign keys to composite primary keys
If it is not an entity but a value type you should map it accordingly.
See https://stackoverflow.com/a/1696146/324900 and Entity and value types in hibernate

Primary AND composite keys in one Entity with Fluent Nhibernate

Here is my problem:
Domain: I have following Entities: [Sensor] that can be positioned at [Location]. It is a many-to-many relationship. I break it into two one-to-many to aggregate a [Position] of a [Sensor] at [Location]. Intermediate Entity is [SensorPosition].
Mapping for [SensorPosition] is as follows:
CompositeId().KeyReference(sp => sp.Sensor).KeyReference(sp => sp.Location);
References(sp => sp.Sensor).ForeignKey().Not.Nullable();
References(sp => sp.Location).ForeignKey().Not.Nullable();
Component(sp => sp.Position, p =>
{
p.Map(pos => pos.X);
p.Map(pos => pos.Y);
p.Map(pos => pos.Z);
});
I'm using a CompositeId() to enforce constraint of only one [Sensor] at one [Location]. (Same [Sensor] can be at different [Location]s, it is a business logic twist)
My question is: Can I add a generated primary key (Id) to this? I've tried it, but with CompositeId() in the mapping it is not being generated. Or is there any other way to enforce this constraint fluently?
I would avoid using composite primary keys. Uniquenes can be enforced by this mapping:
References(sp => sp.Sensor).UniqueKey("KeyName");
References(sp => sp.Location).UniqueKey("KeyName");
For more details see this question.
To generate composite primary key IDs you need to establish a bi-directional relationship between the foreign key properties and each entity referenced in the composite ID. When you create an instance of SensorPosition, assign each References property to the appropriate entities that make up the key and NHibernate will use their Id values for the SensorPosition key.
In the long run, it's much simpler (and generally recommended) to use a "surrogate" key instead of a composite key in NHibernate.

Add a unique constraint on multiple columns with FluentNHibernate

I have a class that has a Primary Key as well as 2 foreign keys. Foreign combinations must be unique. I do not see a way to do this (at least since SetAttribute was deprecated).
James touched on this with SetAttribute:
How to create a Multi-Column Index or Unique Constraint with NHibernate
This might be useful to someone else, FNH mapping of unique constraint is accomplished like this:
mapping.References<FirstClass>(x => x.FirstClass).UniqueKey("unique123");
mapping.References<SecondClass>(x => x.SecondClass).UniqueKey("unique123");
Further, it is explained that this only builds the constraint in the db, but that the developer is responsible to intercept duplicate insert attempts, otherwise an SqlException will be thrown saying an UNIQUE KEY constraint was violated.
from the FNH group

Prevent Nhibernate schemaexport from generating foreign key constraints on has many relationship

I have a mapping like this:
HasMany(x => x.Orders).KeyColumn("CustomerID");
Which is causing a constraint like this to be generated by schemaexport:
alter table [CustomerOrder]
add constraint FK45B3FB85AF01218D
foreign key (CustomerID)
references [Customer]
I have tried adding .NotFound.Ignore() like on a References() mapping to disable the constraint from being generated but this does not work.
Can a mapping be defined that will force SchemaExport to not generate the constraint?
Figured it out:
HasMany(x => x.Orders).KeyColumn("CustomerID").ForeignKeyConstraintName("none");
buried in the source is a check to ignore creation if the name is "none"