Primary keys

Integral to most of these operations is an ID parameter used as the primary key in our table. To support the persistence of our entities using this new database controller, we need to add a property to our Entity class that uniquely identifies an instance of that entity.

In entity.cpp, add a member variable to Entity::Implementation:

QString id;

Then, initialize it in the constructor:

Implementation(Entity* _entity, IDatabaseController* _databaseController, const QString& _key)
    : entity(_entity)
    , databaseController(_databaseController)
    , key(_key)
    , id(QUuid::createUuid().toString())
{
}

When we instantiate a new Entity, we need to generate a new unique ID, and we use the QUuid class to this for us with the createUuid() ...

Get Learn Qt 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.