program tip

SQLAlchemy 버전 관리는 클래스 가져 오기 순서를 걱정합니다.

radiobox 2020. 8. 2. 18:03
반응형

SQLAlchemy 버전 관리는 클래스 가져 오기 순서를 걱정합니다.


나는 여기에 가이드를 따르고 있었다 :

http://www.sqlalchemy.org/docs/orm/examples.html?highlight=versioning#versioned-objects

그리고 문제가 발생했습니다. 나는 나의 관계를 다음과 같이 정의했다 :

generic_ticker = relation('MyClass', backref=backref("stuffs"))

모델 모듈의 가져 오기 순서에 신경 쓰지 않습니다. 이 모든 것이 정상적으로 작동하지만 버전 메타를 사용할 때 다음 오류가 발생합니다.

sqlalchemy.exc.InvalidRequestError : 매퍼 Mapper | MyClass | stuffs를 초기화 할 때 'Trader'표현식이 이름을 찾지 못했습니다 ( "이름 'MyClass'가 정의되지 않았습니다"). 이것이 클래스 이름 인 경우, 두 개의 종속 클래스가 모두 정의 된 후이 relation ()을 클래스에 추가하십시오.

나는 오류를 추적했다.

  File "/home/nick/workspace/gm3/gm3/lib/history_meta.py", line 90, in __init__
    mapper = class_mapper(cls)
  File "/home/nick/venv/tg2env/lib/python2.6/site-packages/sqlalchemy/orm/util.py", line 622, in class_mapper
    mapper = mapper.compile()

class VersionedMeta(DeclarativeMeta):
    def __init__(cls, classname, bases, dict_):
        DeclarativeMeta.__init__(cls, classname, bases, dict_)

        try:
            mapper = class_mapper(cls)
            _history_mapper(mapper)
        except UnmappedClassError:
            pass

나는 람다에 물건을 제외하고 모든 수입이 일어난 후에 모든 것을 실행하여 시도해 보았습니다. 이것은 작동하지만 약간 쓰레기처럼 보입니다.이 문제를 해결하는 방법에 대한 아이디어가 더 나은 방법입니까?

감사!

최신 정보

문제는 실제로 수입 주문에 관한 것이 아닙니다. 버전 관리 예제는 매퍼가 각 버전 관리 클래스의 생성자에서 컴파일을 요구하도록 설계되었습니다. 관련 클래스가 아직 정의되어 있지 않으면 컴파일이 실패합니다. 순환 관계의 경우 매핑 된 클래스의 정의 순서를 변경하여 작동시킬 수있는 방법이 없습니다.

업데이트 2

위의 업데이트 상태 (여기에서 다른 사람들의 게시물을 편집 할 수있는 줄 몰랐습니다 :)는 순환 참조 때문일 수 있습니다. 어떤 경우 누군가 누군가 내 핵을 유용하게 사용할 수 있습니다 (터보 기어와 함께 사용하고 있습니다) (VersionedMeta를 교체하고 history_meta에서 전역 create_mappers에 추가하십시오)

create_mappers = []
class VersionedMeta(DeclarativeMeta):
    def __init__(cls, classname, bases, dict_):
        DeclarativeMeta.__init__(cls, classname, bases, dict_)
        #I added this code in as it was crashing otherwise
        def make_mapper():
            try:
                mapper = class_mapper(cls)
                _history_mapper(mapper)
            except UnmappedClassError:
                pass

        create_mappers.append(lambda: make_mapper())

그런 다음 모델에서 다음과 같은 작업을 수행 할 수 있습니다 __init__.py

# Import your model modules here.
from myproj.lib.history_meta import create_mappers

from myproj.model.misc import *
from myproj.model.actor import *
from myproj.model.stuff1 import *
from myproj.model.instrument import *
from myproj.model.stuff import *

#setup the history
[func() for func in create_mappers]

그렇게하면 모든 클래스가 정의 된 후에 만 ​​매퍼를 만듭니다.

업데이트 3 약간 관련이 없지만 일부 상황에서 중복 기본 키 오류가 발생했습니다 (한 번에 동일한 객체에 2 개의 변경 사항을 커밋). 해결 방법은 새로운 기본 자동 증분 키를 추가하는 것입니다. 물론 mysql을 사용하여 1 개를 초과 할 수 없으므로 기록 테이블을 만드는 데 사용되는 기존 항목의 키를 우선적으로 지정해야했습니다. 내 전체 코드 (hist_id 포함 및 외래 키 제약 조건 제거)를 확인하십시오.

"""Stolen from the offical sqlalchemy recpies
"""
from sqlalchemy.ext.declarative import DeclarativeMeta
from sqlalchemy.orm import mapper, class_mapper, attributes, object_mapper
from sqlalchemy.orm.exc import UnmappedClassError, UnmappedColumnError
from sqlalchemy import Table, Column, ForeignKeyConstraint, Integer
from sqlalchemy.orm.interfaces import SessionExtension
from sqlalchemy.orm.properties import RelationshipProperty
from sqlalchemy.types import DateTime
import datetime
from sqlalchemy.orm.session import Session

def col_references_table(col, table):
    for fk in col.foreign_keys:
        if fk.references(table):
            return True
    return False

def _history_mapper(local_mapper):
    cls = local_mapper.class_

    # set the "active_history" flag
    # on on column-mapped attributes so that the old version
    # of the info is always loaded (currently sets it on all attributes)
    for prop in local_mapper.iterate_properties:
        getattr(local_mapper.class_, prop.key).impl.active_history = True

    super_mapper = local_mapper.inherits
    super_history_mapper = getattr(cls, '__history_mapper__', None)

    polymorphic_on = None
    super_fks = []
    if not super_mapper or local_mapper.local_table is not super_mapper.local_table:
        cols = []
        for column in local_mapper.local_table.c:
            if column.name == 'version':
                continue

            col = column.copy()
            col.unique = False

            #don't auto increment stuff from the normal db
            if col.autoincrement:
                col.autoincrement = False
            #sqllite falls over with auto incrementing keys if we have a composite key
            if col.primary_key:
                col.primary_key = False

            if super_mapper and col_references_table(column, super_mapper.local_table):
                super_fks.append((col.key, list(super_history_mapper.base_mapper.local_table.primary_key)[0]))

            cols.append(col)

            if column is local_mapper.polymorphic_on:
                polymorphic_on = col

        #if super_mapper:
        #    super_fks.append(('version', super_history_mapper.base_mapper.local_table.c.version))

        cols.append(Column('hist_id', Integer, primary_key=True, autoincrement=True))
        cols.append(Column('version', Integer))
        cols.append(Column('changed', DateTime, default=datetime.datetime.now))

        if super_fks:
            cols.append(ForeignKeyConstraint(*zip(*super_fks)))

        table = Table(local_mapper.local_table.name + '_history', local_mapper.local_table.metadata,
                      *cols, mysql_engine='InnoDB')
    else:
        # single table inheritance.  take any additional columns that may have
        # been added and add them to the history table.
        for column in local_mapper.local_table.c:
            if column.key not in super_history_mapper.local_table.c:
                col = column.copy()
                super_history_mapper.local_table.append_column(col)
        table = None

    if super_history_mapper:
        bases = (super_history_mapper.class_,)
    else:
        bases = local_mapper.base_mapper.class_.__bases__
    versioned_cls = type.__new__(type, "%sHistory" % cls.__name__, bases, {})

    m = mapper(
            versioned_cls, 
            table, 
            inherits=super_history_mapper, 
            polymorphic_on=polymorphic_on,
            polymorphic_identity=local_mapper.polymorphic_identity
            )
    cls.__history_mapper__ = m

    if not super_history_mapper:
        cls.version = Column('version', Integer, default=1, nullable=False)

create_mappers = []

class VersionedMeta(DeclarativeMeta):
    def __init__(cls, classname, bases, dict_):
        DeclarativeMeta.__init__(cls, classname, bases, dict_)
        #I added this code in as it was crashing otherwise
        def make_mapper():
            try:
                mapper = class_mapper(cls)
                _history_mapper(mapper)
            except UnmappedClassError:
                pass

        create_mappers.append(lambda: make_mapper())

def versioned_objects(iter):
    for obj in iter:
        if hasattr(obj, '__history_mapper__'):
            yield obj

def create_version(obj, session, deleted = False):
    obj_mapper = object_mapper(obj)
    history_mapper = obj.__history_mapper__
    history_cls = history_mapper.class_

    obj_state = attributes.instance_state(obj)

    attr = {}

    obj_changed = False

    for om, hm in zip(obj_mapper.iterate_to_root(), history_mapper.iterate_to_root()):
        if hm.single:
            continue

        for hist_col in hm.local_table.c:
            if hist_col.key == 'version' or hist_col.key == 'changed' or hist_col.key == 'hist_id':
                continue

            obj_col = om.local_table.c[hist_col.key]

            # get the value of the
            # attribute based on the MapperProperty related to the
            # mapped column.  this will allow usage of MapperProperties
            # that have a different keyname than that of the mapped column.
            try:
                prop = obj_mapper.get_property_by_column(obj_col)
            except UnmappedColumnError:
                # in the case of single table inheritance, there may be 
                # columns on the mapped table intended for the subclass only.
                # the "unmapped" status of the subclass column on the 
                # base class is a feature of the declarative module as of sqla 0.5.2.
                continue

            # expired object attributes and also deferred cols might not be in the
            # dict.  force it to load no matter what by using getattr().
            if prop.key not in obj_state.dict:
                getattr(obj, prop.key)

            a, u, d = attributes.get_history(obj, prop.key)

            if d:
                attr[hist_col.key] = d[0]
                obj_changed = True
            elif u:
                attr[hist_col.key] = u[0]
            else:
                # if the attribute had no value.
                attr[hist_col.key] = a[0]
                obj_changed = True

    if not obj_changed:
        # not changed, but we have relationships.  OK
        # check those too
        for prop in obj_mapper.iterate_properties:
            if isinstance(prop, RelationshipProperty) and \
                attributes.get_history(obj, prop.key).has_changes():
                obj_changed = True
                break

    if not obj_changed and not deleted:
        return

    attr['version'] = obj.version
    hist = history_cls()
    for key, value in attr.iteritems():
        setattr(hist, key, value)

    obj.version += 1
    session.add(hist)

class VersionedListener(SessionExtension):
    def before_flush(self, session, flush_context, instances):
        for obj in versioned_objects(session.dirty):
            create_version(obj, session)
        for obj in versioned_objects(session.deleted):
            create_version(obj, session, deleted = True)

나는 람다에 물건을 제외하고 모든 수입이 일어난 후에 모든 것을 실행하여 시도해 보았습니다.

큰!

참고 URL : https://stackoverflow.com/questions/5733113/sqlalchemy-versioning-cares-about-class-import-order

반응형