OneToOne wihtout unique

Hi all,
I start a new project at GitHub - florintanasa/taxonomy: Plant taxonomy database application develop in Cuba platform , this is a Taxonomy after ITIS model https://itis.gov/

Why the foreign key for one-to-one is the same for foreign key for one-to-many:
one-to-one

-- begin TAXONOMY_LONGNAMES
alter table TAXONOMY_LONGNAMES add constraint FK_TAXONOMY_LONGNAMES_ON_ID_TAXONOMIC_UNITS foreign key (ID_TAXONOMIC_UNITS_ID) references TAXONOMY_TAXONOMIC_UNITS(ID)^
create index IDX_TAXONOMY_LONGNAMES_ON_ID_TAXONOMIC_UNITS on TAXONOMY_LONGNAMES (ID_TAXONOMIC_UNITS_ID)^
-- end TAXONOMY_LONGNAMES

one-to-many

-- begin TAXONOMY_GEOGRAPHIC_DIV
alter table TAXONOMY_GEOGRAPHIC_DIV add constraint FK_TAXONOMY_GEOGRAPHIC_DIV_ON_ID_TAXONOMIC_UNITS foreign key (ID_TAXONOMIC_UNITS_ID) references TAXONOMY_TAXONOMIC_UNITS(ID)^
create index IDX_TAXONOMY_GEOGRAPHIC_DIV_ON_ID_TAXONOMIC_UNITS on TAXONOMY_GEOGRAPHIC_DIV (ID_TAXONOMIC_UNITS_ID)^
-- end TAXONOMY_GEOGRAPHIC_DIV

Is not necessary a unique for ID_TAXONOMIC_UNITS_ID field for one-to-one?
I saw also this aspect for SAMPLE_CUSTOMER table for sample https://demo2.cuba-platform.com/model/#!

I try to force added option unique = true in Longnames entity, like this :

 @NotNull
    @Composition
    @OnDelete(DeletePolicy.CASCADE)
    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "ID_TAXONOMIC_UNITS_ID", unique = true)
    private Taxonomic_units id_taxonomicUnits;

but I not see any constraint unique to add in sql files.

Also look at relation image:

Thanks in advanced!

Hi,
You can create a unique index if you want to maintain consistency on database level. It can be declared manually with @Index JPA annotation, or with help of Entity Designer in Studio.

P. S. CUBA is considered a legacy product, it isn’t a great idea to use it for starting a new project. Consider using Jmix instead: Get Started - Jmix

1 Like

Thank you Alexander for your answer,
Now, I added constraint manually for table taxonomy_longnames in database alter table taxonomy_longnames add constraint uq_id_taxonomic_units_id unique(id_taxonomic_units_id);, after that appear ok, look:

P.S. I started the project in Cuba because I also have others projects in Cuba and I need taxonomy in this started projects. In this moment I requested funds to management to acquire Jmix Studio after that I have in plan to export the projects in Jmix. But is not very easy, because we are non profit company.

Thanks,
I do it what you say:

@Table(name = "TAXONOMY_LONGNAMES",
            indexes = @Index(name = "index_ID_TAXONOMIC_UNITS_ID", columnList = "ID_TAXONOMIC_UNITS_ID", unique = true))
@Entity(name = "taxonomy_Longnames")

and after that I have two unique index, one with my name another with “automatic name”:

-- begin TAXONOMY_LONGNAMES
alter table TAXONOMY_LONGNAMES add constraint FK_TAXONOMY_LONGNAMES_ON_ID_TAXONOMIC_UNITS foreign key (ID_TAXONOMIC_UNITS_ID) references TAXONOMY_TAXONOMIC_UNITS(ID)^
create unique index IDX_TAXONOMY_LONGNAMES_UK_ID_TAXONOMIC_UNITS_ID on TAXONOMY_LONGNAMES (ID_TAXONOMIC_UNITS_ID) where DELETE_TS is null ^
create unique index INDEX_ID_TAXONOMIC_UNITS_ID on TAXONOMY_LONGNAMES (ID_TAXONOMIC_UNITS_ID) where DELETE_TS is null ^
create index IDX_TAXONOMY_LONGNAMES_ON_ID_TAXONOMIC_UNITS on TAXONOMY_LONGNAMES (ID_TAXONOMIC_UNITS_ID)^
-- end TAXONOMY_LONGNAMES