#1 2025-10-06 17:13:19

FatimaEzzahraa14
Member
Registered: 2024-11-22
Posts: 11

mORMot: CreateMissingTables adds default ID despite existing varchar

I have a module that contains several classes mapped to database tables. When I use CreateMissingTables in mORMot, it creates the missing tables for the classes. However, there is an issue:

CreateMissingTables automatically adds an ID field to the tables it creates.

In my case, some classes already have an ID field defined as varchar, but CreateMissingTables still tries to add its default ID field as integer.

This causes a conflict because the table ends up with the wrong type for the ID field, or sometimes it fails to create the table correctly.

Goal:

I want CreateMissingTables to respect the existing ID fields in my classes and not automatically add an integer ID field, especially when my class already defines a varchar ID.

Offline

#2 2025-10-06 17:23:15

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,226
Website

Re: mORMot: CreateMissingTables adds default ID despite existing varchar

As documented, and as required by SQLite3 virtual tables process, the ORM requires an integer primary key for external tables, even if you don't use SQLite3.
Even the IRestOrm itself makes use of this "RowID" integer primary key.
So what you can do is map this "RowID" field to an existing integer field, to be used as primary key, perhaps in addition to the existing varchar ID. The ORM can populate the RowID field for you.

Offline

#3 Yesterday 14:02:17

FatimaEzzahraa14
Member
Registered: 2024-11-22
Posts: 11

Re: mORMot: CreateMissingTables adds default ID despite existing varchar

Thank you for the explanation.
I understand that the ORM requires an integer RowID primary key even for external tables.
However, could you please provide a small example showing how to define a class that uses a varchar ID field together with this RowID integer key?

For example:

How should I declare the class in Delphi?

How to map the RowID to an existing integer field in my external MySQL table?

A short code snippet or schema example would help me understand it better.

Offline

#4 Today 15:49:20

vs
Member
Registered: 2019-10-21
Posts: 49

Re: mORMot: CreateMissingTables adds default ID despite existing varchar

Please try

MyModel.Props[TMyTable].ExternalDB.MapField('ID', 'MY_TABLE_ID');

Offline

Board footer

Powered by FluxBB