Skip to content

Commit 3470b1d

Browse files
committed
Make PostgreSQL lowercaseFieldNames option behave correctly with primary key field names
1 parent 7772042 commit 3470b1d

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,6 +2961,13 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
29612961
throw PGException( result );
29622962
}
29632963

2964+
if ( options->contains( "lowercaseFieldNames" ) && options->value( "lowercaseFieldNames" ).toBool() )
2965+
{
2966+
//convert primary key name to lowercase
2967+
//this must happen after determining the field type of the primary key
2968+
primaryKey = primaryKey.toLower();
2969+
}
2970+
29642971
sql = QString( "CREATE TABLE %1(%2 %3 PRIMARY KEY)" )
29652972
.arg( schemaTableName )
29662973
.arg( quotedIdentifier( primaryKey ) )
@@ -3040,14 +3047,11 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
30403047
for ( int fldIdx = 0; fldIdx < fields.count(); ++fldIdx )
30413048
{
30423049
QgsField fld = fields[fldIdx];
3043-
if ( fld.name() == primaryKey )
3044-
{
3045-
oldToNewAttrIdxMap->insert( fldIdx, 0 );
3046-
continue;
3047-
}
30483050

30493051
if ( fld.name() == geometryColumn )
30503052
{
3053+
//the "lowercaseFieldNames" option does not affect the name of the geometry column, so we perform
3054+
//this test before converting the field name to lowercase
30513055
QgsDebugMsg( "Found a field with the same name of the geometry column. Skip it!" );
30523056
continue;
30533057
}
@@ -3058,6 +3062,12 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
30583062
fld.setName( fld.name().toLower() );
30593063
}
30603064

3065+
if ( fld.name() == primaryKey )
3066+
{
3067+
oldToNewAttrIdxMap->insert( fldIdx, 0 );
3068+
continue;
3069+
}
3070+
30613071
if ( !convertField( fld, options ) )
30623072
{
30633073
if ( errorMessage )

0 commit comments

Comments
 (0)