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

ogr2ogr: Conversion from PostGIS to GeoPackage fails when joined values are NULL #1734

Closed
ilosuna opened this issue Jul 22, 2019 · 0 comments
Milestone

Comments

@ilosuna
Copy link

ilosuna commented Jul 22, 2019

Expected behavior and actual behavior

I want to create a GeoPackage from a PostGIS database by using ogr2ogr with an SQL query. The query contains JOINs that might return NULL values so I would expect a GeoPackage table with NULL values. However, an error occurs in case NULL is returned:

ERROR 1: failed to execute insert : NOT NULL constraint failed: test.type
ERROR 1: Unable to write feature 1 from layer sql_statement.
ERROR 1: Terminating translation prematurely after failed
translation from sql statement.
ERROR 1: sqlite3_exec(CREATE TRIGGER "trigger_insert_feature_count_test" AFTER INSERT ON "test" BEGIN UPDATE gpkg_ogr_contents SET feature_count = feature_count + 1 WHERE lower(table_name) = lower('test'); END;) failed: no such table: main.test
ERROR 1: sqlite3_exec(CREATE TRIGGER "trigger_delete_feature_count_test" AFTER DELETE ON "test" BEGIN UPDATE gpkg_ogr_contents SET feature_count = feature_count - 1 WHERE lower(table_name) = lower('test'); END;) failed: no such table: main.test

Steps to reproduce the problem

My database structure and data:

CREATE TABLE point_type (
    id serial PRIMARY KEY,
    name CHARACTER VARYING NOT NULL
);

CREATE TABLE point (
    id serial primary key,
    name character varying NOT NULL,
    fk_point_type_id INTEGER REFERENCES point_type (id) ON DELETE SET NULL,
    geom geometry(Point,4326)
);

INSERT INTO point_type (name) VALUES ('A');
INSERT INTO point_type (name) VALUES ('B');
INSERT INTO point_type (name) VALUES ('C');

INSERT INTO point (name, fk_point_type_id, geom) VALUES ('Zero/Zero', 2, ST_GeomFromText('POINT(0 0)', 4326));
INSERT INTO point (name, fk_point_type_id, geom) VALUES ('One/One', NULL, ST_GeomFromText('POINT(1 1)', 4326));

Now I can query my points with their types...
SELECT point.name, point_type.name AS type, geom FROM point LEFT JOIN point_type ON point_type.id=point.fk_point_type_id;

...and get this result:

name type geom
Zero/Zero B 0101000020E610...
One/One [null] 0101000020E610...

If I pass this query to ogr2ogr I get the above mentioned error:

ogr2ogr -f "GPKG" test.gpkg PG:"dbname='gdaltest' host='localhost' port=5432 user='gdaltest' password='gdaltest'" -nln 'test' -sql "SELECT point.name, point_type.name AS type, geom FROM point LEFT JOIN point_type ON point_type.id=point.fk_point_type_id;"

Operating system

Ubuntu 18.04.1 LTS 64 bit

GDAL version and provenance

GDAL 2.2.3 from the Ubuntu bionic sources

rouault added a commit that referenced this issue Jul 22, 2019
rouault added a commit that referenced this issue Jul 22, 2019
@rouault rouault added this to the 2.4.3 milestone Jul 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants