SQLAlchemy는 내 인스턴스 만 내 대량의 관계에서 하나만로드하는 이유는 무엇입니까?
-
23-12-2019 - |
문제
나는 내 모델에서 많은 관계에 속성을 추가하려고하고, 이제 객체를로드 할 때 나는 내 쿼리에서 하나의 관련 개체를 가져 오는 것입니다.
특히 주어진 백업과 연관된 모든 앱을 얻으려고하고 있으며 backupApp.Status 열에도 있습니다.
info-dump에 대한 죄송합니다.
코드가 여기 :
나는로드와 수백 가지의 관계가 있다는 것을 알고있다 :
$ sqlite3 dev.db "select count(*) from backups_apps where backup_id = 3 and app_id is not null;"
415
.
그러나 나는 단지 하나만 얻고있다 :
{
"children": [
{
"children": [
{
"bundle_id": "com.iojoe.10",
"compressed": 13151750,
"name": "10",
"size": 14458882
}
],
"name": "Games"
}
],
"name": "root"
}
.
모델 :
# -*- coding: utf-8 -*-
from app_sizer.database import db, CRUDMixin, TimestampMixin, jsonify_plist
from sqlalchemy.ext.associationproxy import association_proxy
class App(db.Model, CRUDMixin, TimestampMixin):
__tablename__ = 'apps'
bundle_id = db.Column(db.String, unique=True, index=True)
app_id = db.Column(db.Integer, unique=True, index=True)
path = db.Column(db.String, unique=True, index=True)
name = db.Column(db.String, index=True)
main_genre = db.Column(db.String, index=True)
compressed_size = db.Column(db.Integer)
full_size = db.Column(db.Integer)
plist = db.Column(db.PickleType)
plist_json = db.Column(db.Text, default=jsonify_plist, onupdate=jsonify_plist)
subgenre_1 = db.Column(db.String, index=True)
subgenre_2 = db.Column(db.String, index=True)
@classmethod
def get_by_bundle_id(cls, bundle_id):
return db.session.query(cls).filter(App.bundle_id == bundle_id).first()
@classmethod
def get_by_path(cls, path):
return db.session.query(cls).filter(App.path == path).first()
@classmethod
def get_all(cls):
return db.session.query(cls).all()
"""
class AppTag(CRUDMixin):
app_id = db.Column(db.Integer, foreign_key='app.id')
user_id = db.Column(db.Integer, foreign_key='user.id')
tag = db.Column(db.String)
"""
# db.Index('idx_tag_user', 'app_tag.user_id', 'app_tag.tag')
class Backup(db.Model, CRUDMixin, TimestampMixin):
__tablename__ = 'backups'
path = db.Column(db.String, unique=True)
name = db.Column(db.String)
notes = db.Column(db.Text)
# Springboard plist
plist = db.Column(db.PickleType)
plist_json = db.Column(db.Text, default=jsonify_plist, onupdate=jsonify_plist)
@classmethod
def get_by_path(cls, path):
return db.session.query(Backup).filter(Backup.path == path).first()
@classmethod
def get_all(cls):
return db.session.query(Backup).all()
# Why does this work but a declarative class doesn't? No idea.
# http://xsnippet.org/359350/
# http://stackoverflow.com/q/5756559/25625
class BackupApp(db.Model, CRUDMixin):
__tablename__ = 'backups_apps'
backup_id = db.Column(db.Integer, db.ForeignKey('backups.id'), nullable=False, default=1)
app_id = db.Column(db.Integer, db.ForeignKey('apps.id'), nullable=False, default=1)
status = db.Column(db.String)
app = db.relationship(App, backref="installed_apps")
backup = db.relationship(Backup, backref="installed_apps")
def __init__(self, app=None, backup=None, status=None):
self.app = app
self.backup = backup
self.status = status
Backup.apps = association_proxy("installed_apps", "app")
App.backups = association_proxy("installed_apps", "backup")
.
동작 :
@blueprint.route("/backup/<int:backup_id>/apps.json")
def apps_json_for_backup(backup_id):
print "in apps_json_for_backup({})".format(backup_id)
backup = Backup.get_by_id(backup_id)
root = to_treemap(backup.apps)
return jsonify(root)
def to_treemap(apps):
root = {"name": "root", "children": list()}
genres = dict()
for app in apps:
if app.main_genre not in genres:
genres[app.main_genre] = list()
leaf = dict(name=app.name, size=app.full_size, compressed=app.compressed_size, bundle_id=app.bundle_id)
genres[app.main_genre].append(leaf)
for genre, leaves in genres.items():
root['children'].append(dict(name=genre, children=leaves))
return root
. 해결책
확인, 여기에 일어난 일이 있습니다.
처음 backups_apps 테이블을 만들면 CRUDMIXIN을 사용하지 않으므로 ID 열이 없었습니다.ID 열을 마이그레이션 할 때 0으로 초기화 된 모든 값을 0으로 초기화 한 모든 값을 조용히 조용히 동일한 ID로 모든 여러 행을 무시했습니다.
제휴하지 않습니다 StackOverflow