0

I have following two schema.

class Stop(Base):
    '''
        Class Model for Stops
    '''
    __tablename__ = 'STOPS'
    uid = Column('uid', Integer, primary_key=True, autoincrement=False)
    category_code = Column('category_code', String(60), nullable=False)

and

class StopName(Base):
    '''
        Class Model for StopNames
    '''
    __tablename__ = 'STOP_NAME'
    uid = Column('uid', Integer, ForeignKey("STOPS.uid"), primary_key=True)
    name = Column('name', String(60), primary_key = True,nullable=False)
    lang_code = Column('lang_code', String(4), nullable=False)

    stop = relationship("Stop", backref = 'name')

While I try to delete data using the code below from both the tables I get the following error,

session.query(StopName).delete()
session.query(Stop).delete()

New instance <Stop at 0xf593d50> with identity key (<class 'harmonize_station_id.core.model.Stop'>, (400000000,)) conflicts with persistent instance <Stop at 0x3bdd850>

I tried using cascade too, with following

stop = relationship("Stop", backref = 'name',cascade="delete")

I get the following error.

AttributeError: 'StopName' object has no attribute '_sa_instance_state'.

1 Answer 1

1

The issue was there because of the mismatch of object type of StopName. Here I was converting Stop object of client to Stop object of server(db.model). But I missed the same conversion of StopName object. So, it was trying to create StopName from client representation of it. So, i explicitly converted it to StopName of server side. It resolved the issue. So Basically, object has no attribute '_sa_instance_state' error comes when sqlalchemy interprets other kind of object as an ORM Object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.