Search code examples
pythonsoapspyne

Set up spyne to skip a field which is not present in SOAP request?


I have a SQLALchemy model with such a column:

updated_at = Column(DateTime, nullable=False, server_default=func.now(),
                        onupdate=func.now())

Which generates WSDL:

<xs:element name="updated_at" type="xs:dateTime" minOccurs="0"/>

In the update request updated_at field is missing, and spyne maps its value to None causing this:

IntegrityError: (IntegrityError) null value in column "updated_at" violates not-null constraint
 'UPDATE subcsription SET updated_at=%(updated_at)s WHERE subcsription.id = %(subcsription_id)s' {'subcsription_id': 27, 'updated_at': None}

How can i set up spyne to skip the field at all when it is not passed in SOAP request?


Solution

  • Your interface and your database schema don't need to match 1-to-1. You can define a separate object that doesn't have the updated_at member and use it as the input type in your service definition. E.g.

    class SomeObject(TableModel):
        __tablename__ = 'some_table'
    
        id = UnsignedInteger64(pk=True)
        updated_at = DateTime(server_default=func.now(), onupdate=func.now())
        some_data = Unicode
    
    class SomeWriteObject(TableModel):
        __tablename__ = 'some_table'
    
        id = UnsignedInteger64(pk=True)
        some_data = Unicode
    
    # or, you could do this:
    class SomeWriteObject(TableModel):
        __tablename__ = 'some_table'
        _type_info = [ (k,v) for k,v in SomeObject._type_info.items()
                                                      if not k in ('updated_at',) ]
    class SomeService(ServiceBase):
        @rpc(SomeWriteObject, _returns=UnsignedInteger64)
        def add_some_object(ctx, obj):
            ctx.udc.session.add(obj)
            ctx.udc.session.commit()
            return obj.id
    

    This way updated_at will be left out in the insert query which will leave it to the database to fill that field according to your instructions.