MaxLength in Generate entities

Hi,
I have a lot of entites with 300-400 fields foreach;
I expected the [Column('City',,30)] attribute to be sufficient to throw an exception in Xdata (or Aurelius) in case the field value is a string longer than 30 characters. Or at least an option that automatically allows you to truncate to the maximum length or choose for an exception to be raised. It would be really necessary but it seems to me that there is none of this.
The only way is to add the [Maxlength] attribute to all strings. Quite long procedure for thousands of fields.
Is there some setting that I don't know about for this management?
And if there are no other systems and the [MaxLength] attribute has to be added for check string length, is it possible to have automatic generation of [MaxLength] when generating the entities from the AureliusConnection?
Thank you

1 Like

The Column attribute specifies the database-level settings. An exception will be raised in the end, but from the database server itself.

For application-level validation, yes, you need to add the MaxLength attribute.

You can't do that from the TAureliusConnection generator, but you can do that from Data Modeler using customization scripts.

Ok, but a simple extension (one check) in TAureliusConnection generator can
can make our life easier while now I have to go through thousands of fields by hand.
A few simple checks to automatically generate validations on the length of the strings or other simple automatic functions.

(In truth, using an ORM it is very illogical that validations on the maximum length of a string should be done at database level, with all the consequences of the case, and not at application level, before involving transactions towards the server. These basic validations should be by default)

Data Modeler unfortunately has its flaws (I cannot generate the autoincrement Identity key fields for PostgreSQL automatically in any way, for example) and could in fact generate entities in a much more refined and precise way at the attribute level (especially on validations and on DisplayName). I'll take a look at his customizations.
Thank you

I take this opportunity to ask for help:
how to set up Data Modeler or how to set ID in Aurelius to get a table with ID "GENERATED BY XX AS IDENTITY" in table creation, for PostgreSQL and for Firebird?
(I have to use the same entities for the 2 different db).
Thanks

In the meantime I integrated the Data modeler script and now I have [Maxlength(xx)] in each string field... :-)

Aurelius generates autoinc fields or Firebird if you use Firebird3 dialect. For PostgreSQL it will always use sequences to implement the auto increment fields.

Since PostgreSQL also makes extensive use of identity fields, is it possible to update these features for PostgreSQL as well as for Firebird 3?
(By the way, in PostgreSQL the Identity field has existed long before)
Clearly my goal is to have a single entity unit that works with different databases, without creating different code for each one.
Thank you

You can accomplish that, Aurelius will just use sequences for the auto increment behavior, but the code will be the same for Firebird and PostgreSQL.

Hi Wagner,
ok for Firebird, but I cannot obtain automatic sequence in PostgreSQL tables (release PostgreSQL 14.0).

Example:
[Entity]
[Table('f24_vers_dettagli')]
[Sequence('SEQ_f24_vers_dettagli')]
[Id('Funivocoid', TIdGenerator.IdentityOrSequence)]
Tf24_vers_dettagli_new = class
private
[Column('univocoid', TColumnProp.Required,TColumnProp.NoInsert,TColumnProp.NoUpdate])]
Funivocoid: Int64;
....
....

I have always error : "null value in column 'univocoid' of relation ''f24_vers_dettagli''.
What did I forget or what am I doing wrong?
Thanks

Remove the NoInsert property from the column, since the value must be inserted:

[Column('univocoid', TColumnProp.Required,TColumnProp.NoUpdate])]
Funivocoid: Int64;

I apologize, but I left Insert because I thought that the sequence assignment was carried out at database level and not at entity level.

I take advantage of the conversation:
it is possible to set the entity to automatically generate (via attributes) a column with TGuid value (better if SmartGuid). But without being the entity's ID key, already assigned by the sequence?
(In practice a double assignment of unique keys, one (entity ID) with the sequence, the other through the assignment of a Guid value?)
Thank you

You can always use the object-oriented programming techniques, that's the advantage of using an ORM. Why can't you simply generate such GUID value in the constructor of the entity class?

In any case, you might also try to use the OnInserting event which is fired before a new entity is created, and set the value there.

You can add the event handler at entity-level using attributes, as explained here.