sqlalchemy ORM:如何声明包含多个键的表类?

时间:2017-03-10 05:31:09

标签: sqlalchemy

我是Sqlalchemy的新手。 我在USERNAME列中有一个包含多个键的表。 这就是我在模型中所做的。

型号:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root@localhost/admin'
db = SQLAlchemy(app) 

class RADUSAGE(db.Model):
    __tablename__ = 'RADUSAGE'
    USERNAME = db.Column(db.String(513))
    AGE = db.Column(db.Integer)

    def __init__(self, USERNAME, AGE):
       self.USERNAME = USERNAME
       self.AGE = AGE

def __repr__(self):
    return '<RADUSAGE %r>' % self.USERNAME

但我收到了一个错误:

File "/Users/admin/rad_env/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 530, in map
 **self.mapper_args
File "<string>", line 2, in mapper
File "/Users/admin/rad_env/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 677, in __init__
self._configure_pks()
File "/Users/admin/rad_env/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 1277, in _configure_pks
(self, self.mapped_table.description))
sqlalchemy.exc.ArgumentError: Mapper Mapper|RADUSAGE|RADUSAGE could not assemble any primary key columns for mapped table 'RADUSAGE'

如何在sqlalchemy中声明包含多个键的表类?希望可以有人帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您应该能够在每个PK列的映射中指定primary_key = true:

USERNAME = db.Column(db.String(513), primary_key=True)
AGE = db.Column(db.Integer, primary_key=True)

你试过吗?