On 10/23/2016 03:46 PM, Ergo wrote:
Hi,

I'm trying to use get() with composite primary key like this:

class GroupPermissionService(BaseService):
    @classmethod
    def get(cls, group_id, perm_name, db_session=None):
        db_session = get_db_session(db_session)
        return db_session.query(cls.model).get([group_id, perm_name])

I'm using Python 3.5.1 with SQLAlchemy==1.0.12,

It seems that my tests fail on random when .get() returned nothing, I've
added print(cls.model.__mapper__.primary_key) to the body of my function
to see the order of primary keys and it appears that
the order can change between the runs, this is the output of print:

(Column('perm_name', Unicode(length=64), table=<groups_permissions>,
primary_key=True, nullable=False), Column('group_id', Integer(),
ForeignKey('groups.id'), table=<groups_permissions>, primary_key=True,
nullable=False))

(Column('group_id', Integer(), ForeignKey('groups.id'),
table=<groups_permissions>, primary_key=True, nullable=False),
Column('perm_name', Unicode(length=64), table=<groups_permissions>,
primary_key=True, nullable=False))

So my question is: is this a bug or am I trying to use this incorrectly
and I should somehow determine the order when application is loaded?

I would assume the order of keys should be deterministic in this scenario.

you likely have some mixins or something producing your primary key and not coming out in the same order on each run of the interpreter. Add a PrimaryKeyConstraint to your declared class that establishes the desired column ordering.




--
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 sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to