Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when declaring a non-default primary key, why cant I access it by the name of the field? #1293

Closed
vahana opened this issue May 11, 2016 · 2 comments

Comments

@vahana
Copy link
Contributor

vahana commented May 11, 2016

if I have
gid = mongo.ObjectIdField(primary_key=True)

I can access it only using "id" or "pk", but not its own name.

@amcgregor
Copy link
Contributor

There is a huge internal dependance on the existence of "id" and "pk", such that if the field marked as primary_key weren't renamed, pretty much everything would explode.

Since your example here is… basically the stock default use for _id, there's a trick I use to allow for this kind of re-naming:

class Foo(Document):
    gid = ObjectIdField(db_field="_id")

This assigns the gid attribute name to point at the actual _id field db-side. Note this is still a terrible idea. From the Zen of Python: there should be one, and preferably only one, right way to do something. Since MongoDB requires the primary key be called _id, why not stick to MongoEngine's default id (saving typing the _ all the time)?

@bagerard
Copy link
Collaborator

Not sure when but this got fixed so accessing it by "gid" would work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants