question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

keyword-based get() for composite primary key

See original GitHub issue

Migrated issue, originally created by Alex Rothberg

Right now the ident parameter to query.get is a tuple in the case of composite primary keys with the stipulation:

For a composite primary key, the order of identifiers corresponds in most cases to that of the mapped :class:.Table object’s primary key columns.

Having to know and hard code the order of the primary keys seems less than ideal. I suggest accepting a dict mapping names to the values for the primary keys. My implementation of this looks like:

from sqlalchemy.inspection import inspect

def get_by_pks(model, **kwargs):
    return model.query.get(
        tuple(
            kwargs[key.name]
            for key in inspect(model).primary_key
        )
    )

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
zzzeekcommented, Feb 18, 2019

@sanjana96 that is the general idea, although it requires a few changes. if you can submit as a PR I can illustrate; the conversion from mapper.primary_key needs to be stated in terms of attribute names, not column keys, and I’d like to see if there’s a way to implement the test for length only once rather than stating it twice (easier to maintain and cover in tests).

0reactions
zzzeekcommented, Apr 5, 2022

Here’s an MCVE that works, try this

from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import PrimaryKeyConstraint
from sqlalchemy import String
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import Session

Base = declarative_base()


class GuaranteedValue(Base):
    __tablename__ = "GUARANTEEDVALUE"
    __table_args__ = (PrimaryKeyConstraint("SPEC", "NAME"),)
    SPEC = Column(String)
    NAME = Column(String)


e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

s = Session(e)
s.add(GuaranteedValue(SPEC="spec", NAME="name"))
s.commit()

gv = s.get(GuaranteedValue, {"SPEC": "spec", "NAME": "name"})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Composite Key in SQL: Your Ultimate Guide to Mastery
The primary key is a constraint that recognizes all of the columns in a row as unique. For a constraint to be recognized...
Read more >
Composite Key in SQL
A composite key is made by the combination of two or more columns in a table that can be used to uniquely identify...
Read more >
SQL COMPOSITE KEY
Composite key is a key which is the combination of more than one field or column of a given table. It may be...
Read more >
Learn How to Use Composite Key in SQL
The Composite Key in SQL is a combination of two or more columns, which are used to identify the rows from a table....
Read more >
PostgreSQL Composite Primary Keys
Sometimes you can guarantee uniqueness of a record through a combination of column values. This is what a composite key is. It's a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found