Skip to content

Commit 6d4ae25

Browse files
authored
Merge pull request #9258 from nyalldawson/fix_mapping_table_not_in_contents
[gpkg] Ensure that mapping tables are inserted into gpkg_contents
2 parents d9094ce + 0cf5e7d commit 6d4ae25

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

autotest/ogr/ogr_gpkg.py

+10
Original file line numberDiff line numberDiff line change
@@ -7260,6 +7260,16 @@ def get_query_row_count(query):
72607260
== 1
72617261
)
72627262

7263+
# validate mapping table was created
7264+
assert get_query_row_count("SELECT * FROM 'origin_table_dest_table'") == 0
7265+
# validate mapping table is present in gpkg_contents
7266+
assert (
7267+
get_query_row_count(
7268+
"SELECT * FROM gpkg_contents WHERE table_name='origin_table_dest_table' AND data_type='attributes'"
7269+
)
7270+
== 1
7271+
)
7272+
72637273
lyr = ds.CreateLayer("origin_table2", geom_type=ogr.wkbNone)
72647274
fld_defn = ogr.FieldDefn("o_pkey", ogr.OFTInteger)
72657275
assert lyr.CreateField(fld_defn) == ogr.OGRERR_NONE

doc/source/drivers/vector/gpkg.rst

+6
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ only be updated to change their base or related table fields, or the relationshi
177177
table type. It is not permissible to change the base or related table itself, or the mapping
178178
table details. If this is desired then a new relationship should be created instead.
179179

180+
Note that when a many-to-many relationship is created in a GeoPackage, GDAL will always
181+
insert the mapping table into the gpkg_contents table. Formally this is not required
182+
by the Related Tables Extension (instead, the table should only be listed in gpkgext_relations),
183+
however failing to list the mapping table in gpkg_contents prevents it from being usable
184+
in some other applications (e.g. ESRI software).
185+
180186
Dataset open options
181187
--------------------
182188

ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -9986,6 +9986,35 @@ bool GDALGeoPackageDataset::AddRelationship(
99869986
return false;
99879987
}
99889988

9989+
/*
9990+
* Strictly speaking we should NOT be inserting the mapping table into gpkg_contents.
9991+
* The related tables extension explicitly states that the mapping table should only be
9992+
* in the gpkgext_relations table and not in gpkg_contents. (See also discussion at
9993+
* https://github.com/opengeospatial/geopackage/issues/679).
9994+
*
9995+
* However, if we don't insert the mapping table into gpkg_contents then it is no longer
9996+
* visible to some clients (eg ESRI software only allows opening tables that are present
9997+
* in gpkg_contents). So we'll do this anyway, for maximum compatiblity and flexibility.
9998+
*
9999+
* More related discussion is at https://github.com/OSGeo/gdal/pull/9258
10000+
*/
10001+
pszSQL = sqlite3_mprintf(
10002+
"INSERT INTO gpkg_contents "
10003+
"(table_name,data_type,identifier,description,last_change,srs_id) "
10004+
"VALUES "
10005+
"('%q','attributes','%q','Mapping table for relationship between "
10006+
"%q and %q',%s,0)",
10007+
osMappingTableName.c_str(), /*table_name*/
10008+
osMappingTableName.c_str(), /*identifier*/
10009+
osLeftTableName.c_str(), /*description left table name*/
10010+
osRightTableName.c_str(), /*description right table name*/
10011+
GDALGeoPackageDataset::GetCurrentDateEscapedSQL().c_str());
10012+
10013+
// Note -- we explicitly ignore failures here, because hey, we aren't really
10014+
// supposed to be adding this table to gpkg_contents anyway!
10015+
(void)SQLCommand(hDB, pszSQL);
10016+
sqlite3_free(pszSQL);
10017+
998910018
pszSQL = sqlite3_mprintf(
999010019
"CREATE INDEX \"idx_%w_base_id\" ON \"%w\" (base_id);",
999110020
osMappingTableName.c_str(), osMappingTableName.c_str());

0 commit comments

Comments
 (0)