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):
In the export/save dialog, expand
Select fields to export and their export options
and de-select the existingfid
from export (uncheck the box). QGIS will automatically create a newfid
-field with unique values.In the
export/save dialog
, expandLayer Options
and define a new name for thefid
likefid_2
to use this as new uniqe id-field. Unique values will be created automatically when you save.Delete the existing
fid
field of the temporary layer before attempting to save it as Geopackage. QGIS will automatically create a newfid
-field with unique values.Be aware: solutions 1 and 3 result in loosing the original
fid
-values - so if you intend to use the originalfid
again in the workflow (e.g. for joining), first create an additional attribute likeold_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 likeExplode lines
: one line-feature results in multiple features - one for each segment. Same withMultipart 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 samefid
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 existingfid
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 samefid
- this prevents QGIS from saving this layer as Geopackage:De-selecting the existing
fid
from export (above, solution 1) or re-naming the field that is used as uniqueid
to something else likefid_2
(below, solution 2):