On Sep 21, 2010, at 8:42 PM, Michael Hipp wrote:

> 
> class Car(Base):
>    __tablename__ = 'cars'
>    id_ = Column(Integer, primary_key=True)
>    auct_id = Column(Integer, ForeignKey('auctions.id_'), nullable=False)
>    auction = relationship('Auction', backref=backref('cars', order_by=lane))
> 
> If I do this, it fails trying to do an 'INSERT' on the new Car.
>    new.auction = old.auction
> 
> If I change that to:
>    new.auct_id = old.auct_id
> 
> It attempts an UPDATE as expected. (I don't understand why that 
> auct_id/auction column would have that effect; it's not a primary key.)

It definitely does not attempt an INSERT if id_ is set to a non-None value, 
assuming that row already exists in the DB, without something else in your 
model/usage causing that to happen.    If id_ is None or the given id_ doesn't 
exist in the DB, you get an INSERT.   auct_id has no direct effect here.

> 
> But that won't work because new.auction is still None so when I attempt a 
> commit it fails with:
> 
> Exception: IntegrityError: (IntegrityError) null value in column "auct_id" 
> violates not-null constraint
> 'UPDATE cars SET make=%(make)s, auct_id=%(auct_id)s, 
> version_id=%(version_id)s WHERE cars.id_ = %(cars_id_)s AND cars.version_id = 
> %(cars_version_id)s' {'auct_id': None, 'make': '', 'cars_version_id': 1, 
> 'cars_id_': 2, 'version_id': 2}

that also makes no sense since if you set "auct_id" manually, assuming 
old.auct_id is not None, it wouldn't be None in the UPDATE statement.

> 
> Why is 'auction' causing it to do an INSERT and how can I set it?

No idea.   There's definitely more going on that you aren't illustrating here.  
  As usual, distilling down the behavior that appears wrong into a single 
file/no-dependencies/test-data-provided-inline/fully runnable/reproducible test 
will reveal either the issue in your usage, or whatever bug/subtle SQLAlchemy 
issue is at play.   

-- 
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.

Reply via email to