Does SA natively support (or is there a module on PyPI that supports)
client-side validation of SQLAlchemy objects? For example, I have this
declarative class:
class ImportedPayment(Base):
__tablename__ = 'importedpayment'
__table_args = {'schema': 'public'}
paymentid = Column(Integer, primary_key=True)
externalid = Column(String(16), nullable=False)
line = Column(Integer, nullable=False)
invoicestatus = Column(String(32), nullable=False)
quantity = Column(Numeric(scale=2), nullable=False)
rate = Column(Numeric(scale=2), nullable=False)
I'm reading data from a directory full of spreadsheets and generating millions
of these objects. If one contains invalid data, it's OK to just log it and move
on. Unfortunately, that pretty much means that I have to commit after every
insertion so that I can catch any potential exceptions and this makes the whole
process take ages to run.
Now, I've already defined the error conditions I'm likely to encounter and that
I can easily handle: they're the constraints I defined in the class above. I'd
love for the objects I'm creating to validate themselves through a method call
like:
newobject = ImportedPayment(externalid='foo', line=None, [...])
try:
newobject.validate()
except ValueError as e:
print e.column, e.errmsg
else:
session.add(newobject)
yielding
'line', 'None in not-nullable column'
or similar. Granted, I could write these tests easily myself:
if externalid is None or len(externalid) > 16:
return False
but I've already specified them once and I don't want to repeat myself as it's
a lot of extra typing and a lot harder to maintain (if I change "invoicestatus"
to a String(64), I have to update every module which manually validates that
data). Note that I'm not talking about higher-level checks like "the
emailaddress Column contains a valid email address", but just the simple data
type checks that can be inferred from class definitions (strings of appropriate
length, not null, etc.).
Is there an easy way to do this? If not, why? And if the answer to that is
"because you haven't written it yet", would anyone be interested in using it if
I were to create such a thing?
Kirk
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.