Incorrect .id attribute in __repr__ method

11 views
Skip to first unread message

RexE

unread,
Dec 6, 2020, 9:26:48 AM12/6/20
to sqlalchemy
Hello, I have this base model:

class AnyModel(DeclarativeBase):
    __abstract__ = True

    id = Column(st.Integer, primary_key=True)

    def __repr__(self):
        return '<{} id={}>'.format(self.__class__.__name__, self.id)

repr() seems to be printing the incorrect value for my  IDs. For example,

[<Player  3>, <Player  1>, <Player  2>, <Player  1>]
[<Player  3>, <Player  1>, <Player  3>, <Player  4>]
etc...

The output is seemingly random.

Even though I have hundreds of objects in the DB, it always prints out a number that is less than than the number of objects created per commit, and there are duplicates and missing numbers when I query:

[<Participant id=2>, <Participant id=1>, <Participant id=3>, <Participant id=2>, <Participant id=3>, <Participant id=5>, <Participant id=8>, <Participant id=3>, <Participant id=8>, <Participant id=3>]

if I define the __repr__ on the child class it gives the correct output. even though calling the super() method gives a different result: 

        def __repr__(self):
             print('in child class dt is correct:', self.id)
             print('but in base class it is', super().__repr__())
             return '<{} id={}>'.format(self.__class__.__name__, self.id)

But if I rename the method __repr2__ this same issue does not occur :?

If I dump the sqlite database, the data all looks fine and consistent. IDs are numbered sequentially 1-100 with no duplicates or gaps.

Any ideas? I tried to create a MWE but for some reason this problem only happens in my full-scale app.

Thank you!

Mike Bayer

unread,
Dec 6, 2020, 11:09:28 AM12/6/20
to noreply-spamdigest via sqlalchemy
hey there -

it seems pretty likely that the source of objects you are using, which would be the "query" you refer towards, or some process afterwards by which these objects are assembled into the list you are printing,  contains the same object instance multiple times, hence you see the same IDs indicated multiple times.

The reality of your situation might be much different than this, however the information provided is extremely scant which prevents me from having any better answer.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
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+...@googlegroups.com.

RexE

unread,
Dec 6, 2020, 11:31:09 AM12/6/20
to sqlalchemy
Hello, thank you for the reply. All those lists are the output of:

session.query(Player).all()

For example, here is my code:

player_list = db.query(Player).all()
ids_only = [p.id for p in player_list]

print('player_list', player_list)
print('ids_only', ids_only)

Here is the output:

player_list [<Player  1>, <Player  2>, <Player  2>, <Player  1>, <Player  2>, <Player  2>]
ids_only [1, 2, 3, 4, 5, 6]

What I would expect is:
player_list [<Player  1>, <Player  2>, <Player  3>, <Player  4>, <Player  5>, <Player  6>]  

Since my __repr__ method just outputs the id, as in my previous post.

Thank you and let me know if I can provide further info.

RexE

unread,
Dec 6, 2020, 2:27:17 PM12/6/20
to sqlalchemy
This issue is no longer occurring, so I take back the question for now :)
Reply all
Reply to author
Forward
0 new messages