运行迁移时出现sqlalchemy.exc.ArgumentError

时间:2020-07-06 20:55:47

标签: python-3.x flask flask-sqlalchemy

我在尝试运行迁移时遇到Argument错误,但是我无法弄清楚问题出在哪里,要实现的是两个模型之间的关系。下面是我的模型课:

class TourPackages(db.Model):
    __tablename__ = 'tour_package'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    description = db.Column(db.TEXT)
    price = db.Column(db.Float)
    destination = db.relationship('Destinations', backref='tourpackages', lazy=True)
    capacity = db.Column(db.Integer)

    @property
    def serialize(self):
        return {
            'name': self.name,
            'description': self.destination,
            'price': self.price,
            'destination': self.destination,
            'capacity': self.capacity
        }


class Destinations(db.Model):
    __tablename__ = 'destination'

    id = db.Column(db.Integer)
    location = db.Column(db.String(50))
    danger_type = db.Column(db.String(50))

当我使用以下命令运行迁移时:

flask db migrate -m "Initial migration."

我收到此错误:

sqlalchemy.exc.ArgumentError: Mapper mapped class Destinations->destination could not assemble any primary key columns for mapped table 'destination'

我在这里做什么错

1 个答案:

答案 0 :(得分:1)

在Destinations模型中,您需要将id列更改为主键

id = db.Column(db.Integer, primary_key=True)

并再次在“目的地”中为该关系添加外键列:

tourpackages_id = db.Column(db.Integer, db.ForeignKey('tour_package.id'))

请参见documentation