Peewee: what does 'backref' really mean in foreign key?

Created on 28 Sep 2019  路  7Comments  路  Source: coleifer/peewee

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?

Most helpful comment

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

All 7 comments

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

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

Is this feature especially used for NoSQL?

Is this feature especially used for NoSQL?

im-76a73f7f4d

Is this feature especially used for NoSQL?

im-76a73f7f4d

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aellwein picture aellwein  路  3Comments

alexpantyukhin picture alexpantyukhin  路  5Comments

ghost picture ghost  路  5Comments

alexlatchford picture alexlatchford  路  4Comments

ezk84 picture ezk84  路  4Comments