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?

1 Answer 1


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

  • I am curious why this "non-unique" fid problem is specific to Geopackage layers. It does not happen with other data storage formats, such as shapefiles or FGDB feature classes.
    – Stu Smith
    Commented Jun 6, 2021 at 15:14
  • 1
    That's because Geopackage is based on SQLite, thus it is a database format,what is not the case with shapefiles - however I don't know the technical details however and the differences with File Geodata bases. For geopackage, there might be some information here: geopackage.org
    – Babel
    Commented Jun 6, 2021 at 15:20
  • 3
    Shapefile does not have a real feature id attribute. The features are stored sequentially and for the first feature the fid=1, for the second fid=2 etc. So non-unique fids cannot occur.
    – user30184
    Commented Jun 6, 2021 at 18:04
  • 1
    What if the tools that create this problem are re-coded to output a new unique fid, as well as the original (non-unique) fid?
    – Stu Smith
    Commented Jun 7, 2021 at 21:03

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.