#1 2016-07-15 12:18:39

Bo
Member
From: Melbourne
Registered: 2016-07-04
Posts: 57
Website

ORM on legacy database

I am trying to apply ORM on a legacy database.

First, I tried manually create a TSQLRecord descendant for a table in the database, CreateMissingTable did not report any error but the ID column was not added to the table.
But if I remove one existed column from that table, CreateMissingTable added the column back to the table but still had no ID column.

Then found this post http://synopse.info/forum/viewtopic.php?id=2981, in the comment of following generated code, it does say the ORM will add ID to the table:

type
  /// totalsSent Table
  // - type definition auto-generated by SynDBExplorer 1.18.2765 at 2016-07-15 21:58:23
  // from totalsSent
  // - note that the ORM will add one missing ID field via:
  // $ ALTER TABLE totalsSent ADD ID INTEGER NOT NULL PRIMARY KEY
  TSQLtotalsSent = class(TSQLRecord)
  protected
    fdate: Double;
    fattachments: Int64;
    ferrors: Int64;
  published
    /// match totalsSent.date [FLOAT]
    property date: Double read fdate write fdate;
    /// match totalsSent.attachments [INTEGER]
    property attachments: Int64 read fattachments write fattachments;
    /// match totalsSent.errors [INTEGER]
    property errors: Int64 read ferrors write ferrors;
  end;

...

My CreateMissingTable code:

  LModel := TSQLModel.Create([TSQLproperties,TSQLtotalsSent]);
  LDB := TSQLRestServerDB.Create(LModel,'status.sqlite1');
  LDB.CreateMissingTables(1);
...

So what have I done wrong? How can I get it to create ID by the model so that I don't need to manually create one for each table.

Last edited by Bo (2016-07-19 22:35:47)

Offline

#2 2016-07-17 03:07:32

Bo
Member
From: Melbourne
Registered: 2016-07-04
Posts: 57
Website

Re: ORM on legacy database

I have checked the code in CreateMissingTables. It creates the table if the table does not exist, the script for creating a new table includes column ID, but it only goes through published fields of TSQLRecord if the table is already existed in the database, and ID is not a published field in the TSQLRecord, thus it won't add column ID into a existed table.

I am going to add a method TSQLRestServerDB.CreateMissingIDs to add ID column to tables for my legacy database.

Offline

Board footer

Powered by FluxBB