I need to create a unique id when adding an object to a shapefile (point, line, polygon).

I read a lot of things about that, but stay sceptic when using it.

I presume static field can not be envisaged for that so I went to virtual field. I put a formula (like $id) in it and the system can generate a unique number.

The problem starts when I delete an entity, the system recalculates the virtual field everywhere, and so changes the unique number previously generated for some of them.

That's not what I want. I want that the unique id is generated at creation, but remains UNCHANGED, whatever is happening later (like in a SQL database).

Is there an easy way to acheve this?

  • 1
    Have you seen the Add autoincremental field tool?
    – Matt
    Commented Mar 1, 2022 at 11:17
  • I did not know what you suggested, so I had a look. I am a little bit puzzled with this process: storing the primary key I want to create for a table in another table is puzzling my understanding of datamodeling. I wonder how the join is made between those tables : is there a hidden unique identifier somewhere?
    – ClaudeVer
    Commented Mar 1, 2022 at 16:50

2 Answers 2


The problem is that not all data storages support this functionality. For example, geopackage supports it by default, but shapefile does not.

You can try using uuid() function to autofill the field in a shapefile, but I'm not sure if the previously entered unique value won't be repeated after deletion.

uuid() description:

Generates a Universally Unique Identifier (UUID) for each row using the Qt QUuid::createUuid method.

enter image description here

  • I am realy looking with the behaviour you show for the geopackage,.... but with a shapefile.... the unique identifier is not realy usable if you want to identify for exemple a tree, and ask somebody discovering a problem with this tree make reference to such a key! I am expecting something more simple, like in the geopackage exemple. Thanks anyway
    – ClaudeVer
    Commented Mar 1, 2022 at 17:03
  • 1
    @ClaudeVer I don't see any way to implement this behavior for shapefile, because this file type is obsolete and has very limited functionality. Why is it necessary to use a shapefile? Commented Mar 1, 2022 at 19:25
  • Good question! A just used QGis for 6 months, as a stand alone system, for a citizen initiative. I followed different youtube video to understantd how to use QGis and in fact never heard about something else than shapefiles to handle points and lines on a map! What alternative solution are you thinking about?
    – ClaudeVer
    Commented Mar 3, 2022 at 7:03

The behavior that you are experiencing, ClaudeVer, is to be expected. Please allow me to elaborate:

Every spatial database table (Shapefile, Geopackage, Geodatabase...) contains an internal id field (typically named FID or OBJECTID) which contains unique values, and is maintained by the software without user intervention. The values in this internal field are dynamic; that is, the software will change them without notice. For example, every time a shapefile is edited, its internal id field is renumbered - as you have witnessed. In your example, the formula $id looks to the internal id field, so naturally its output to the virtual field will change every time the table is edited.

Because such internal id fields may change with time, they should never be relied on as a source for a static unique id.

There are several ways to create a static id field. A simple, albeit tedious, way would be to create a new integer field in the table and manually enter your unique values (1, 2, 3...). If you want to avoid such manual editing, I'm aware of two automated ways to create a static id field. The first, previously mentioned by Matt, is the Processing > Toolbox > Vector Table > Add Autoincremental Field tool. The second is the plugin Sort and Number.

  • the other solution proposing a default value with a formula in it like "if (maximum("uid") is NULL, 1000 ,maximum("uid") + 1", was realy what i was expecting. I tested it and works exactly as expected.. UUID could be a valid as far as you do not need a human readable id. Autoincremental seems to me too complex and I suspect difficult to use later in the processes.
    – ClaudeVer
    Commented Mar 4, 2022 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.