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

backref versus back_populates #46

Closed
elemoine opened this issue Oct 18, 2015 · 3 comments
Closed

backref versus back_populates #46

elemoine opened this issue Oct 18, 2015 · 3 comments

Comments

@elemoine
Copy link
Contributor

We currently use a backref in the document relationship set on DocumentVersion. That backref creates a (view-only) relationship versions on Document, which makes sense.

At pyconfr I attended an SQLAlchemy talk where the presenter recommended to use back_populates instead of backref.

back_populates has the same meaning as backref, except that the complementing relationship property is not created automatically. So using back_populates makes the model code more explicit, with no hidden/implicit properties.

See the relationship-backref documentation for more information.

This is a minor thing, but I thought I could mention it here :)

@tsauerwein
Copy link
Member

I tried to change it, but there is a cyclic dependency between Document and DocumentVersion, so that I would have to add the relationship on Document after both classes have been defined. Which does not make it more explicit.

@elemoine
Copy link
Contributor Author

Not a big deal. Thanks for trying it out!

@deckar01
Copy link

Sorry to comment on an old, closed issue, but this shows up on the first page of google results when you search "backref vs back_populates".

there is a cyclic dependency between Document and DocumentVersion

SQLAlchemy's documentation shows using strings for the table references in relationships for this reason. You would always end up with circular imports if you had to import related classes. I personally consider backref detrimental to code quality, because it hides important information about what data the model contains.

http://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html#one-to-many

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