Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using "Add feature" to create feature and child feature with transaction group causes foreign key error #37222

Closed
jdugge opened this issue Jun 15, 2020 · 7 comments · Fixed by #37249
Assignees
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Relations

Comments

@jdugge
Copy link
Contributor

jdugge commented Jun 15, 2020

The QGIS documentation includes an example of creating many-to-many relationships with PostGIS and Geopackage databases, using transaction groups and two one-to-many relations on a junction table.

After working through the example using Geopackage in QGIS 3.12 a "foreign key error" occurs when trying to simultaneously create a new feature with a new child feature using the "Add record (Ctrl + .)" menu, see the GIF below:

add_record

This might be due to the fact that the id field of the new feature is not automatically filled when going through "Add record" (instead it reads "Autogenerate"). When adding a new feature using the attribute table the id is already filled in, and creating a new feature with a new child feature works without problems, see the GIF below:

add_new_feature

A QGIS project and Geopackage file for testing is attached:

many_to_many.zip

@jdugge jdugge added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Jun 15, 2020
@gioman
Copy link
Contributor

gioman commented Jun 15, 2020

The QGIS documentation includes an example of creating many-to-many relationships with PostGIS and Geopackage databases, using transaction groups and two one-to-many relations on a junction table.

@jdugge does it mean that in a previous QGIS release this example worked as documented?

@gioman gioman added the Feedback Waiting on the submitter for answers label Jun 15, 2020
@jdugge
Copy link
Contributor Author

jdugge commented Jun 15, 2020

I'm not sure if this exact action (creating a new feature using the "Add Record" tool and then creating a new child feature from the attribute form) ever worked. I tried this with QGIS 3.2, 3.6, 3.10 and 3.12, and current master, and the behaviour is always the same.

@gioman gioman removed the Feedback Waiting on the submitter for answers label Jun 15, 2020
@jdugge
Copy link
Contributor Author

jdugge commented Jun 15, 2020

Poking around the code, it seems the difference in behaviour stems from the fact that when using the "Add Record" tool a QgsAttributeDialog in AddFeatureMode is created which only applies default values as configured in the Layer Properties.
By contrast, using "Add Feature" in the Attribute Table calls the layer's addFeature function, which for a Geopackage pre-fetches the ID from the database

It might be more consistent and intuitive for the user if QgsMapToolAddFeature::addFeature always creates a new feature using the layer's addFeature method, regardless of whether the Attribute Form is shown or not. In case the Attribute Form is shown, the newly created feature can be applied to the form, and if the user cancels the form, the feature creation could be undone using the edit buffer.

@elpaso
Copy link
Contributor

elpaso commented Jun 16, 2020

@jdugge is there another bug here (even if this issue was fixed)? When the user adds a new feature from the top menu (like in GIF 1) there is a "Cancel" button in the dialog, but it has no effect (due to the transaction).

@elpaso
Copy link
Contributor

elpaso commented Jun 16, 2020

@jdugge I'd appreciate if you could test my patch, if you are on windows you'll find a built test package in a about an hour and a half (search for "Artifact"): https://github.com/qgis/QGIS/actions/runs/137201841

@jdugge
Copy link
Contributor Author

jdugge commented Jun 16, 2020

@elpaso: Excellent, that works great!
One minor thing: Clicking "Add feature" now immediately puts the layer into dirty mode, and it remains dirty even if the dialog is closed by clicking cancel.

@elpaso
Copy link
Contributor

elpaso commented Jun 16, 2020

Thanks for testing, I'm preparing a new patch with a different and possibly cleaner approach (that requires ticking the checkbox "Evaluate on provider side", the same way as postgis is supposed to work.

In any event I won't be able to fix the "dirty project" issue, for GPKG at least the evaluation has the side effect that the sequence is actually incremented and due to the fact that's inside the GDAL/OGR library and I cannot get an handle to the sqlite DB so I cannot add the increment inside the transaction, that means that the sequence will be permanently incremented each time a feature is added, even if it's not saved.

elpaso added a commit to elpaso/QGIS that referenced this issue Jun 17, 2020
for PK default value, fixes qgis#37222

Also, fix dangling transactions for spatialite.
nyalldawson pushed a commit that referenced this issue Jun 19, 2020
for PK default value, fixes #37222

Also, fix dangling transactions for spatialite.
espinafre pushed a commit to espinafre/QGIS that referenced this issue Jun 19, 2020
espinafre pushed a commit to espinafre/QGIS that referenced this issue Jun 19, 2020
for PK default value, fixes qgis#37222

Also, fix dangling transactions for spatialite.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Relations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants