"Fossies" - the Fresh Open Source Software Archive

Member "grails-core-5.2.4/grails-docs/src/test/resources/docs/ref/Constraints/unique.html" (14 Sep 2022, 60926 Bytes) of package /linux/www/grails-core-5.2.4.tar.gz:


Caution: In this restricted "Fossies" environment the current HTML page may not be correctly presentated and may have some non-functional links. You can here alternatively try to browse the pure source code or just view or download the uninterpreted raw source code. If the rendering is insufficient you may try to find and view the page on the grails-core-5.2.4.tar.gz project site itself.

(Quick Reference)

unique

Purpose

Constrains a property as unique at the database level

Examples

username unique: true

Description

Set to true if the property must be unique. This is a persistent call and will query the database.

It is possible (though unlikely in practice) for uniqueness validation to pass but for the subsequent save to fail. If another save or update happens to update the database between the Grails check and the actual save of your instance, the call will fail. The only way to prevent this would be to use a transaction at the SERIALIZABLE isolation level, but this is very bad for performance.

You can also define multi-column unique constraints by declaring the other field(s) to be included as the parameter value. If there is one other field, specify its name, but if there are more than one use a List, for example:

Example:

group unique: 'department'

In this example the group name must be unique in one department but there might be groups with same name in different departments, i.e. the group name isn’t unique by itself.

Another example:

username(unique: ['group', 'department'])

In this example username must be unique in group and department. There might be same logins in different groups or different departments.

This constraint influences schema generation.

Error Code: className.propertyName.unique