I’ve a users table and want to use username (which is an email address) as the id field so am using the generated:’assigned” mapping to do this. I also have added the constraint for the username field to be validated as an email address but this is not enforced. Here is my user class:
class User {
transient springSecurityService
String username
String password
String firstName
String lastName
static hasMany = [accounts: Account]
static hasOne = [company:Company]
static belongsTo = Company
static constraints = {
username blank: false, unique: true, email: true
password blank: false
accounts nullable:true
}
static mapping = {
password column: '`password`'
id generator:'assigned', name:'username', property:'username'
}
Basically, I’m trying to use the user’s email as the id field and use that as the foreign key in its relationships with other objects (as the one-to-many relationship the user has with Account), but when I try to manually add username in dbconsole, it lets me add any string.
How do I enforce the email constraint?
You need to remember that the constraints and validation framework are used before to try insert into database using GORM. If you are using dbconsole directly you are inserting directly in the database and you are skipping the validation framework.
If you need validate the email in the database maybe you can define a BEFORE INSERT TRIGGER in your table. But it will be database specific.