我正在使用postgres和sqlalchemy来设置一个保留表,其中包含有关特定作业应用程序的信息。当用户申请作业时,应用程序的默认状态为“待处理”。接受预订后,应用程序的状态会发生变化。我正在实现枚举类型以在状态之间切换。我收到一条错误消息,指出这种ENUM类型没有用postgres正确处理。我正在将我的数据库从SQLite切换到postgres,所以这很多都是postgres语法差异。提前谢谢:〜)
[SQL:“创建类型reservation_status_enum AS ENUM('待定','已确认','已拒绝')”]
这是我的模特:
class Reservation(db.Model):
__tablename__ = "reservations"
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
message = db.Column(db.String, nullable=False)
status = db.Column(db.Enum('pending', 'confirmed', 'rejected', name='reservation_status_enum'), default='pending')
anonymous_phone_number = db.Column(db.String, nullable=True)
guest_id = db.Column(db.Integer, db.ForeignKey('users.id'))
job_task_id = db.Column(db.Integer, db.ForeignKey('job_listings.id'))
guest = db.relationship("User", back_populates="reservations")
job_task = db.relationship("JobTask", back_populates="reservations")
def __init__(self, message, job_task, guest):
self.message = message
self.guest = guest
self.job_task = job_task
self.status = 'pending'
这是我的sqlalchemy设置:
op.create_table('reservations',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('message', sa.String(), nullable=False),
sa.Column('status', sa.Enum('pending', 'confirmed', 'rejected', name='reservation_status_enum'),
nullable=True),
sa.Column('anonymous_phone_number', sa.String(), nullable=True),
sa.Column('guest_id', sa.Integer(), nullable=True),
sa.Column('job_task_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['guest_id'], ['users.id'], ),
sa.ForeignKeyConstraint(['job_task_id'], ['job_listings.id'], ),
sa.PrimaryKeyConstraint('id')
)
这是我的stacktrace:
File "/Users/shai/Development/Python/mask_num/gigaware/venv/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 450, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) type "reservation_status_enum" already exists [SQL: "CREATE TYPE reservation_status_enum AS ENUM ('pending', 'confirmed', 'rejected')"]