Closed wanghaisheng closed 1 year ago
I can’t make sense out of all that. Can you provide a minimal example that reproduces the issue? If so I will reopen.
@coleifer this is a minimal one
from peewee import Model, CharField, IntegerField,TextField,BooleanField,IntegerField,ForeignKeyField
from peewee import *
import random
db_uri='1.sqlite3'
db = SqliteDatabase(db_uri)
class BaseModel(Model):
class Meta:
database = db
class AccountModel(BaseModel):
id = IntegerField(primary_key=True)
platform = IntegerField(null=True)
username = TextField(null=True)
password = TextField(null=True)
class YoutubeVideoModel(BaseModel):
id = IntegerField(primary_key=True)
video_title = TextField(null=True,default=None)
class UploadSettingModel(BaseModel):
id = IntegerField(primary_key=True)
timeout = IntegerField(default=200000)
account = ForeignKeyField(AccountModel, backref='account_id')
class TaskModel(BaseModel):
id = IntegerField(primary_key=True)
status = IntegerField()
video = ForeignKeyField(YoutubeVideoModel, backref='videos')
setting = ForeignKeyField(UploadSettingModel, backref='settings')
def filter_tasks(cls, status=None, type=None,uploaded_at=None,setting=None,inserted_at=None,video_title=None,video_id=None,username=None):
query=TaskModel.select()
# query = TaskModel.select(TaskModel,YoutubeVideoModel,UploadSettingModel,AccountModel)
print('all tasks are2 ',list(query))
if video_title is not None :
print('ding ding video title')
query = query.join(YoutubeVideoModel).where(YoutubeVideoModel.video_title == video_title)
query=query.switch(TaskModel) # <-- switch the "query context" back to ticket.
# 如果存在account 相关的查询参数,先找到对应的 setting id集合
if username is not None:
query = query.join(UploadSettingModel).join(AccountModel).where(AccountModel.username == username)
query=query.switch(TaskModel) # <-- switch the "query context" back to ticket.
print('2')
for i in query:
print('filter tasks',i.video,i.setting)
try:
result = list(query)
for i in result:
print('filter tasks',i.video,i.setting)
except cls.DoesNotExist:
result = None # Set a default value or perform any other action
return result
def filter_tasks2(cls, status=None, type=None,uploaded_at=None,setting=None,inserted_at=None,video_title=None,video_id=None,username=None):
query = (TaskModel
.select(TaskModel, YoutubeVideoModel, UploadSettingModel, AccountModel)
.join(YoutubeVideoModel) # Join favorite -> user (owner of favorite).
.switch(TaskModel)
.join(UploadSettingModel) # Join favorite -> tweet
.join(AccountModel)) # Join tweet -> user
print('999999999',query)
for fav in query:
print('================')
print(fav.video,fav.setting)
if video_title is not None :
print('ding ding video title')
query=query.switch(TaskModel) # <-- switch the "query context" back to ticket.
query = (query
# .join(YoutubeVideoModel,on=(TaskModel.video_id == YoutubeVideoModel.id))
# .where(YoutubeVideoModel.video_title.regexp(video_title))
.where(YoutubeVideoModel.video_title==video_title)
)
query=query.switch(TaskModel) # <-- switch the "query context" back to ticket.
print('1')
if video_id is not None:
query = query.join(YoutubeVideoModel,on=(TaskModel.video == YoutubeVideoModel.id)).where(YoutubeVideoModel.youtube_video_id == video_id)
query=query.switch(TaskModel) # <-- switch the "query context" back to ticket.
print('3')
# 如果存在account 相关的查询参数,先找到对应的 setting id集合
if username is not None:
query = query.join(UploadSettingModel,on=(TaskModel.setting == UploadSettingModel.id)).join(AccountModel,on=(UploadSettingModel.account == AccountModel.id)).where(AccountModel.username.regexp(username))
query=query.switch(TaskModel) # <-- switch the "query context" back to ticket.
print('2')
try:
result = list(query)
for i in result:
for att in dir(i):
print('===============')
print(att,getattr(i,att))
print('----------',i.video)
print('----------',i.setting)
except cls.DoesNotExist:
result = None # Set a default value or perform any other action
return result
db.create_tables([AccountModel,UploadSettingModel,YoutubeVideoModel,TaskModel])
test_user={
"platform":random.choice([0,1,2,3]),
"username":"y1",
"password":"p1"
}
accounts=[]
for i in range(1,20):
account=AccountModel.save(
test_user
)
accounts.append(account)
test_setting={
"timeout":random.choice(range(200,2000)),
"account":random.choice(accounts),
}
settings=[]
for i in range(1,20):
setting=UploadSettingModel.save(
test_setting,account=random.choice(accounts)
)
settings.append(setting)
test_video={
"video_title": 'test title'+str(random.choice(range(1,100)))
}
videos=[]
for i in range(1,50):
video=YoutubeVideoModel.save(
test_video
)
videos.append(setting)
test_task={
"type":'',
"status":random.choice([0,1,2])
}
tasks=[]
for i in range(1,100):
t=TaskModel.save(
test_task,setting=random.choice(settings),video=random.choice(videos)
)
tasks.append(t)
TaskModel.filter_tasks( status=None, type=None,uploaded_at=None,setting=None,inserted_at=None,video_title=None,video_id=None,username=None)
TaskModel.filter_tasks( status=None, type=None,uploaded_at=None,setting=None,inserted_at=None,video_title='test',video_id=None,username=None)
TaskModel.filter_tasks2( status=None, type=None,uploaded_at=None,setting=None,inserted_at=None,video_title=None,video_id=None,username=None)
TaskModel.filter_tasks2( status=None, type=None,uploaded_at=None,setting=None,inserted_at=None,video_title='test',video_id=None,username=None)
I'm sorry but this code is neither minimal nor readable.
Also, this code does not even work...
@coleifer try this
from peewee import Model, CharField, IntegerField,TextField,BooleanField,IntegerField,ForeignKeyField
from peewee import *
import random,os
os.remove('1.sqlite3')
db_uri='1.sqlite3'
db = SqliteDatabase(db_uri)
class BaseModel(Model):
class Meta:
database = db
class AccountModel(BaseModel):
id = IntegerField(primary_key=True)
platform = IntegerField(null=True)
username = TextField(null=True)
password = TextField(null=True)
class YoutubeVideoModel(BaseModel):
id = IntegerField(primary_key=True)
video_title = TextField(null=True,default=None)
class UploadSettingModel(BaseModel):
id = IntegerField(primary_key=True)
timeout = IntegerField(default=20)
account = ForeignKeyField(AccountModel, backref='account_id')
class TaskModel(BaseModel):
id = IntegerField(primary_key=True)
status = IntegerField()
video = ForeignKeyField(YoutubeVideoModel, backref='videos')
setting = ForeignKeyField(UploadSettingModel, backref='settings')
@classmethod
def filter_tasks2(cls, status=None, type=None,uploaded_at=None,setting=None,inserted_at=None,video_title=None,video_id=None,username=None):
query=TaskModel.select()
print('========show without filter========')
print('======show attr=========')
for i in query:
print(list(query))
for att in dir(i):
if 'setting' in att:
print(att,getattr(i,att))
if 'video' in att:
print(att,getattr(i,att))
print('======show ids=========')
for i in query:
print(i.id)
print('======show fks=========')
for i in query:
print(i.video,i.setting)
query = (TaskModel
.select(TaskModel, YoutubeVideoModel, UploadSettingModel, AccountModel)
.join(YoutubeVideoModel) # Join favorite -> user (owner of favorite).
.switch(TaskModel)
.join(UploadSettingModel) # Join favorite -> tweet
.join(AccountModel)) # Join tweet -> user
print('========show with join========')
for fav in query:
print(fav.video,fav.setting)
if video_title is not None :
print('ding ding video title')
query=query.switch(TaskModel) # <-- switch the "query context" back to ticket.
query = (query
# .join(YoutubeVideoModel,on=(TaskModel.video_id == YoutubeVideoModel.id))
# .where(YoutubeVideoModel.video_title.regexp(video_title))
.where(YoutubeVideoModel.video_title==video_title)
)
query=query.switch(TaskModel) # <-- switch the "query context" back to ticket.
print('1')
if video_id is not None:
query = query.join(YoutubeVideoModel,on=(TaskModel.video == YoutubeVideoModel.id)).where(YoutubeVideoModel.youtube_video_id == video_id)
query=query.switch(TaskModel) # <-- switch the "query context" back to ticket.
print('3')
# 如果存在account 相关的查询参数,先找到对应的 setting id集合
if username is not None:
query = query.join(UploadSettingModel,on=(TaskModel.setting == UploadSettingModel.id)).join(AccountModel,on=(UploadSettingModel.account == AccountModel.id)).where(AccountModel.username.regexp(username))
query=query.switch(TaskModel) # <-- switch the "query context" back to ticket.
print('2')
try:
result = list(query)
for i in result:
for att in dir(i):
print('======show attr=========')
print(att,getattr(i,att))
print('----------',i.video)
print('----------',i.setting)
except cls.DoesNotExist:
result = None # Set a default value or perform any other action
return result
db.create_tables([AccountModel,UploadSettingModel,YoutubeVideoModel,TaskModel])
test_accounts=[]
for i in range(1,5):
account=AccountModel.create(platform= random.choice([0,1,2,3]), username='y1',password='p1'
)
test_accounts.append(account)
print('111',test_accounts)
test_settings=[]
for i in range(1,5):
print('idnex',i)
setting=UploadSettingModel.create(id=i,timeout=5,account=random.choice(test_accounts))
test_settings.append(setting)
print('222',test_settings)
test_videos=[]
for i in range(1,10):
video=YoutubeVideoModel(id=i,video_title='test title'+str(random.choice(range(1,100))))
test_videos.append(video)
print('333',test_videos)
tasks=[]
for i in range(1,5):
t = TaskModel.create(id=i,status=1,video=random.choice(test_videos),setting=random.choice(test_settings))
tasks.append(t)
print('444',tasks)
TaskModel.filter_tasks2( status=None, type=None,uploaded_at=None,setting=None,inserted_at=None,video_title=None,video_id=None,username=None)
TaskModel.filter_tasks2( status=None, type=None,uploaded_at=None,setting=None,inserted_at=None,video_title='test',video_id=None,username=None)
you will see show att works but show fks falied
That code does not work either, it dies on the following line:
t = TaskModel.create(id=i,status=1,video=random.choice(test_videos),setting=random.choice(test_settings))
Because you never called .create()
on the YoutubeVideoModel... Bro I mean ... this is just shit.
When I finally fix your broken code and run it, I get the following output (no errors): out.txt.
I am sorry for this trouble