From 0fbcc6994a81416bc42199f11de1ad06540bbe48 Mon Sep 17 00:00:00 2001 From: Vraj Mohan Date: Thu, 28 Jul 2016 20:40:28 -0400 Subject: [PATCH 1/2] Use id for primary key name `index` seems to be an unusual name for a key --- test_load_table.py | 10 +++++----- utils.py | 5 +++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/test_load_table.py b/test_load_table.py index 624abd1..8c8504f 100644 --- a/test_load_table.py +++ b/test_load_table.py @@ -51,12 +51,12 @@ def test_good_csv(self, tmpdir): assert 'people' in self.meta.tables people = self.meta.tables['people'] - assert_column_type(people.columns['index'], int) + assert_column_type(people.columns['id'], int) assert_column_type(people.columns['name'], str) assert_column_type(people.columns['dob'], str) assert_column_type(people.columns['number_of_pets'], str) - assert people.columns['index'].primary_key is True + assert people.columns['id'].primary_key is True assert people.columns['name'].primary_key is False assert_columns_have_non_unique_indexes(self.engine, 'people', 'name', @@ -64,7 +64,7 @@ def test_good_csv(self, tmpdir): connection = self.engine.connect() results = connection.execute( - select([people]).order_by(people.c.index)).fetchall() + select([people]).order_by(people.c.id)).fetchall() assert (0, 'Tom', '1980-02-26', '0') == results[0] assert (1, 'Dick', '1982-03-14', '3') == results[1] assert (2, 'Harry', '1972-11-24', '2') == results[2] @@ -82,7 +82,7 @@ def test_load_with_chunking(self, tmpdir): connection = self.engine.connect() results = connection.execute( - select([people]).order_by(people.c.index)).fetchall() + select([people]).order_by(people.c.id)).fetchall() assert (0, 'Tom', '1980-02-26', '0') == results[0] assert (1, 'Dick', '1982-03-14', '3') == results[1] assert (2, 'Harry', '1972-11-24', '2') == results[2] @@ -120,7 +120,7 @@ def test_null_empty_string_handling(self, tmpdir): connection = self.engine.connect() results = connection.execute( - select([people]).order_by(people.c.index)).fetchall() + select([people]).order_by(people.c.id)).fetchall() assert (0, 'Tom', '0') == results[0] assert (1, None, '3') == results[1] assert (2, None, '2') == results[2] diff --git a/utils.py b/utils.py index 3e5c1f6..c01e4af 100644 --- a/utils.py +++ b/utils.py @@ -96,11 +96,12 @@ def load_table(filename, skipinitialspace=True) for idx, chunk in enumerate(chunks): chunk.index += chunksize * idx + chunk.index.name = 'id' to_sql(tablename, engine, chunk, chunksize=chunksize, - keys='index', + keys='id', if_exists='append', ) _index_table(tablename, metadata, engine, config.CASE_INSENSITIVE) @@ -111,7 +112,7 @@ def _index_table(tablename, metadata, engine, case_insensitive=False): """ table = sa.Table(tablename, metadata, autoload_with=engine) for label, column in table.columns.items(): - if label == 'index': + if label == 'id': continue index_name = 'ix_{0}'.format(label.lower()) indexes = [sa.Index(index_name, column)] From 5bd8ae04976c96993dd94fcfe779243b34e0e85a Mon Sep 17 00:00:00 2001 From: Vraj Mohan Date: Sun, 31 Jul 2016 22:11:05 -0400 Subject: [PATCH 2/2] Start id column at 1 --- test_load_table.py | 18 +++++++++--------- utils.py | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test_load_table.py b/test_load_table.py index 8c8504f..3cbe394 100644 --- a/test_load_table.py +++ b/test_load_table.py @@ -65,9 +65,9 @@ def test_good_csv(self, tmpdir): connection = self.engine.connect() results = connection.execute( select([people]).order_by(people.c.id)).fetchall() - assert (0, 'Tom', '1980-02-26', '0') == results[0] - assert (1, 'Dick', '1982-03-14', '3') == results[1] - assert (2, 'Harry', '1972-11-24', '2') == results[2] + assert (1, 'Tom', '1980-02-26', '0') == results[0] + assert (2, 'Dick', '1982-03-14', '3') == results[1] + assert (3, 'Harry', '1972-11-24', '2') == results[2] def test_load_with_chunking(self, tmpdir): CSV = """name, dob, number_of_pets @@ -83,9 +83,9 @@ def test_load_with_chunking(self, tmpdir): connection = self.engine.connect() results = connection.execute( select([people]).order_by(people.c.id)).fetchall() - assert (0, 'Tom', '1980-02-26', '0') == results[0] - assert (1, 'Dick', '1982-03-14', '3') == results[1] - assert (2, 'Harry', '1972-11-24', '2') == results[2] + assert (1, 'Tom', '1980-02-26', '0') == results[0] + assert (2, 'Dick', '1982-03-14', '3') == results[1] + assert (3, 'Harry', '1972-11-24', '2') == results[2] def test_empty_csv(self, tmpdir): file_name = 'empty.csv' @@ -121,9 +121,9 @@ def test_null_empty_string_handling(self, tmpdir): connection = self.engine.connect() results = connection.execute( select([people]).order_by(people.c.id)).fetchall() - assert (0, 'Tom', '0') == results[0] - assert (1, None, '3') == results[1] - assert (2, None, '2') == results[2] + assert (1, 'Tom', '0') == results[0] + assert (2, None, '3') == results[1] + assert (3, None, '2') == results[2] def test_embedded_space_in_tablename_and_filename(self, tmpdir): CSV = """name, number_of_pets diff --git a/utils.py b/utils.py index c01e4af..b927592 100644 --- a/utils.py +++ b/utils.py @@ -95,7 +95,7 @@ def load_table(filename, dtype=dtypes, skipinitialspace=True) for idx, chunk in enumerate(chunks): - chunk.index += chunksize * idx + chunk.index += chunksize * idx + 1 chunk.index.name = 'id' to_sql(tablename, engine,