简体   繁体   English

sqlalchemy经典映射关系问题

[英]sqlalchemy classical mapping relationship issue

I am on a mission to finally learn sqlAlchemy so that I can reap the benefits in the years to come. 我的任务是最终学习sqlAlchemy,这样我才能在未来几年中获益。

I am neck deep in the sqlalchemy documents and have been for the past two days. 我对sqlalchemy的文件很感兴趣并且已经过去两天了。 I am hell bent on learning the classical mapping way, instead of the declarative, bc the db I want to hook up to exists, and does not have a unique id column in all of it's tables. 我很想学习经典的映射方式,而不是声明性的,bc我想要连接的db存在,并且在它的所有表中都没有唯一的id列。 According to this article classical mapping is the way to go under such circumstances 根据这篇文章,经典映射是在这种情况下的方法

I have been following the classical examples from the sqlalchemy site, but I just cannot seem to find the correct relationship configuration to get this to work. 我一直在关注sqlalchemy网站上的经典示例,但我似乎无法找到正确的关系配置来实现这一点。

Here is all my code: 这是我的所有代码:

engine = create_engine(
    "mssql+pyodbc://someaddress/test?driver=FreeTDS?TDS_version=8.0", echo=True)

metadata = MetaData(engine)

class User(object):

    def __repr__(self):
        return "<User(User_id='%s', name='%s', age='%s')>" % (
                            self.user_id, self.name, self.age)
class Email(object):

    def __repr__(self):
        return "<User(email_id='%s', address='%s', user_id='%s')>" % (
                            self.email_id, self.address, self.user_id)

users = Table('users', metadata,
    Column('user_id', Integer, primary_key=True),
    Column('name', String(40)),
    Column('age', Integer),
    schema='test.dbo.users')

mapper(User, users, properties={'Email': relationship(Email, primaryjoin=users.c.user_id==emails.c.user_id)})

emails = Table('emails', metadata,
    Column('email_id', Integer, primary_key=True),
    Column('address', String),
    Column('user_id', Integer, ForeignKey('test.dbo.users.user_id')),
    schema='test.dbo.emails')

mapper(Email, emails)

Session = sessionmaker(bind=engine)
session = Session()

mary = session.query(User, Email).filter(User.user_id == Email.user_id)

The pursuing error message makes it clear that it is the mapper / relationship that is the problem. 追求错误消息清楚表明它是映射器/关系是问题。

InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers.  Original exception was: Could not determine join condition between parent/child tables on relationship User.Email - there are no foreign keys linking these tables.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

I have tried a long list of different things to try to remedy, but I just cannot get to the bottom of it. 我尝试了很多不同的东西来尝试补救,但我无法深究它。

Any pointers in the right direction would be much appreciated! 任何指向正确的方向将非常感谢!

The sqlalchemy version I am on is; 我所使用的sqlalchemy版本是;

'1.0.12'

I had better luck with: 我运气好了:

Column('user_id', Integer, ForeignKey('users.user_id')),

I also reordered the table and mapping code slightly: 我还重新排序了表和映射代码:

users = Table('users', metadata,
    Column('user_id', Integer, primary_key=True),
    Column('name', String(40)),
    Column('age', Integer))

emails = Table('emails', metadata,
    Column('email_id', Integer, primary_key=True),
    Column('address', String),
    # foreign key to table users
    Column('user_id', Integer, ForeignKey('users.user_id')))

mapper(Email, emails)

mapper(User, users, properties={
    'Email': relationship(
        Email, primaryjoin=users.c.user_id==emails.c.user_id)})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM