BESSER-PEARL / BESSER

A Python-based low-modeling low-code platform for smart software
https://besser.readthedocs.io/en/latest/
MIT License
46 stars 11 forks source link

Define unique primary keys in SQL / SQLAlchemy generator #108

Open Aran30 opened 3 weeks ago

Aran30 commented 3 weeks ago

Currently, the default primary key will always be an auto-generated ID. There is no possibility to set other attributes as primary key.

Aran30 commented 2 weeks ago

Beyond primary keys, there is currently no possibility to set attributes as unique (without them being a primary key). The question is, should B-UML support setting properties as unique, even setting combination of properties as unique?

Aran30 commented 2 weeks ago

UML and B-UML support the setting of attributes as "ID", yet there does not seem to be a differentiation between unique and Primary Key like in SQL. Should our DB generators just treat B-UML's "ID" as unique and always create an integer primary key? (Setting anything besides an integer as primary key does not seem to be best practice) @jcabot @ivan-alfonso any thoughts?

ivan-alfonso commented 2 weeks ago

I can think of three solutions for dealing with unique fields in these generators, but I'm not sure which one is most efficient for this case:

  1. Currently an OCL constraint could be written to specify the attributes that are unique to a class. For example,
context Person
inv UniqueDNI: Person.allInstances()->isUnique(dni)

Then the code generator would have to take as input the list of OCL constraints, and process them.

  1. Another solution would be to add the boolean parameter is_unique to the Property class of our B-UML metamodel. But I don't know if it is worth it just for this property that would be used by some code generators (the UML specification does not consider this attribute in the way we are describing it).

  2. You could also send to the code generator a dictionary specifying which attributes are unique, and modify the code generator to process this dictionary when the user needs to define unique attributes in the DB.

jcabot commented 2 weeks ago

Honestly, while I agree our solution is not ideal (as we can only indicate the pk and not other unique keys) , I think it's enough for now. This is one of those things that if there is anybody really interested they could easily contribute