Skip to content

Commit

Permalink
Merge pull request #110 from activated-insights/master
Browse files Browse the repository at this point in the history
Fix for "django.db.utils.ProgrammingError: type "serial" does not exist" when importing PKs
  • Loading branch information
palewire authored Sep 21, 2019
2 parents 86489dc + 871cb39 commit 59292bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions postgres_copy/copy_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ def prep_insert(self):
# Pull the field object from the model
field = self.get_field(field_name)
field_type = field.db_type(self.conn)
if field_type == "serial":
field_type = "integer"

# Format the SQL
string = 'cast("%s" as %s)' % (header, field_type)
Expand Down
9 changes: 9 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,15 @@ def test_save_foreign_key_by_id(self, _):
date(2012, 1, 1)
)

@mock.patch("django.db.connection.validate_no_atomic_block")
def test_save_pk_field_type(self, _):
# Django casts PK fields to "serial"
MockObject.objects.from_csv(
self.name_path,
dict(id='NUMBER', name='NAME', dt='DATE'),
)
self.assertEqual(MockObject.objects.count(), 3)

def test_silent_save(self):
c = CopyMapping(
MockObject,
Expand Down

0 comments on commit 59292bd

Please sign in to comment.