Grails generating error while altering a table - grails

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.

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.

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.

Try use class, not primitive type: Boolean expedite

Related

Is there a way to trigger spring-elastic-search mapping type when a field value is null by default?

In our current Spring-data-elasticsearch v.4.0.4 application, we are using #Document on a POJO and we notice that whenever we have a null value field, the field will not have a mapping type in Elasticsearch. Therefore when we run a query and sort on this field, we will have Elasticsearch error No mapping found for [view_date] in order to sort on
We tried using the storeNulValue option of the #Field in v.4.1.0, the field presents in the index. However, there is still no mapping type for this field in Elasticsearch and we still see the no mapping found error when we sort on this null value field.
Is there a way to trigger the mapping even when a field has null value?
How do you have the mapping for your index created? When creating the mapping with Spring Data Elasticsearch this is done without a special entity and without knowing if a special instance has null valued properties. the storeNullValue argument is used then.
The mapping will be automatically created when you use Spring Data Elasticsearch repositories and the index does not yet exist. Or you need to call the IndexOperations.create() and IndexOperations.putMapping() by yourself.

use grails unique constraint only for generating DDL

I would like to generate multicolumn unique constraints in Grails such as one defined in following entity class
class Relationship {
Element source
Element destination
Type type
// other properties omitted
static constraints = {
type unique: ['source', 'destination']
}
}
but I don't want to have that constraint active during the validation as it consumes lot of resources (see alternative to grails multicolumn unique constraint (optimistic inserts)) for stats.
Is there any way to achieve that? What are the alternatives to generate the unique index automatically (with checking for existing one)?
I've done this in an app where I knew that a column's values were unique based on how they were generated, so I didn't want Grails to run a select query during validation, but wanted the database check just in case something weird happened.
I would do this using a database migration. Add whatever constraints and indexes you like, in addition to the updates that are needed to keep the code and database in sync (e.g. adding/removing columns or tables, changing column types, etc.)

How does Breeze handle database column defaults?

I can't find any info about this in the documentation, so I will ask here. How does breeze handle database column defaults? I have required columns in my database, but there are also default static values supplied for these in the database column definitions. Normally, I can insert null into these columns, and the new records will get the default. However, breeze doesn't seem to be aware of database column defaults, and the entities that have null in these columns fail validation on saving.
Thanks,
Mathias
Try editing the edmx xml by adding StoreGeneratedPattern = "Computed" attribute to the column with default value in the DB.
Edit:
Actually, before doing editing the xml, try setting the StoreGeneratedPattern property to Computed in the model editor itself.
Update:
This was fixed in Breeze 1.4.6 ( or later), available now.
Original Post:
There is currently in a bug in Breeze that should be fixed in the next release, out in about week. When this fix gets in then breeze will honor any defaultValues it finds in the EntityFramework data model.
One problem though is while it is easy to get 'defaultValues' into a Model First Entity Framework model via the properties editor, it's actually difficult to get it into a Code First EF model, unless you use fluent configuration. Unfortunately, EF ignores the [DefaultValue] attribute when constructing Code First model metadata.
One workaround that you can use now is to poke the 'defaultValue' directly onto any dataProperty. Something like:
var customerType = myEntityManager.metadataStore.getEntityType("Customer");
var fooProperty = customerType.getProperty("foo");
fooProperty.defaultValue = 123;

Grails db-reverse-engineer id and version

I am still new to Grails and Hibernate so if you answer keep in mind I have only used the IDE for 5 days. I have a MYSQL db I am trying to connect to. I set the DB to update as I will need to later on. Then I run the db-reverse-engineer plugin and it auto generates the groovy files in STS for all the tables. When I look at the files I notice in the file a static mapping. I tested one table and created its controller to show all records. It ran perfect. When I looked at the table structure it create 2 new columns "id" and "version". So I noticed the static mapping looks like this:
class TopTen {
Integer ttMlId
Integer ttWeekId
Integer ttAmount
Integer ttRank
static mapping = {
id column: "tt_id"
version false
}
}
When I remove these then it doesn't work at all. I can understand the second line but not why it would create a new column in the DB, and I don't understand what version is or why it puts it there when reverse engineering.
Here is the db table in MySQL database version 5.0.51
DROP TABLE IF EXISTS `top_ten`;
CREATE TABLE `top_ten` (
`tt_id` int(10) unsigned NOT NULL auto_increment,
`tt_ml_id` int(10) unsigned NOT NULL default '0',
`tt_week_id` int(10) unsigned NOT NULL default '601',
`tt_amount` int(10) unsigned NOT NULL default '0',
`tt_rank` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`tt_id`)
) ENGINE=InnoDB AUTO_INCREMENT=511 DEFAULT CHARSET=latin1;
Hibernate can use optimistic locking and by default it's enabled for all GORM domain classes. But if you don't have a column to use for that in a legacy table or if you want to explicitly disable it for some reason, you can add version false to the mapping block and it won't be active.
Optimistic locking is implemented by comparing the version you think you're editing with the current version at the time the row is updated, and if there's a mismatch it's assumed that there was another user's edit between when you read the row to display the edit form and when you submitted the updated data. It's called optimistic since there's no explicit locking, which is safer but expensive, and it's hoped that two users won't edit the same row at the same time.
If you're seeing a new 'id' column in the database with that mapping block, something is wrong. That should tell GORM that the primary key column has a non-standard column name of 'ttx_id' and use that instead of the 'id' column that it typically uses. Please create a bug report at http://jira.codehaus.org/browse/GRAILSPLUGINS under the Grails-Reverse-Engineer component and include the current database table SQL and the contents of the generated domain class and I'll take a look.

Enityframework - ConcurrencyMode where clause

Reading up on the web I can set ConcurrencyMode=Fixed against a entity framework database field.
My understanding is that any update statements include in its where clause the original values to determine if the datacontext has changed.
(So if rows effected gets a hit then all is good otherwise we have a conflict )
Now my question is..
Do only the columns changed in the
datacontext get included in the where
clause or all columns that are
marked as fixed.
i.e.
(If I have the following setup)
name=fixedconcurrency
DateofBith=fixedconcurrency
NI=fixedconcurrency
When only the name field changes would I get:
update tbuser set name="newJason"
where Id=2 and name="oldJason" and
DateofBith="19/10/1970" and NI=1234566
or
Update tbuser set name="newJason"
where Id=2 and name="oldJason"
My goal is to only have conflicts raised when a user overwrites another users data ( At field level not record level).
A snippet from MS says that entity framework will only update the fields that a user has edited. IF all fields are included in the where clause it would make this statement redundant.
Thanks,
Jason
All of them. The intention of the feature is for something like a TIMESTAMP field, which the user cannot assign directly.

Resources