bingo! thanks Simon. that's exactly the question :)

well, the checks on the cube are already there (they must have a value higher then 0 to have a volume), but i must not increment the cube children more then it's maximum capacity. i'm considering an event listener as well, but i'm wondering if it can't be done at database level, using postgres?

or, what would be the better approach scenario for this? a trigger, an event, both?



On 05/19/2015 01:13 PM, Simon King wrote:
On Tue, May 19, 2015 at 4:06 PM, Mike Bayer <mike...@zzzcomputing.com> wrote:

On 5/19/15 10:54 AM, Richard Gerd Kuesters wrote:

thanks Mike!

when i stated about the limit, it was because it must not be taken as a
parameter for any query, which "select * from blah" and "select * from bla
limit N" should be return the same exactly number of rows, including where
filters and so on. it is something like a "physical" rule, where my parent
is (really) a box and the children its items (so, i cannot put more items
then the box's limit).

either way, creating a relationship with limit *can* provide me that sort of
behaviour?

I don't understand what "the behavior" is here.   No LIMIT, yet there is a
"limit".   An assertion?    I have no idea what you mean.     The
relationship 1. emits SQL 2. loads the results into objects.    Are we
talking about 1. or 2. ?


I'm guessing he's looking for something like this:

class Cube(Base):
     __table_name__ = 'cube'
     id = sa.Column(sa.Integer, primary_key=True)
     rows = sa.Column(sa.Integer)
     cols = sa.Column(sa.Integer)
     depth = sa.Column(sa.Integer)

     @property
     def volume(self):
         return self.rows * self.cols * self.depth


class Cell(Base):
     __table_name__ = 'cell'
     id = sa.Column(sa.Integer, primary_key=True)
     cube_id = sa.Column(sa.ForeignKey(Cube.id))
     row = sa.Column(sa.Integer)
     col = sa.Column(sa.Integer)
     depth = sa.Column(sa.Integer)

     __table_args__ = (
         sa.UniqueConstraint('cube_id', 'row', 'col', 'depth')
     )

     cube = saorm.relationship(Cube, backref='cells')

Now, given an instance of Cube, how can you ensure that it is
impossible to add more than Cube.volume cells to the Cube.cells
relationship? I imagine it is possible by attaching an event listener
to Cube.cells and doing the validation in there.

If Richard's data really is structured like this, I'd probably instead
want to enforce that:

     0 <= cell.row < cell.cube.rows
     0 <= cell.col < cell.cube.cols
     0 <= cell.depth < cell.cube.depth

I think you could probably do this with SQLAlchemy validators. I don't
know enough Postgres, but I suspect you could also enforce it at the
database level.

Simon


--
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

<<attachment: richard.vcf>>

Reply via email to