在SQLAlchemy中将类映射到多个表

时间:2013-07-05 18:16:17

标签: python orm sqlalchemy mapping cherrypy

# ! /usr/bin/env python
# -*- coding: utf-8 -*-
# login_frontend.py

""" Python        2.7.3
    Cherrypy      3.2.2
    PostgreSQL    9.1
    psycopy2      2.4.5
    SQLAlchemy    0.7.10
"""

我在一个Python / SQLAlchemy类中连接四个表时遇到问题。我正在尝试这个,所以我可以迭代这个类的实例,而不是我从连接ORM的表中得到的命名元组。

为什么这一切?因为我已经开始这样了,而且我走得太远,只是离开它。此外,它必须是可能的,所以我想知道它是如何完成的。

对于这个项目(cherrypy web-frontend),我得到了一个已经完成的包含表类的模块。我把它移到了这篇文章的底部,因为对你来说甚至可能都没有。

以下只是联接多表类尝试的一个示例。我选择了一个简单的案例,其中只有两个表和一个联结表。在这里,我不会写入这些连接表,但它在其他地方是必要的。这就是为什么类可以很好地解决这个问题。


我尝试加入类

这是给定的表类模块和这两个网站的示例的组合:

- Mapping a Class against Multiple Tables
- SQLAlchemy: one classes – two tables

class JoinUserGroupPerson (Base):

    persons = md.tables['persons']
    users = md.tables['users']
    user_groups = md.tables['user_groups']
    groups = md.tables['groups']

    user_group_person =(
        join(persons, users, persons.c.id == users.c.id).
        join(user_groups, users.c.id == user_groups.c.user_id).
        join(groups, groups.c.id == user_groups.c.group_id))

    __table__ = user_group_person

    """ I expanded the redefinition of 'id' to three tables,
        and removed this following one, since it made no difference:
        users_id = column_property(users.c.id, user_groups.c.user_id)
    """

    id = column_property(persons.c.id, users.c.id, user_groups.c.user_id)
    groups_id = column_property(groups.c.id, user_groups.c.group_id)
    groups_name = groups.c.name

    def __init__(self, group_name, login, name, email=None, phone=None):
        self.groups_name = group_name
        self.login = login
        self.name = name
        self.email = email
        self.phone = phone

    def __repr__(self):
        return(
            "<JoinUserGroupPerson('%s', '%s', '%s', '%s', '%s')>" %(
            self.groups_name, self.login, self.name, self.email, self.phone))

使用此连接类

的不同表访问
  • 这是我尝试在另一个模块中查询此类的方法:

    pg = sqlalchemy.create_engine(
        'postgresql://{}:{}@{}:{}/{}'.
        format(user, password, server, port, data))
    Session = sessionmaker(bind=pg)
    s1 = Session()
    
    query = (s1.query(JoinUserGroupPerson).
        filter(JoinUserGroupPerson.login==user).
        order_by(JoinUserGroupPerson.id))
    
        record = {}
        for rowX in query:
            for colX in rowX.__table__.columns:
                record[column.name] = getattr(rowX,colX.name)
    
    
    """ RESULT:
    """
    
    
    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/cherrypy/_cprequest.py", line 656, in respond
        response.body = self.handler()
      File "/usr/local/lib/python2.7/dist-packages/cherrypy/lib/encoding.py", line 228, in __call__
        ct.params['charset'] = self.find_acceptable_charset()
      File "/usr/local/lib/python2.7/dist-packages/cherrypy/lib/encoding.py", line 134, in find_acceptable_charset
        if encoder(encoding):
      File "/usr/local/lib/python2.7/dist-packages/cherrypy/lib/encoding.py", line 86, in encode_string
        for chunk in self.body:
      File "XXX.py", line YYY, in ZZZ
        record[colX.name] = getattr(rowX,colX.name)
    AttributeError: 'JoinUserGroupPerson' object has no attribute 'user_id'
    
  • 然后我检查了表格属性:

    for rowX in query:
        return (u'{}'.format(rowX.__table__.columns))
    
    
    """ RESULT:
    """
    
    
    ['persons.id',
     'persons.name',
     'persons.email',
     'persons.phone',
     'users.id',
     'users.login',
     'user_groups.user_id',
     'user_groups.group_id',
     'groups.id',
     'groups.name']
    
  • 然后我通过使用计数器检查了查询或我的班级是否完全不起作用。 我起床了(计数== 5),所以前两个人加入了桌子。但是当我将条件设置为(count == 6)时,我再次收到第一条错误消息。 AttributeError:'JoinUserGroupPerson'对象没有属性'user_id'。:

    list = []
    for rowX in query:
        for count, colX in enumerate(rowX.__table__.columns):
            list.append(getattr(rowX,colX.name))
            if count == 5:
                break
    return (u'{}'.format(list))
    
    
    """ RESULT:
    """
    
    
    [4, u'user real name', None, None, 4, u'user']
    
    
    """ which are these following six columns:
        persons[id, name, email, phone], users[id, login]
    """
    
  • 然后我检查了每一栏:

    list = []
    for rowX in query:
        for colX in rowX.__table__.columns:
            list.append(colX)
    return (u'{}'.format(list))
    
    
    """ RESULT:
    """
    
    
    [Column(u'id', INTEGER(), table=, primary_key=True, nullable=False, server_default=DefaultClause(, for_update=False)),
     Column(u'name', VARCHAR(length=252), table=, nullable=False),
     Column(u'email', VARCHAR(), table=),
     Column(u'phone', VARCHAR(), table=),
     Column(u'id', INTEGER(), ForeignKey(u'persons.id'), table=, primary_key=True, nullable=False),
     Column(u'login', VARCHAR(length=60), table=, nullable=False),
     Column(u'user_id', INTEGER(), ForeignKey(u'users.id'), table=, primary_key=True, nullable=False),
     Column(u'group_id', INTEGER(), ForeignKey(u'groups.id'), table=, primary_key=True, nullable=False),
     Column(u'id', INTEGER(), table=, primary_key=True, nullable=False),
     Column(u'name', VARCHAR(length=60), table=, nullable=False)]
    
  • 然后我尝试了另外两次直接访问,这让我获得了'id'和'persons.id'的KeyErrors:

    for rowX in query:
        return (u'{}'.format(rowX.__table__.columns['id'].name))
    
    for rowX in query:
        return (u'{}'.format(rowX.__table__.columns['persons.id'].name))
    

结论

我尝试了其他一些东西,这些东西更令人困惑。由于他们没有透露更多信息,我没有添加它们。我不知道我的班级错在哪里。

我想,不知怎的,我必须以某种方式设置类,这只能正确地连接前两个表。但是联接至少部分起作用,因为当'user_groups'表为空时,我也得到了一个空查询。

或许我对这个'user_groups'表的映射做错了。由于连接的某些列是双倍的,因此它们需要一个额外的定义。并且'user_id'已经是人员和用户表的一部分,所以我不得不将它映射两次。

我甚至尝试从联接中删除'user_groups'表,因为它在关系中(与辅助关系)。它给我一个外键错误消息。但也许我做错了。

不可否认,我甚至不知道为什么......

rowX.__table__.columns                  # column names as table name suffix 

...具有与...不同的属性名称。

colX in rowX.__table__.columns        # column names without table names

额外编辑

  • 另一个想法!继承可以实现所有这一切吗?每个类都有自己的映射,但是user_groups类可能是必需的。连接必须在单个类之间。 init()和repr()仍然需要重新定义。

  • 它可能与'user_groups'表有关,因为我甚至无法将其与'groups'或'users'表联系起来。它总是说,类对象没有属性'user_id'。也许这与多对多关系有关。


附件

这是已经给定的SQLAlchemy模块,带有标题,没有关于数据库的特定信息,以及连接表的类:

#!/usr/bin/python
# vim: set fileencoding=utf-8 :

import sqlalchemy
from sqlalchemy import join
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref, column_property

pg = sqlalchemy.create_engine(
    'postgresql://{}@{}:{}/{}'.format(user, host, port, data))

md = sqlalchemy.MetaData(pg, True)
Base = declarative_base()



""" ... following, three of the four joined tables.
    UserGroups isn't necessary, so it wasn't part of the module.
    And the other six classes shouldn't be important for this ...
"""


class Person(Base):
    __table__ = md.tables['persons']

    def __init__(self, name, email=None, phone=None):
        self.name = name
        self.email = email
        self.phone = phone

    def __repr__(self):
        return(
            "<Person(%s, '%s', '%s', '%s')>" %(
            self.id, self.name, self.email, self.phone))

class Group(Base):
    __table__ = md.tables['groups']

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return("<Group(%s, '%s')>" %(self.id, self.name))

class User(Base):
    __table__ = md.tables['users']

    person = relationship('Person')
    groups = relationship(
        'Group', secondary=md.tables['user_groups'], order_by='Group.id',
        backref=backref('users', order_by='User.login'))

    def __init__(self, person, login):
        if isinstance(person, Person):
            self.person = person
        else:
            self.id = person
        self.login = login

    def __repr__(self):
        return("<User(%s, '%s')>" %(self.id, self.login))

也许以下创建数据库的脚本,以及已经给出的脚本,在这里证明是有用的。最后一部分是一些测试数据 - 但列之间应该是标签,没有空格。因此,此脚本也可以找到gist on github

-- file create_str.sql
-- database creation script
-- central script for creating all database objects

-- set the database name
\set strdbname logincore

\c admin

BEGIN;
\i str_roles.sql
COMMIT;

DROP DATABASE IF EXISTS :strdbname;
CREATE DATABASE :strdbname TEMPLATE template1 OWNER str_db_owner
    ENCODING 'UTF8';
\c :strdbname

SET ROLE str_db_owner;

BEGIN;
\i str.sql
COMMIT;
RESET ROLE;





-- file str_roles.sql
-- create roles for the database

-- owner of the database objects
SELECT create_role('str_db_owner', 'NOINHERIT');

-- role for using
SELECT create_role('str_user');

-- make str_db_owner member in all relevant roles
GRANT str_user TO str_db_owner WITH ADMIN OPTION;





-- file str.sql
-- creation of database

-- prototypes
\i str_prototypes.sql

-- domain for non empty text
CREATE DOMAIN ntext AS text CHECK (VALUE<>'');

-- domain for email addresses
CREATE DOMAIN email AS varchar(252) CHECK (is_email_address(VALUE));

-- domain for phone numbers
CREATE DOMAIN phone AS varchar(60) CHECK (is_phone_number(VALUE));

-- persons
CREATE TABLE persons (
    id    serial       PRIMARY KEY,
    name  varchar(252) NOT NULL,
    email email,
    phone phone
);

GRANT SELECT, INSERT, UPDATE, DELETE ON persons TO str_user;
GRANT USAGE ON SEQUENCE persons_id_seq TO str_user;

CREATE TABLE groups (
    id   integer     PRIMARY KEY,
    name varchar(60) UNIQUE NOT NULL
);

GRANT SELECT ON groups TO str_user;

-- database users
CREATE TABLE users (
    id    integer     PRIMARY KEY REFERENCES persons(id) ON UPDATE CASCADE,
    login varchar(60) UNIQUE NOT NULL
);

GRANT SELECT ON users TO str_user;

-- user <-> groups
CREATE TABLE user_groups (
    user_id  integer NOT NULL REFERENCES users(id)
                              ON UPDATE CASCADE ON DELETE CASCADE,
    group_id integer NOT NULL REFERENCES groups(id)
                              ON UPDATE CASCADE ON DELETE CASCADE,
    PRIMARY KEY (user_id, group_id)
);

-- functions
\i str_functions.sql





-- file str_prototypes.sql
-- prototypes for database

-- simple check for correct email address
CREATE FUNCTION is_email_address(email varchar) RETURNS boolean
    AS $CODE$
    SELECT FALSE
    $CODE$ LANGUAGE sql IMMUTABLE STRICT;

-- simple check for correct phone number
CREATE FUNCTION is_phone_number(nr varchar) RETURNS boolean
    AS $CODE$
    SELECT FALSE
    $CODE$ LANGUAGE sql IMMUTABLE STRICT;





-- file str_functions.sql
-- functions for database

-- simple check for correct email address
CREATE OR REPLACE FUNCTION is_email_address(email varchar) RETURNS boolean
    AS $CODE$
    SELECT $1 ~ E'^[A-Za-z0-9.!#$%&\'\*\+\-/=\?\^_\`{\|}\~\.]+@[-a-z0-9\.]+$'
    $CODE$ LANGUAGE sql IMMUTABLE STRICT;

-- simple check for correct phone number
CREATE OR REPLACE FUNCTION is_phone_number(nr varchar) RETURNS boolean
    AS $CODE$
    SELECT $1 ~ E'^[-+0-9\(\)/ ]+$'
    $CODE$ LANGUAGE sql IMMUTABLE STRICT;





-- file fill_str_test.sql
-- test data for database
-- between the columns are supposed to be tabs, no spaces !!!

BEGIN;

COPY persons (id, name, email) FROM STDIN;
1   Joseph Schneider    jschneid@lab.uni.de
2   Test User   jschneid@lab.uni.de
3   Hans Dampf  \N
\.
SELECT setval('persons_id_seq', (SELECT max(id) FROM persons));

COPY groups (id, name) FROM STDIN;
1   IT
2   SSG
\.

COPY users (id, login) FROM STDIN;
1   jschneid
2   tuser
3   dummy
\.

COPY user_groups (user_id, group_id) FROM STDIN;
1   1
2   1
3   2
\.

COMMIT;

1 个答案:

答案 0 :(得分:1)

关于KeyErrorrepr对象的__table__.columns中打印的字符串不是键,因为您有多个id列,名字正在进行中。您可能希望"persons_id"而不是"persons.id",但我建议您打印__table__.columns.keys()以确定。

关于AttributeError:SQLAlchemy默认情况下将列名直接映射到属性,除非您自己定义属性映射,否则就是这样。您在id上将column_property属性定义为persons.c.id, users.c.id, user_groups.c.user_id这一事实意味着这些列中的任何一个都不会直接映射到ORM类上的属性,但它们仍将是在columns集合中。所以你不能使用columns作为属性名称的迭代。

我没有重现您的所有代码/数据,但我将一个更简单的测试用例与3个表(包括m2m关系)放在一起来验证这些项目。