balki
balki

Reputation: 27664

create_or_get entry in a table

I have two related classes as below:

class IP(Base):
    __tablename__ = 'ip'
    id = Column(Integer, primary_key=True)
    value = Column(String, unique=True)
    topics = relationship('Topic')


class Topic(Base):
    __tablename__ = 'topic'
    id = Column(Integer, primary_key=True)
    value = Column(String)
    ip_id = Column(Integer, ForeignKey('ip.id'))
    ip = relationship('IP')

if __name__ == '__main__':
  Base.metadata.create_all(engine)
  topics = [
      Topic(value='t1', ip=IP(value='239.255.48.1')),
      Topic(value='t2', ip=IP(value='239.255.48.1')),
      Topic(value='t3', ip=IP(value='239.255.48.1'))
      ]
  session.add_all(topics)

The above doesnt work as it tries to add different ip entries with same value. Is it possible to create or get the existing one so that I can use like below?

  topics = [
      Topic(value='t1', ip=create_or_get(value='239.255.48.1')),
      Topic(value='t2', ip=create_or_get(value='239.255.48.1')),
      Topic(value='t3', ip=create_or_get(value='239.255.48.1'))
      ]

Upvotes: 1

Views: 609

Answers (1)

van
van

Reputation: 76992

Sure, just create the function:

def create_or_get(value):
    obj = session.query(IP).filter(IP.value==value).first()
    if not obj:
        obj = IP(value=value)
        session.add(obj)
    return obj

Of course, it needs a session, but if you use scoped_session factory, it is straightforward. Alternatively, you might look into events, but it gets too complicated for the problem to solve.

Upvotes: 2

Related Questions