Skip to content
/ peewee Public
  • 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.

Sign up for GitHub

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

Jump to bottom

what does 'backref' really mean in foreign key? #2027

Closed
bmxbmx3 opened this issue Sep 28, 2019 · 8 comments
Closed

what does 'backref' really mean in foreign key? #2027

bmxbmx3 opened this issue Sep 28, 2019 · 8 comments

Comments

@bmxbmx3
Copy link

bmxbmx3 commented Sep 28, 2019

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?

@coleifer
Copy link
Owner

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

@coleifer coleifer closed this as completed Sep 28, 2019
@bmxbmx3
Copy link
Author

bmxbmx3 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

Is this feature especially used for NoSQL?

@coleifer
Copy link
Owner

coleifer commented Sep 28, 2019

Is this feature especially used for NoSQL?

im-76a73f7f4d

@bmxbmx3
Copy link
Author

bmxbmx3 commented Sep 28, 2019

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.

@V-U-Simon
Copy link

V-U-Simon commented Oct 15, 2020

Thank you! This is really useful馃構

@fallaha
Copy link

fallaha commented Jan 9, 2021

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

@coleifer
Copy link
Owner

coleifer commented Jan 9, 2021

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

@ldconejo ldconejo mentioned this issue Nov 8, 2022
Made changes per comment from first pull request but still need help bgross2022/lesson03_activity#2
Closed
@mcavag
Copy link

mcavag commented Feb 11, 2024

Doesn't peewee already know which table to query, since you've established the ForeignKeyField() relationship?

What's the use case for providing some different value for backref?

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

5 participants
@coleifer @mcavag @fallaha @bmxbmx3 @V-U-Simon

Footer

© 2024 GitHub, Inc.

深圳SEO优化公司兴安盟网站开发推荐铜仁网站制作价格渭南外贸网站制作贵阳网站设计临夏网站优化推广报价湛江网站开发推荐常德seo哪家好临猗企业网站改版价格成都网站建设哪家好莆田百度竞价包年推广价格大丰网站seo优化哪家好思茅企业网站制作公司张家界百度竞价包年推广报价海西百度seo公司柳州关键词排名多少钱平顶山网站建设设计报价金华seo网站推广多少钱扬州阿里店铺托管徐州网站seo优化多少钱厦门优化南阳优化岳阳网站优化按天扣费价格通化网站关键词优化报价桐城网站搭建多少钱迁安seo哪家好桐城seo公司永湖seo网站优化报价天门关键词排名价格本溪seo网站优化鞍山网站推广工具公司歼20紧急升空逼退外机英媒称团队夜以继日筹划王妃复出草木蔓发 春山在望成都发生巨响 当地回应60岁老人炒菠菜未焯水致肾病恶化男子涉嫌走私被判11年却一天牢没坐劳斯莱斯右转逼停直行车网传落水者说“没让你救”系谣言广东通报13岁男孩性侵女童不予立案贵州小伙回应在美国卖三蹦子火了淀粉肠小王子日销售额涨超10倍有个姐真把千机伞做出来了近3万元金手镯仅含足金十克呼北高速交通事故已致14人死亡杨洋拄拐现身医院国产伟哥去年销售近13亿男子给前妻转账 现任妻子起诉要回新基金只募集到26元还是员工自购男孩疑遭霸凌 家长讨说法被踢出群充个话费竟沦为间接洗钱工具新的一天从800个哈欠开始单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#中国投资客涌入日本东京买房两大学生合买彩票中奖一人不认账新加坡主帅:唯一目标击败中国队月嫂回应掌掴婴儿是在赶虫子19岁小伙救下5人后溺亡 多方发声清明节放假3天调休1天张家界的山上“长”满了韩国人?开封王婆为何火了主播靠辱骂母亲走红被批捕封号代拍被何赛飞拿着魔杖追着打阿根廷将发行1万与2万面值的纸币库克现身上海为江西彩礼“减负”的“试婚人”因自嘲式简历走红的教授更新简介殡仪馆花卉高于市场价3倍还重复用网友称在豆瓣酱里吃出老鼠头315晚会后胖东来又人满为患了网友建议重庆地铁不准乘客携带菜筐特朗普谈“凯特王妃P图照”罗斯否认插足凯特王妃婚姻青海通报栏杆断裂小学生跌落住进ICU恒大被罚41.75亿到底怎么缴湖南一县政协主席疑涉刑案被控制茶百道就改标签日期致歉王树国3次鞠躬告别西交大师生张立群任西安交通大学校长杨倩无缘巴黎奥运

深圳SEO优化公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化