From b5a1dd8f70a85cf8788a683a4b33dd5295aba0a0 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Sat, 21 Jul 2018 16:20:38 -0400 Subject: [PATCH] Fix copy table for empty table --- util/migrate/table_ops.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/util/migrate/table_ops.py b/util/migrate/table_ops.py index 83abd630b..634e84287 100644 --- a/util/migrate/table_ops.py +++ b/util/migrate/table_ops.py @@ -2,10 +2,12 @@ def copy_table_contents(source_table, destination_table, conn): if conn.engine.name == 'postgresql': conn.execute('INSERT INTO "%s" SELECT * FROM "%s"' % (destination_table, source_table)) result = list(conn.execute('Select Max(id) from "%s"' % destination_table))[0] - new_start_id = result[0] + 1 - conn.execute('ALTER SEQUENCE "%s_id_seq" RESTART WITH %s' % (destination_table, new_start_id)) + if result[0] is not None: + new_start_id = result[0] + 1 + conn.execute('ALTER SEQUENCE "%s_id_seq" RESTART WITH %s' % (destination_table, new_start_id)) else: conn.execute("INSERT INTO `%s` SELECT * FROM `%s` WHERE 1" % (destination_table, source_table)) result = list(conn.execute('Select Max(id) from `%s` WHERE 1' % destination_table))[0] - new_start_id = result[0] + 1 - conn.execute("ALTER TABLE `%s` AUTO_INCREMENT = %s" % (destination_table, new_start_id)) + if result[0] is not None: + new_start_id = result[0] + 1 + conn.execute("ALTER TABLE `%s` AUTO_INCREMENT = %s" % (destination_table, new_start_id))