Search code examples
ormlucee

Lucee ORM - Duplicate entry on primary key


I have been working with Lucee (coldfusion) for some time now and I can't resolve a certain issue we're having. We use the ORM framework with entities to work with data. It's all working nicely except one thing:

We have an external API that doesn't use the ORM framework. I use mysql queries to insert a row in a table. Works all fine and dandy, but if I go to our ORM application and try to save a second row then I get a "Duplicate Entry for key PRIMARY" error.

So it's clear to me the ORM framework isn't up to date with the externally added row. How do I tell ORM to reload this data? ORMReload() is very slow so I don't want to use that (and that's for the whole structure). EntityReload isn't working either.

A simple example on how I use the ORM framework:

var simple_item = entityNew("item");
entitySave(simple_item);
ormFlush();

So anyone got an idea how I can fix this? Thanks in advance!

P.S. Currently I set the primary key with a function inside the entity instead of using generator="increment" to make it work, but that's just a workaround.


Solution

  • Currently I set the primary key with a function inside the entity instead of using generator="increment"

    Are you sure "increment" is the appropriate generator algorithm? From the Adobe docs, which also apply to Lucee ORM (my emphasis):

    increment: This algorithm generates identifiers of type long, short, or int by incrementing a counter maintained by ORM. This is commonly used when auto-generation for the primary key is not enabled in the table and you want ORM to generate the primary key. This should be used when a single instance of ColdFusion is the only process to insert data into the table.

    If a non-ORM process can also insert data then you should be letting the database handle primary key increments natively and instead use

    generator="native"