Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify order of fields in DDL generated from GORM classes?

I use GORM to generate my database's DDL from groovy classes. Which is great. However, the order of fields in the generated SQL is not the same as the order of fields in the class. For example, if I create the class

class Person
{
  String firstName
  String lastName
  String address
  String email
}

the following SQL is generated (for MySQL)

CREATE TABLE `test` (
  `id` bigint(20) NOT NULL auto_increment,
  `version` bigint(20) NOT NULL,
  `address` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `first_name` varchar(255) NOT NULL,
  `last_name` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

The fields have been sorted into alphabetical order (after the autogenerated id and version fields). This is O.K. in this instance, but I have some much wider tables in which there is important contextual information in the order of the fields.

Here is my question: How do you tell GORM to order the fields in SQL in the order of declaration in the groovy class?

like image 939
mycro.be Avatar asked Dec 02 '25 22:12

mycro.be


1 Answers

I'm not absolutely sure about this, but the constraints closure defines the order of the fields in the views, maybe that reflects to the fields on tables.

class Person
{
  String firstName
  String lastName
  String address
  String email
}

static constraints = {
  firstName()
  lastName()
  address()
  email()
}
like image 69
elbicho Avatar answered Dec 05 '25 10:12

elbicho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!