QGIS Error – Fixing ‘Unique Constraint Failed’ Error When Saving GeoPackage in QGIS

errorgeopackageqgissaveunique id

In QGIS, when using Processing tools, the temporary output layer in many cases cannot be saved as Geopackage. This happens for example when using "Explode lines" or "Multipart to Single parts", but using other tools as well. When trying to save the output to a GeoPackage, an error message returns:

Could not make temporary scratch layer permanent. Error: Feature
write errors: Feature creation error (OGR error: failed to execute
insert :UNIQUE constraint failed: temperror.fid) Only 1 of [3]
features written.

What causes this error and how to save such a layer or make it permanent as a GeoPackage file?

Best Answer

The short answer: how to solve the problem

There are at least three possibilities (screenshot for solution 1 and 2 at the bottom):

  1. In the export/save dialog, expand Select fields to export and their export options and de-select the existing fid from export (uncheck the box). QGIS will automatically create a new fid-field with unique values.

  2. In the export/save dialog, expand Layer Options and define a new name for the fid like fid_2 to use this as new uniqe id-field. Unique values will be created automatically when you save.

  3. Delete the existing fid field of the temporary layer before attempting to save it as Geopackage. QGIS will automatically create a new fid-field with unique values.

Be aware: solutions 1 and 3 result in loosing the original fid-values - so if you intend to use the original fid again in the workflow (e.g. for joining), first create an additional attribute like old_fid where you copy the values of the original features.

What causes the error

The error has to do that the SQlite- (database-) based Geopackage format requires an unique ID for each feature. As a default, the attribute named fid is used for that.

The problem now is if you have an input layer that already contains an attribute called fid (which normally is the case if the layer's source is a Geopackage) and some features of this layer are split up. This is the case with many processing tool like Explode lines: one line-feature results in multiple features - one for each segment. Same with Multipart to Single parts: one feature conaining differnt parts is split up to several features - one for each part. The attributes of the newly created features are copied from the orginal layer. So you suddenly have several features with the same fid field.

Like this, the unique constrained that defines each feature must have it's own, unique fid value is violated and the Geopackage cannot be properly generated. That's why you either have to define a new name for the uniqe-id-field or delete / re-create new values for the existing fid field.

Multipart-polygon split up in singe-part polygons: the fid-attribute is taken form the single feature of the original polygon so that all three polygons in the new layer have the same fid - this prevents QGIS from saving this layer as Geopackage: enter image description here

De-selecting the existing fid from export (above, solution 1) or re-naming the field that is used as unique id to something else like fid_2(below, solution 2): enter image description here

Related Question