Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create unique constraint for 2 columns in JDL-Studio?

Tags:

jhipster

I have following entity configuration:

entity AirplaneModelSeat { 
    id Long, 
    seatNo String required 
}
relationship ManyToOne   { 
    AirplaneModelSeat{modelId(model)} to AirplaneModel 
}

This entity configuration creates such a table:

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| seat_no     | varchar(255) | NO   |     | NULL    |                |
| model_id_id | bigint(20)   | YES  | MUL | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

How can I apply unique constraint for (seat_no, model_id_id) column combination in JDL-Studio?

If this is not possible in JDL-Studioi is there any other to accomplish this?

like image 847
Halil Avatar asked Sep 04 '16 10:09

Halil


People also ask

How do I create a unique constraint in multiple columns?

To define a UNIQUE constraint, you use the UNIQUE keyword followed by one or more columns. You can define a UNIQUE constraint at the column or the table level. Only at the table level, you can define a UNIQUE constraint across multiple columns.

How do I add a unique constraint in two columns in SQL?

To define a UNIQUE on multiple columns, we put a comma-separated columns list inside parenthesis that follows the UNIQUE keyword.

Can we apply unique key constraint on multiple columns?

Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint. However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

How do I create a Jhipster entity?

You can generate entities from a JDL file using the jdl sub-generator, by running jhipster jdl your-jdl-file. jh . If you do not want to regenerate your entities, while importing a JDL, you can use the --json-only flag to skip entity creation part and create only the json files in . jhipster folder.


1 Answers

As far as I know, constraints are not part of JDL in a general manner. You can define such things as validations. But as for the Domain, the unique constraint is not that general anymore, it is a database level constraint, where it has to be applied.

For this, JHipster includes Liquibase. So you can find the changelog, defining the entity constraints in "src/main/resources/config/liquibase", and add a

<addUniqueConstraint tableName="airplane_model_seat" columnNames="seat_no, model_id_id"/>

to that changelog.

If you already started your application used h2 disk persistent databse, make a mvn clean / ./gradlew clean before starting your app again.

like image 75
David Steiman Avatar answered Oct 12 '22 23:10

David Steiman