0

I am trying to move an already existing PHP application into grails.

I have created the domains based on the existing database and the code worked perfectly.

The issue arises when I need to add an additional boolean field in my domain.

I am getting the following error.

2014-06-10 16:24:54,146 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate  - Unsuccessful: alter table entry add expedite tinyint not null

Error |
2014-06-10 16:24:54,163 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate  - ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'expedite' cannot be added to non-empty table 'entry' because it does not satisfy these conditions.

I have tried to specify default values in the variable itself.

boolean expedite = false

I also tried to add default values in static mapping as below:

static mapping = {
    table 'entry'
    expedite defaultValue: false
    version false
}

But still the error crops up. Any idea where I am going wrong? I am using sql server 2012.

Rammohan
  • 493
  • 6
  • 27

3 Answers3

1

Since by default mysql maps boolean field as one bit value, so the value of the boolean field can not be null.

Update your existing records manually by:

update my_table set expedite = 0;

Or you can use grails database migration plugin to generate migrations for you.

Any primitive data types in a domain class gets default value, so if you would have defined your new field like Boolean expedite then it can work with null values.

So always be sure with primitive & non primitive data types.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
  • My problem is that I cannot update the tables manually. I need to do it using gorm. This problem continues even using non-primitive data types. Also this problem occurs using sql server 2012, not mysql. – Rammohan Jun 11 '14 at 09:17
0

Looks like sql server uses 0 and 1 instead of TRUE/FALSE. Is this what you are looking for?

https://forum.hibernate.org/viewtopic.php?f=1&t=996345

another person solves like this...

http://codexplo.wordpress.com/2012/06/21/mapping-a-boolean-with-hibernate/

Looks like you just have to create a method in your domain to intercept and convert.

Orubel
  • 316
  • 4
  • 16
0

Try use class, not primitive type: Boolean expedite

Andrius V.
  • 13
  • 1
  • 4
  • I get the same error even using Boolean class. This problem arises only using sql server 2012. It does not occur in mysql. – Rammohan Jun 11 '14 at 09:14
  • Is it possible that field is already created in a table ? and you are tying to map it. If so remove field manually and let grails create field for you again using class declaration. – Andrius V. Jun 11 '14 at 12:32
  • No, this field is not present in any other table. – Rammohan Jun 11 '14 at 14:26
  • Tried on SQL Server 2005. Got the same exception. It works if constrains has: expedite(nullable : true) – Andrius V. Jun 19 '14 at 14:45