On 04/11/2016 08:08 PM, Matthew Blain wrote:
Hi,
I'm learning SQLAlchemy, and a Composite Column seems like a very useful
concept.
However, there are very few mentions of it I can find. So some related
questions after reading some docs and some experimentation.
* 'Hiding'--let's say that for the Vertex example in the docs, I
really want people to use Vertex.start, and not Vertex.x1/y1. Is it
recommended to name them _x1 and _y1, say by using _x1 = Column('x1,
Integer) etc?
that's pretty much up to you. current composite methodology should
allow those columns to work in a direct context pretty well.
* Validation: let's say I want 'QuadrantOnePoint', i.e. x and y need
to be positive. Is there some way to use @validates in the composite
class (e.g. Point)? Even better in the constructor somehow, so that
Point can also be used elsewhere, but with the caveat that it's probably
bad for the system to fail to load if data in the DB is somehow invalid.
well if you're in the constructor, you're there, validate away. If a
"quadrantonepoint" with negative x/y makes no sense then add that rule
to its __init__() method.
@validates should work for a composite object as well but this happens
when you actually assign the composite object to the parent object.
I see the MutableBase.coerce() note about validation, but I want the
validation used primarily at the constructor (e.g. v1 =
Vertex(start=Point(3, 4), end=Point(12, 15)) should work, but v1 =
Vertex(start=Point(-3, 4), end=Point(12, -15)) should fail, because the
two Point constructors are both for points outside the valid range.
in that example, Vertex is a mapped class, Point is the composite. if a
Point(-3, 4) by itself makes sense, then you'd leave Point() alone, and
put the validation either into Vertex.__init__, or as a @validates
setter for Vertex. The @validates setter is of more general use,
because when you assign inside of __init__, it still gets called.
* Queries: if someone does query(Vertex).filter(start == Point(0,0)),
that seems to work (at least using a my own similar class). But not
filter(start >= Point(1,1)). I get "NotImplementedError: <built-in
function gt>". Implementing __gt__ on Point does not fix this. Is
something else (say a comparator_factory) required?
yes see the example here:
http://docs.sqlalchemy.org/en/rel_1_0/orm/composites.html#redefining-comparison-operations-for-composites
Thanks,
Matthew
--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.