Skip to content

Commit

Permalink
Merge 8f02d42 into 86489dc
Browse files Browse the repository at this point in the history
  • Loading branch information
sheats committed Sep 3, 2019
2 parents 86489dc + 8f02d42 commit 0585c4d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions postgres_copy/copy_from.py
Expand Up @@ -20,6 +20,7 @@ class CopyMapping(object):
"""
Maps comma-delimited file to Django model and loads it into PostgreSQL database using COPY command.
"""

def __init__(
self,
model,
Expand Down Expand Up @@ -365,6 +366,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
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 0585c4d

Please sign in to comment.