peewee的related_name是做什么用的
from peewee import *
from pprint import pprint
import datetime
import random
import sys
db = SqliteDatabase('people.db')
class User(Model):
uid = IntegerField(primary_key=True)
name = CharField()
class Meta:
database = db # This model uses the "people.db" database.
class Tweet(Model):
user = ForeignKeyField(User, related_name='tweets')
message = TextField()
created_date = DateTimeField(default=datetime.datetime.now)
is_published = BooleanField(default=True)
#db.create_tables([User,Tweet])
username=str(random.random())+"oo"
msg=str(random.random())+"xx"
try:
user = User.get(name=username)
print(user.uid)
except:
user=User.create(name=username)
print(user.uid)
try:
tt = Tweet.get(user=4)
print(tt.user)
except:
user=Tweet.create(user=4,message=msg)
print(user.message)
total=User.select().count()
print(total)
t=Tweet.select().join(User).where(User.uid ==8).order_by(Tweet.created_date.desc()).get()
print(t)
http://peewee.readthedocs.org/en/latest/peewee/querying.html
照着官方的文档写的,但是报错
peewee.OperationalError: no such table: user
user表肯定有,因为create,get都能正确执行
就是到join关联表的时候出错
写成
t=Tweet.select().join(tweets).where(User.uid ==8).order_by(Tweet.created_date.desc()).get()
也一样
正规空母加贺改
9 years, 2 months ago