Skip to content

Commit

Permalink
Make PostgreSQL lowercaseFieldNames option behave correctly with prim…
Browse files Browse the repository at this point in the history
…ary key field names
  • Loading branch information
nyalldawson committed May 20, 2014
1 parent 7772042 commit 3470b1d
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -2961,6 +2961,13 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
throw PGException( result );
}

if ( options->contains( "lowercaseFieldNames" ) && options->value( "lowercaseFieldNames" ).toBool() )
{
//convert primary key name to lowercase
//this must happen after determining the field type of the primary key
primaryKey = primaryKey.toLower();
}

sql = QString( "CREATE TABLE %1(%2 %3 PRIMARY KEY)" )
.arg( schemaTableName )
.arg( quotedIdentifier( primaryKey ) )
Expand Down Expand Up @@ -3040,14 +3047,11 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
for ( int fldIdx = 0; fldIdx < fields.count(); ++fldIdx )
{
QgsField fld = fields[fldIdx];
if ( fld.name() == primaryKey )
{
oldToNewAttrIdxMap->insert( fldIdx, 0 );
continue;
}

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

if ( fld.name() == primaryKey )
{
oldToNewAttrIdxMap->insert( fldIdx, 0 );
continue;
}

if ( !convertField( fld, options ) )
{
if ( errorMessage )
Expand Down

0 comments on commit 3470b1d

Please sign in to comment.