- Notifications
- Fork 1.4k
-
Star 10.9k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
what does 'backref' really mean in foreign key? #2027
what does 'backref' really mean in foreign key? #2027
Comments
coleifer
commented
Sep 28, 2019
It's pretty straightforward if you have an understanding of foreign-keys in the first place. If you have a user and tweet model, the tweet has a foreign-key to user to indicate the author of the tweet: class User(Model):
username = TextField()
class Tweet(Model):
user = ForeignKeyField(User, backref='tweets')
content = TextField() So when you create a tweet, you store the tweet content along with a reference to the user who created it. Thus a tweet has a relationship to one user, and a user has an implicit relationship to any number 0..n tweets. The backref provides a way to access the tweets that refer to the given user. user = User.create(username='huey')
Tweet.create(user=user, content='huey-1')
Tweet.create(user=user, content='huey-2')
# Get all tweets by huey.
query = Tweet.select().where(Tweet.user == user)
# Equivalent to above, using backref:
query = user.tweets |
bmxbmx3
commented
Sep 28, 2019
Is this feature especially used for NoSQL? |
coleifer
commented
Sep 28, 2019
|
bmxbmx3
commented
Sep 28, 2019
sorry,it seems that I've made a mistake.It's my fault...anyway,I just don't understand in your toy code why the object |
V-U-Simon
commented
Oct 15, 2020
Thank you! This is really useful馃構 |
fallaha
commented
Jan 9, 2021
•
edited
edited
what is the default name for |
coleifer
commented
Jan 9, 2021
Yes, peewee uses the django convention for the default backref accessor. |
mcavag
commented
Feb 11, 2024
Doesn't peewee already know which table to query, since you've established the What's the use case for providing some different value for |
I have read the documentation and try to understand the meaning of 'backref',which I have found in the word said "Every foreign-key field has an implied back-reference, which is exposed as a pre-filtered Select query using the provided backref attribute."But I still don't know how the back-reference could be exposed as a pre-filtered Select query,can you explain it more clearly please?
The text was updated successfully, but these errors were encountered: