How to implement a unique validation on an attribute + foreign key

Hi, I have a very simple data model to try out Mendix. ORDER and ORDER LINE with a one to many relationship. ORDER LINE contains a sequence number. I want the sequence number to be unique for one order. Normally I would add a database constraint on the combination of the sequence number and the foreign key pointing to ORDER. But that doesn't seem possible in Mendix. So how can I implement such a rule? Secondly, how can I enforce two attributes of an entity to be unique? Help is much appreciated.
3 answers

Just use a 'Rule' which checks the uniqueness from a BCo microflow

Retrieve action on Orderline within this rule has an XPath that looks something like below.

[Module.OrderlineOrder = $Orderline/Module.OrderlineOrder] [Seq = $Orderline/Seq] [id != $Orderline]

If an object is found the orderline sequence is not unique within the order

The same goes for a unique combi of attributes:

[id != $Orderline] [Attr1 = $Orderline/Attr1] [Attr2 = $Orderline/Attr2]


A simular question: same data model ORDER and ORDERLINE with a one to many relationship.

In ORDERLINE I would like an attribute LINENUMBER to increase by one automatically every time I add an ORDERLINE.

I tried to add this into the Domain model as a microflow for this attribute. I retrieve the Order, get a list of ORDERLINES (or empty). So far so good. But If i want to sort the list I get an error: "Calculated value cannot be used in its own calculation."

Any tips?

Regards Mark


The solution of checking the uniqueness of combined attributes within a Before Commit microflow is not atomic and not thread safe. It will not guarantee uniqueness in a multi-user environment. However it could be very rare that duplication will arise. You could disallow the concurrent execution of the BC MF. But that is very restrictive and has side effects of error messages with rollback or running an alternative MF.

Concatenating the attributes in a new attribute in the entity with a uniqueness validation is guaranteed unique.

Otherwise you need a semaphore before the uniqueness check in the MF that allows only one thread at a time to pass and let others wait (like synchronized code in Java). One a single server deployment you could implement it in Java (global system storage) or in combination with the database. But on a multi-server environment (load-balanced) I do not think there is anything that could synchronize between threads on different servers. I even think that disallowing concurrent MF on different servers does not synchronize.