Skip to content

Commit 7d79a73

Browse files
committedSep 2, 2015
Importing a layer, create an autoincremental integer pk if either no pk was passed or it's an integer field
1 parent 71afc9a commit 7d79a73

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed
 

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2939,14 +2939,7 @@ bool QgsPostgresProvider::convertField( QgsField &field, const QMap<QString, QVa
29392939
break;
29402940

29412941
case QVariant::Int:
2942-
if ( fieldPrec < 10 )
2943-
{
2944-
fieldType = "int4";
2945-
}
2946-
else
2947-
{
2948-
fieldType = "numeric";
2949-
}
2942+
fieldType = "int4";
29502943
fieldPrec = 0;
29512944
break;
29522945

@@ -3054,7 +3047,8 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
30543047
}
30553048
}
30563049

3057-
// if the field doesn't not exist yet, create it as a serial field
3050+
// if the pk field doesn't exist yet, create a serial pk field
3051+
// as it's autoincremental
30583052
if ( primaryKeyType.isEmpty() )
30593053
{
30603054
primaryKeyType = "serial";
@@ -3066,6 +3060,19 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
30663060
}
30673061
#endif
30683062
}
3063+
else
3064+
{
3065+
// if the pk field's type is one of the postgres integer types,
3066+
// use the equivalent autoincremental type (serialN)
3067+
if ( primaryKeyType == "int2" || primaryKeyType == "int4" )
3068+
{
3069+
primaryKeyType = "serial";
3070+
}
3071+
else if ( primaryKeyType == "int8" )
3072+
{
3073+
primaryKeyType = "serial8";
3074+
}
3075+
}
30693076

30703077
try
30713078
{

‎src/providers/spatialite/qgsspatialiteprovider.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,20 @@ QgsSpatiaLiteProvider::createEmptyLayer(
175175
}
176176
}
177177

178-
// if the field doesn't not exist yet, create it as a int field
178+
// if the pk field doesn't exist yet, create an integer pk field
179+
// as it's autoincremental
179180
if ( primaryKeyType.isEmpty() )
180181
{
181182
primaryKeyType = "INTEGER";
182-
#if 0 // TODO
183-
// check the feature count to choose if create a bigint pk field
184-
if ( layer->featureCount() > 0xFFFFFF )
185-
{
186-
primaryKeyType = "BIGINT";
187-
}
188-
#endif
183+
}
184+
else
185+
{
186+
// if the pk field's type is bigint, use the autoincremental
187+
// integer type instead
188+
if ( primaryKeyType == "BIGINT" )
189+
{
190+
primaryKeyType = "INTEGER";
191+
}
189192
}
190193

191194
try

0 commit comments

Comments
 (0)
Please sign in to comment.