Python 3.5将记录插入到另一个模式的Postgres表中

时间:2016-02-16 23:15:35

标签: sqlalchemy schema insert-into python-3.5

.....我的postgres db class ......

class User(db.Model):
    """docstring for User"""

    __tablename__ = 'user'
    user_name = db.Column(db.String(45), primary_key=True)
    email = db.Column(db.String(45))

    def __init__(self, user_name, email):
        super(User, self).__init__()
        self.user_name = user_name
        self.email = email

.....尝试插入时我收到错误消息

@app.route('/add_user', methods=['POST'])
def add_user():
    user = User(user_name = request.form['user_name'], email = request.form['email'])
    print(user.user_name, user.email)
    db.session.add(user)
    db.session.commit()
    return render_template('get_address.html')

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "user" does not exist LINE 1: INSERT INTO "user" (user_name, email) VALUES ('as', 'sd') ^ [SQL: 'INSERT INTO "user" (user_name, email) VALUES (%(user_name)s, %(email)s)'] [parameters: {'email': 'sd', 'user_name': 'as'}]

.....正在使用多个模式 .....上面的类在一个名为users

的模式中

我认为问题是表名解析:即数据库中的表'用户'?我认为必须有资格使用表'用户'模式名称为__tablename__ = 'users.user'。这不起作用。

......产生的错误是:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "users.user" does not exist LINE 1: INSERT INTO "users.user" (user_name, email) VALUES ('po', 'p... ^ [SQL: 'INSERT INTO "users.user" (user_name, email) VALUES (%(user_name)s, %(email)s)'] [parameters: {'email': 'po', 'user_name': 'po'}]

我无法在SqlAlchemy.org网站或其他任何地方找到任何有关任何构造的信息或如何解决此问题。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用schema argument to Table指定架构名称:

class User(db.Model):
    ...
    __table_args__ = {
        "schema": "users"
    }