Issue: what does 'backref' really mean in foreign key?
Author: bmxbmx3
State: closed
Created: 2019-09-28 10:15:23

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?

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:

```python
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.

```python
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
```

> 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:
> 
> ```python
> 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.
> 
> ```python
> 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
> ```

Is this feature especially used for NoSQL?

> Is this feature especially used for NoSQL?

![im-76a73f7f4d](https://user-images.githubusercontent.com/119974/65818195-e868a800-e1d4-11e9-9b3b-9acc3cc94235.png)

> > Is this feature especially used for NoSQL?
> 
> ![im-76a73f7f4d](https://user-images.githubusercontent.com/119974/65818195-e868a800-e1d4-11e9-9b3b-9acc3cc94235.png)

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 `user` could be the class variable of `Tweet` like this:
`Tweet.create(user=**user**, content='huey-1')`
why not the basic data types like int,char and so on?I haven't met it before,just seems a bit weird for me.

Thank you! This is really useful😋

what is the default name for `backref` when we don't specify `backref` parameter?
`<model_name>_set` in django for example.

Yes, peewee uses the django convention for the default backref accessor.