qgis / QGIS

QGIS is a free, open source, cross platform (lin/win/mac) geographical information system (GIS)
https://qgis.org
GNU General Public License v2.0
10.37k stars 2.98k forks source link

Attributes Form > Field > Default value: Unexcepted results when pasting in multiple features at once #51123

Open etaunknown opened 1 year ago

etaunknown commented 1 year ago

What is the bug or the crash?

Background

My goal was to have an unique ID field that was required for label positioning. In this case, a simple auto-incremental field on a shapefile.

The best solution I could find was to use an custom ID field and to set a default value on this.

if (maximum("id") is NULL, 1 ,maximum("id") + 1)

Issue

Expected results were that this field would be updated on each additional element and be incremented by one every time.

Actual results were generally very good, unless pasting multiple items at once.

Without constraints, the fields would all be added with the same ID. ID 23 23 23

With constraints, forcing unique values produced ID NULL 23 24

A workaround exists in that you add a NULL value to the shapefile. Afterwards, paste works as expected. ID 23 24 25

Steps to reproduce the issue

  1. Create two point layers, Src and Dest
  2. Add a new ID field on Dest
  3. Add the following Attributes Form > Field > Default value expression. if (maximum("ID") is NULL, 1 ,maximum("ID") + 1)
  4. Add three or more points in Src layer and copy / paste these into Dest
  5. View the attributes table to see 1, 1, 1 rather than the expected results of 1, 2, 3

Delete the points and repeat using the field unique constraint to see the alternative behaviour of NULL, 1, 2

Versions

QGIS version 3.24.1-Tisler QGIS code revision 5709b82461 Qt version 5.15.2 Python version 3.9.5 GDAL/OGR version 3.4.2 PROJ version 9.0.0 EPSG Registry database version v10.054 (2022-02-13) GEOS version 3.10.2-CAPI-1.16.0 SQLite version 3.38.1 PDAL version 2.3.0 PostgreSQL client version unknown SpatiaLite version 5.0.1 QWT version 6.1.3 QScintilla2 version 2.11.5 OS version Windows 10 Version 2009

Active Python plugins AusMap 1.1 FreehandRasterGeoreferencer 0.8.3 GeometryShapes 0.7 joinmultiplelines Version 0.4.1 latlontools 3.6.6 qchainage 3.0.1 QuickOSM 2.1.1 quick_map_services 0.19.29 raster_tracer 0.3.2 shapetools 3.4.9 db_manager 0.1.20 grassprovider 2.12.99 MetaSearch 0.3.6 processing 2.12.99 sagaprovider 2.12.99

Supported QGIS version

New profile

Additional context

No response

DelazJ commented 1 year ago

Confirmed on ed3ad0430f

nicogodet commented 1 year ago

As workaround, work with geopackage that has a specific field "fid" for your needs (unique integer as key) and export as shapefile when done.