跨文件的 SQLAlchemy 类 [英] SQLAlchemy classes across files

查看:75
本文介绍了跨文件的 SQLAlchemy 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何将 SQLAlchemy 类分布在多个文件中,但我一生都无法弄清楚如何去做.我对 SQLAlchemy 还很陌生,所以如果这个问题微不足道,请原谅我..

I'm trying to figure out how to have SQLAlchemy classes spread across several files, and I can for my life not figure out how to do it. I am pretty new to SQLAlchemy so forgive me if this question is trivial..

考虑这 3 个类各自的文件:

A.py:

from sqlalchemy import *
from main import Base

class A(Base):
    __tablename__ = "A"
    id  = Column(Integer, primary_key=True)
    Bs  = relationship("B", backref="A.id")
    Cs  = relationship("C", backref="A.id")

B.py:

from sqlalchemy import *
from main import Base

class B(Base):
    __tablename__ = "B"
    id    = Column(Integer, primary_key=True)
    A_id  = Column(Integer, ForeignKey("A.id"))

C.py:

from sqlalchemy import *
from main import Base

class C(Base):
    __tablename__ = "C"    
    id    = Column(Integer, primary_key=True)
    A_id  = Column(Integer, ForeignKey("A.id"))

然后说我们有一个 ma​​in.py 像这样:

And then say we have a main.py something like this:

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref, sessionmaker

Base = declarative_base()

import A
import B
import C

engine = create_engine("sqlite:///test.db")
Base.metadata.create_all(engine, checkfirst=True)
Session = sessionmaker(bind=engine)
session = Session()

a  = A.A()
b1 = B.B()
b2 = B.B()
c1 = C.C()
c2 = C.C()

a.Bs.append(b1)
a.Bs.append(b2)    
a.Cs.append(c1)
a.Cs.append(c2)    
session.add(a)
session.commit()

上面给出了错误:

sqlalchemy.exc.NoReferencedTableError: Foreign key assocated with column 'C.A_id' could not find table 'A' with which to generate a foreign key to target column 'id'

如何在这些文件之间共享声明性基础?

How do I share the declarative base across these files?

考虑到我可能会在上面扔PylonsTurbogears 之类的东西,实现这一目标的正确"方法是什么?

What is the "the right" way to accomplish this, considering that I might throw something like Pylons or Turbogears on top of this?

编辑 10-03-2011

我发现 this 来自 Pyramids 框架的描述,它描述了问题,更重要的是验证这是一个实际问题,而不仅仅是(仅)我困惑的自我问题.希望它可以帮助那些敢于走这条危险道路的人:)

I found this description from the Pyramids framework which describes the problem and more importantly verifies that this is an actual issue and not (only) just my confused self that's the problem. Hope it can help others who dares down this dangerous road :)

推荐答案

解决您问题的最简单方法是从导入 A 的模块中取出 BaseBC;打破循环导入.

The simplest solution to your problem will be to take Base out of the module that imports A, B and C; Break the cyclic import.

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

a.py

from sqlalchemy import *
from base import Base
from sqlalchemy.orm import relationship

class A(Base):
    __tablename__ = "A"
    id  = Column(Integer, primary_key=True)
    Bs  = relationship("B", backref="A.id")
    Cs  = relationship("C", backref="A.id")

b.py

from sqlalchemy import *
from base import Base

class B(Base):
    __tablename__ = "B"
    id    = Column(Integer, primary_key=True)
    A_id  = Column(Integer, ForeignKey("A.id"))

c.py

from sqlalchemy import *
from base import Base

class C(Base):
    __tablename__ = "C"    
    id    = Column(Integer, primary_key=True)
    A_id  = Column(Integer, ForeignKey("A.id"))

main.py

from sqlalchemy import create_engine
from sqlalchemy.orm import relationship, backref, sessionmaker

import base


import a
import b
import c

engine = create_engine("sqlite:///:memory:")
base.Base.metadata.create_all(engine, checkfirst=True)
Session = sessionmaker(bind=engine)
session = Session()

a1 = a.A()
b1 = b.B()
b2 = b.B()
c1 = c.C()
c2 = c.C()

a1.Bs.append(b1)
a1.Bs.append(b2)    
a1.Cs.append(c1)
a1.Cs.append(c2)    
session.add(a1)
session.commit()

适用于我的机器:

$ python main.py ; echo $?
0

这篇关于跨文件的 SQLAlchemy 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆