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.
On Monday, December 7, 2020 at 12:09:28 AM UTC+8 Mike Bayer wrote:
> 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.
>
>
>
> On Sun, Dec 6, 2020, at 9:26 AM, RexE wrote:
>
> 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!
>
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sqlalchemy/065d40f8-a146-4766-9ae2-08d406a44b9fn%40googlegroups.com
>
> <https://groups.google.com/d/msgid/sqlalchemy/065d40f8-a146-4766-9ae2-08d406a44b9fn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
>
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/b5658cab-e009-47e8-b2b5-7d7b95a4cbc1n%40googlegroups.com.