Skip to content

Commit fae67d8

Browse files
committed
catch type errors and retry parsing after re-fetching the type info
1 parent 3ebcc6b commit fae67d8

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

postgres/__init__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -839,17 +839,24 @@ def make(self, values):
839839
instance = ModelSubclass(record)
840840
return instance
841841

842-
def parse(self, s, curs):
842+
def parse(self, s, curs, retry=False):
843843
if s is None:
844844
return None
845845

846846
tokens = self.tokenize(s)
847847
if len(tokens) != len(self.atttypes):
848-
# The type has changed, re-fetch it from the DB
848+
# The number of columns has changed, re-fetch the type info
849849
self.__dict__.update(self._from_db(self.name, curs).__dict__)
850850

851-
values = [ curs.cast(oid, token)
852-
for oid, token in zip(self.atttypes, tokens) ]
851+
try:
852+
values = [ curs.cast(oid, token)
853+
for oid, token in zip(self.atttypes, tokens) ]
854+
except ValueError:
855+
# The type of a column has changed, re-fetch it and retry once
856+
if retry:
857+
raise
858+
self.__dict__.update(self._from_db(self.name, curs).__dict__)
859+
return self.parse(s, curs, True)
853860

854861
return self.make(values)
855862

0 commit comments

Comments
 (0)