Search code examples
jpacuba-platform

Failing me to apply update query scripts in Cuba Platform


I am receiving this error when I apply this update database script.

default expression needed in statement [alter table WATERSCHEME_LOCATION add column LGA_ID integer not null ]

LGA_ID is a column that is associated with Location Table, LGA_ID is set to mandatory. But when I remove the mandatory, I can update the scripts.

@Table(name = "WATERSCHEME_SCHEME_C")
@Entity(name = "waterscheme$Scheme_c")
public class Scheme_c extends BaseIntegerIdEntity {
private static final long serialVersionUID = -5886267876250540580L;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "LGA_NAME_ID")
protected Lga lgaName;"""

this is my location table

/*
 * Copyright (c) 2016 water-scheme
 */
package com.company.waterscheme.entity;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.haulmont.cuba.core.entity.BaseIntegerIdEntity;
import com.haulmont.chile.core.annotations.NamePattern;

/**
 * @author samuel.thampy
 */
@NamePattern("%s|locationName")
@Table(name = "WATERSCHEME_LOCATION")
@Entity(name = "waterscheme$Location")
public class Location extends BaseIntegerIdEntity {
    private static final long serialVersionUID = 8201565825728955033L;

    @Column(name = "LOCATION_NAME", nullable = false)
    protected String locationName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "LGA_ID")
    protected Lga lgaId;

    public void setLocationName(String locationName) {
        this.locationName = locationName;
    }

    public String getLocationName() {
        return locationName;
    }

    public void setLgaId(Lga lgaId) {
        this.lgaId = lgaId;
    }

    public Lga getLgaId() {
        return lgaId;
    }
    }

Can somebody help me why I should set a default value?


Solution

  • Another way is to modify your update script. If you use the CUBA Studio then go the Entity panel and press Generate DB scripts as it is shown in the picture below:

    enter image description here

    Then you will see all database scripts. Find proper scrip that has alter table WATERSCHEME_LOCATION add column LGA_ID integer not null statement and add DEFAULT {DEFAULT_VALUE} clause, where you can specify id of the Location entity that should be set while altering for null values. Now you can update your database!

    P.S. Note that if you run this script on another database it may cause FOREIGN KEY constraint exception, because Location with the default id doesn't exist. To avoid this you can add another statement to the same update script, so that if the default location doesn't exist in your database then insert it with the specified id.