Skip to content
This repository has been archived by the owner on Mar 21, 2021. It is now read-only.

Adding commands for unique constraints #18

Closed
heppa opened this issue May 12, 2016 · 17 comments
Closed

Adding commands for unique constraints #18

heppa opened this issue May 12, 2016 · 17 comments

Comments

@heppa
Copy link

heppa commented May 12, 2016

I just love expressing the domain data model in JDL. However, whenever there is a change, I find myself manually editing the server code plus regenerating the client and test code with jhipster-uml, because there is manual changes necessary in the domain code like unique constraints, advanced validation constraints etc.

Wouldn't it be nice to have the ability to generate unique constraints by expressing them via JDL? Or am I missing something?

for example:
unique { <entity>{<column_list>} }

example:
unique { A{columnA, columnB} }

to define a unique constraint on entity A with columns columnA and columnB.

@MathieuAA
Copy link
Member

@deepu105 Is unique available in a released version? I was on holidays and I didn't check my mails at all (and couldn't).
@heppa If it's available, I'll add it.

@deepu105
Copy link
Member

No we dont support that. Inly required validation for relationship as of
now. Having unique validation is very tricky and troublesome
On 17 May 2016 06:05, "Mathieu ABOU-AICHI" notifications@github.com wrote:

@deepu105 https://github.com/deepu105 Is unique available in a released
version? I was on holidays and I didn't check my mails at all (and
couldn't).
@heppa https://github.com/heppa If it's available, I'll add it.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#18 (comment)

@MathieuAA
Copy link
Member

Yeah... Thanks man!

Le 17 mai 2016 03:51:21 GMT+02:00, Deepu K Sasidharan notifications@github.com a écrit :

No we dont support that. Inly required validation for relationship as
of
now. Having unique validation is very tricky and troublesome
On 17 May 2016 06:05, "Mathieu ABOU-AICHI" notifications@github.com
wrote:

@deepu105 https://github.com/deepu105 Is unique available in a
released
version? I was on holidays and I didn't check my mails at all (and
couldn't).
@heppa https://github.com/heppa If it's available, I'll add it.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub

#18 (comment)


You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#18 (comment)

Mathieu ABOU-AICHI

@taurus227
Copy link

@deepu105 "Having unique validation is very tricky and troublesome" Why do you say that?

@deepu105
Copy link
Member

deepu105 commented Nov 3, 2017

To really validate something is unique before saving it you need to compare it against existing items to determine if unique which is costly and tricky operation. If you have a simpler way to do it you are welcome to suggest

@heppa
Copy link
Author

heppa commented Nov 3, 2017

Sorry for getting back on this so late, but Robert Mikes' question got me back here - I also don't really understand this, because what I mean is, that I would like to add the "@column(unique=true)" JPA annotation parameter to tell the DB to add a unique constraint. I don't want anything to be validated.

@gzsombor
Copy link
Member

gzsombor commented Nov 3, 2017

The @column(unique=true) just changes the JPA/Hibernate schema generation, which JHipster doesn't use, as we relay on Liquibase scripts to create the db.
It is possible to extend the validators, with a generic, unique validator, but this will create an additional sql query every time.

@heppa
Copy link
Author

heppa commented Nov 3, 2017

Well, ok, true, didn't think about this, but why not add the uniqueConstraint to the liquibase generation script then? I guess, you agree, that in some cases it is necessary to make sure on DB side, that a unique constraint is a final assurance to not have duplication, but we cannot generate that with JHipster JDL.

@taurus227
Copy link

taurus227 commented Nov 3, 2017

Yes, that's what I was thinking, to add a "unique" validation much like "required", which would just add the constraint for that column to the liquibase change log.

This would not handle multi-column unique constraints, for those we would need a separate constraint definition, at the same level as entity and relationship, similarly to how it can be done in liquibase. But I wouldn't worry about multi-column for now.

@taurus227
Copy link

taurus227 commented Nov 7, 2017

@MathieuAA Fine, then let's continue the discussion here.
Agreed, the syntax should be

entity A {
  mySuperImportantField String required unique,
  myOtherImportantField unique
}

but that only works for single-field unique constraints. For multi-field unique constraints, where a value-pair is supposed to be unique, we need the additional syntax. For example in a many-to-many relationship, in the relationship table we need to make sure that the same key-pair is not defined twice.

@deepu105 The unique constraint created by liquibase in the database will validate the values when saving, no need to do an additional select. Unique constraints are very important to ensure that there's no duplication. For example, cannot have two users with the same username.

@MathieuAA
Copy link
Member

@taurus227 point taken

@MathieuAA MathieuAA reopened this Nov 10, 2017
@deepu105
Copy link
Member

If you want to add a simple unique like required then its fine but then it would be nice if someone can PR it to the generator first as we clearly don't have the time at the moment

@MathieuAA
Copy link
Member

@deepu105 that's the point =) @taurus227 is working on it.

@delrotf
Copy link

delrotf commented Jan 25, 2018

Any update on this? can we now use the unique? Thanks.

@MathieuAA
Copy link
Member

As soon as I have time to do it, I'll do it. It will likely be today or this week-end.

@MathieuAA
Copy link
Member

What I'm about to do: create a branch with the necessary code to support the unique constraint. Once it's available, I'll merge it into master.

MathieuAA pushed a commit that referenced this issue Jan 27, 2018
@rifatdover
Copy link

Maybe PK will also be something useful.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants