I have legacy Database Schema (PostgreSQL) I cant change it. I’m having problems mapping my class with a database model, this is what I want and what I have:
-> Database Schema – DataTable Users – PostgreSQL
PK id STRING (email), password STRING
-> My model – Domain Users
PK email STRING, password STRING
I need be able to let to the users insert via the create.gsp view the id (email) of the user, i dont want to change manually the create.gsp or other .gsp I want Grails auto-generate the views for me.
So, How can i do to auto-generate the views showing the primary key like other field to let final user insert/see it?
In my Domain I have something like this:
class Usuario {
String id
String password
static constraints = {
id email: true
password blank:false, nullable:false
}
static mapping = {
table 'usuario'
version false
id column: 'id', generator: 'assigned'
}
String toString(){
return id
}
}
Thanks in advance, and sorry about my english!
UPDATE:
I just try to insert [the following piece of code] into my user controller and now i can see the id field, but only when I create a new user create.gsp, when I show list of users in the list.gsp I cant see the field id (email)
def scaffold = Usuario
UPDATE II:
I have followed the comment from user1128791 and it worked for me, I install the templates system and then I worked over it, changing list.gsp form src/templates/scaffolding to show the id field, something like this:
excludedProps = Event.allEvents.toList() << 'version' << 'id'
allowedNames = domainClass.persistentProperties*.name << 'dateCreated' << 'lastUpdated'
for this
excludedProps = Event.allEvents.toList() << 'version'
allowedNames = domainClass.persistentProperties*.name << 'dateCreated' << 'lastUpdated' << 'id'
UPDATE III:
I’ve gone further and made the following changes, to work only with my email field and password field.
class User {
String email
String password
static transients = ['id']
void setId(String email) {
id = email
}
String getId() {
return email
}
static constraints = {
email email: true, blank:false, nullable:false
password blank:false, nullable:false
}
static mapping = {
table 'usuario'
version false
id generator: 'assigned', name: 'email'
email column: 'id'
}
String toString(){
return id
}
}
To enable you to have the maximum amount of information about my problem and how i solved it, thats what i’m mapping:
CREATE TABLE usuario
(
id text NOT NULL,
"password" text,
CONSTRAINT "PK usuario" PRIMARY KEY (id)
);
From http://www.grails.org/Artifact+and+Scaffolding+Templates:
To customize the templates for you project you need to the install the templates:
This will create the src/templates folder in your project which will contain various artifact and scaffolding templates.
Than you will have to change each *.gsp template to render a textfield instead of hidden field for ID’s.
EDIT: Looking into the templates. Grails 2.0 should do this automaticaly for ‘assigned’ id’s.