最新消息:20190529 VPS服务器已从腾讯云香港换为Vultr新加坡,主题仍用朋友推荐的大前端D8

[已解决]SQLAlchemy执行create_all初始化enum时出错:AttributeError: replace

SQLAlchemy crifan 751浏览 0评论

在:

[已解决]SQLAlchemy中添加枚举类型字段

之后,代码:

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/db_create.py

db.drop_all()
db.create_all()

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/models.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from sipevents import app
from . import db
# from app import db
# from datetime import datetime
import enum
event_joiners = db.Table(‘event_joiners’,
    db.Column(‘event_id’, db.Integer, db.ForeignKey(‘events.id’)),
    db.Column(‘joiner_openid’, db.Integer, db.ForeignKey(‘wechat_users.openid’))
)
class User(db.Model):
    __tablename__ = ‘wechat_users’
    # Columns
    openid = db.Column(db.String(64), primary_key=True, nullable=False)
    province = db.Column(db.String(16))
    avatar_url = db.Column(db.String(256))
    avatar_static_path = db.Column(db.String(256))
    language = db.Column(db.String(8))
    city = db.Column(db.String(16))
    country = db.Column(db.String(32))
    sex = db.Column(db.SmallInteger)
    nickname = db.Column(db.String(128))
    events = db.relationship(‘Event’, backref = ‘creator’, lazy = ‘dynamic’)
    def __init__(self,
                 openid,
                 province = “”,
                 avatar_url = “”,
                 avatar_static_path = “”,
                 language = “”,
                 city = “”,
                 country = “”,
                 sex = 0,
                 nickname = “”):
        self.openid     = openid
        self.province   = province
        self.avatar_url = avatar_url
        self.avatar_static_path = avatar_static_path
        self.language   = language
        self.city       = city
        self.country    = country
        self.sex        = sex
        self.nickname   = nickname
    def __repr__(self):
        #return u'<User %r %s>’ % (self.nickname, self.openid)
        return ‘<User nickname=%r openid=%s avatar_static_path=%s>’ % (self.nickname, self.openid, self.avatar_static_path)
class NotificationTime(enum.Enum):
    NotNotify = u”不提醒”
    WhenEventHappen = u”事件发生时”
    FiveMinutesBefore = u”5分钟前”
    FiftyMinutesBefore = u”15分钟前”
    ThirtyMinutesBefore = u”30分钟前”
    OneHourBefore = u”1小时前”
    TwoHoursBefore = u”2小时前”
    OneDayBefore = u”1天前”
    TwoDaysBefore = u”2天前”
    OneWeekBefore = u”1周前”
class Event(db.Model):
    __tablename__ = “events”
    # Columns
    id = db.Column(db.Integer, primary_key = True, autoincrement = True, nullable=False)
    #user_openid = db.Column(db.String(64))
    #user_openid = db.Column(db.String(64), db.ForeignKey(‘user.openid’))
    user_openid = db.Column(db.String(64), db.ForeignKey(‘wechat_users.openid’))
    title = db.Column(db.String(128))
    start_date = db.Column(db.DateTime)
    end_date = db.Column(db.DateTime)
    location = db.Column(db.String(256))
    cur_user_num = db.Column(db.Integer)
    max_user_num = db.Column(db.Integer)
    is_public = db.Column(db.Boolean)
    description = db.Column(db.Text)
    notification_time = db.Column(db.Enum(NotificationTime))
    # joiners = db.relationship(‘EventJoiner’, backref=’user’, lazy=’dynamic’)
    #joiners = db.relationship(‘User’, secondary = event_joiners, backref=’events’)
    #joiners = db.relationship(‘User’, secondary=event_joiners, backref=’joined_events’,lazy=’dynamic’)
    joiners = db.relationship(‘User’, secondary=event_joiners, backref=’joined_events’)
    def __init__(self,
                 # id,
                 user_openid,
                 title = “”,
                 location = “”,
                 start_date = None,
                 end_date = None,
                 cur_user_num = 0,
                 max_user_num = 0,
                 is_public = False,
                 description = “”,
                 notification_time = NotificationTime.NotNotify):
        # self.id = id
        self.user_openid = user_openid
        self.title = title
        self.location = location
        self.start_date = start_date
        self.end_date = end_date
        self.cur_user_num = cur_user_num
        self.max_user_num = max_user_num
        self.is_public = is_public
        self.description = description
        self.notification_time = notification_time
    def __repr__(self):
        return u'<Event id=%d user_openid=%s title=%r>’ % (self.id, self.user_openid, self.title)

然后出错:

DEBUG in __init__ [/usr/share/nginx/html/SIPEvents/sipevents/__init__.py:25]:
db=<SQLAlchemy engine=’sqlite:////usr/share/nginx/html/SIPEvents/instance/sipevents.db’>

<div–<——————————————————————————

Traceback (most recent call last):
  File “db_create.py”, line 42, in <module>
    db.create_all()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py”, line 972, in create_all
    self._execute_for_all_tables(app, bind, ‘create_all’)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py”, line 964, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/schema.py”, line 3745, in create_all
    tables=tables)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1856, in _run_visitor
    conn._run_visitor(visitorcallable, element, **kwargs)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1481, in _run_visitor
    **kwargs).traverse_single(element)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py”, line 121, in traverse_single
    return meth(obj, **kw)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py”, line 730, in visit_metadata
    _is_metadata_operation=True)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py”, line 121, in traverse_single
    return meth(obj, **kw)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py”, line 764, in visit_table
    include_foreign_key_constraints=include_foreign_key_constraints
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 914, in execute
    return meth(self, multiparams, params)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py”, line 68, in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 962, in _execute_ddl
    compiled = ddl.compile(dialect=dialect)
  File “<string>”, line 1, in <lambda>
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/elements.py”, line 494, in compile
    return self._compiler(dialect, bind=bind, **kw)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py”, line 26, in _compiler
    return dialect.ddl_compiler(dialect, self, **kw)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 190, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 213, in process
    return obj._compiler_dispatch(self, **kwargs)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py”, line 81, in _compiler_dispatch
    return meth(self, **kw)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 2173, in visit_create_table
    create.include_foreign_key_constraints)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 2220, in create_table_constraints
    for constraint in constraints
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 2218, in <genexpr>
    p for p in
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 2226, in <genexpr>
    not getattr(constraint, ‘use_alter’, False)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 213, in process
    return obj._compiler_dispatch(self, **kwargs)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py”, line 93, in _compiler_dispatch
    return meth(self, **kw)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 2369, in visit_check_constraint
    literal_binds=True)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 213, in process
    return obj._compiler_dispatch(self, **kwargs)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py”, line 93, in _compiler_dispatch
    return meth(self, **kw)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 927, in visit_binary
    return self._generate_generic_binary(binary, opstring, **kw)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 944, in _generate_generic_binary
    binary.right._compiler_dispatch(self, **kw)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py”, line 93, in _compiler_dispatch
    return meth(self, **kw)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 527, in visit_grouping
    return “(” + grouping.element._compiler_dispatch(self, **kwargs) + “)”
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py”, line 93, in _compiler_dispatch
    return meth(self, **kw)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 751, in visit_clauselist
    for c in clauselist.clauses)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 748, in <genexpr>
    s for s in
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 751, in <genexpr>
    for c in clauselist.clauses)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py”, line 93, in _compiler_dispatch
    return meth(self, **kw)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 1071, in visit_bindparam
    bindparam, within_columns_clause=True, **kwargs)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 1103, in render_literal_bindparam
    return self.render_literal_value(value, bindparam.type)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py”, line 1118, in render_literal_value
    return processor(value)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/sqltypes.py”, line 171, in process
    value = value.replace(“‘”, “””)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/enum/__init__.py”, line 390, in __getattr__
    raise AttributeError(name)
AttributeError: replace

看起来是enum的问题。

SQLAlchemy enum AttributeError: replace

sqlalchemy enum AttributeError: ‘list’ object has no attribute ‘replace’ – python_tty的专栏 – 博客频道 – CSDN.NET

python – SQLAlchemy column with enum using PEP 435 compliant enum produces table creation error – Stack Overflow

1.1 Changelog — SQLAlchemy 1.1 Documentation

What’s New in SQLAlchemy 1.1? — SQLAlchemy 1.1 Documentation

说是:

最新的SQLAlchemy升级到1.1.0b3后,才支持这个enum的。。。

所以要去:

[已解决]通过pip升级SQLAlchemy为最新的1.1版本

然后就解决了此处的错误了。。。

(SIPEvents) ➜  SIPEvents python db_create.py      

<div–<——————————————————————————

DEBUG in __init__ [/usr/share/nginx/html/SIPEvents/sipevents/__init__.py:25]:
db=<SQLAlchemy engine=’sqlite:////usr/share/nginx/html/SIPEvents/instance/sipevents.db’>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:61]:
crifanUserJson={‘province’: u’\u6c5f\u82cf’, ‘openid’: ‘oswjmv4X0cCXcfkIwjoDfCkeTVVY’, ‘headimgurl’: ‘http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ/0’, ‘language’: u’zh_CN’, ‘city’: u’\u82cf\u5dde’, ‘privilege’: [], ‘country’: u’\u4e2d\u56fd’, ‘nickname’: u’\u793c\u8c8c’, ‘sex’: 1}

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:64]:
existedUser=None

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:82]:
crifanUser=<User nickname=u’\u793c\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:86]:
added crifanUser=<User nickname=u’\u793c\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:89]:
currentUsers=[<User nickname=u’\u793c\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png>]

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:92]:
currentUsersCount=1

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:96]:
firstUser=<User nickname=u’\u793c\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:105]:
startDatetime=2016-08-29 21:58:31.522510

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:108]:
oneDayDelta=1 day, 0:00:00

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:110]:
endDatetime=2016-08-30 21:58:31.522510

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:126]:
eventDict={‘location’: u’\u82cf\u5dde\u56ed\u533a\u6797\u6cc9\u8857299\u53f7\u4e1c\u5357\u5927\u5b66\u5357\u5de5\u9662305′, ‘end_date’: datetime.datetime(2016, 8, 30, 21, 58, 31, 522510), ‘title’: u’\u8ba8\u8bba\u65e5\u5386\u7684\u516c\u4f17\u53f7\u540d\u5b57′, ‘user_openid’: ‘oswjmv4X0cCXcfkIwjoDfCkeTVVY’, ‘cur_user_num’: 3, ‘description’: u’\u5934\u8111\u98ce\u66b4\u8ba8\u8bba\u516c\u4f17\u53f7\u7684\u540d\u5b57′, ‘start_date’: datetime.datetime(2016, 8, 29, 21, 58, 31, 522510), ‘is_public’: False, ‘max_user_num’: 6}

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:140]:
existedAllEvent=[]

<div–<——————————————————————————

Traceback (most recent call last):
  File “/usr/local/lib/python2.7/logging/__init__.py”, line 861, in emit
    msg = self.format(record)
  File “/usr/local/lib/python2.7/logging/__init__.py”, line 734, in format
    return fmt.format(record)
  File “/usr/local/lib/python2.7/logging/__init__.py”, line 465, in format
    record.message = record.getMessage()
  File “/usr/local/lib/python2.7/logging/__init__.py”, line 329, in getMessage
    msg = msg % self.args
  File “/usr/share/nginx/html/SIPEvents/sipevents/models.py”, line 117, in __repr__
    return u'<Event id=%d user_openid=%s title=%r>’ % (self.id, self.user_openid, self.title)
TypeError: %d format: a number is required, not NoneType
Logged from file db_create.py, line 154

<div–<——————————————————————————

DEBUG in db_create [db_create.py:156]:
type(crifanUser)=<class ‘sipevents.models.User’>, crifanUser=<User nickname=u’\u793c\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:161]:
added newEvent=<Event id=1 user_openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY title=u’\u8ba8\u8bba\u65e5\u5386\u7684\u516c\u4f17\u53f7\u540d\u5b57′>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:172]:
currentEvents=[<Event id=1 user_openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY title=u’\u8ba8\u8bba\u65e5\u5386\u7684\u516c\u4f17\u53f7\u540d\u5b57′>]

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:175]:
currentEventsCount=1

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:179]:
firstEvent=<Event id=1 user_openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY title=u’\u8ba8\u8bba\u65e5\u5386\u7684\u516c\u4f17\u53f7\u540d\u5b57′>, firstEvent.creator=<User nickname=u’\u793c\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:181]:
type(firstEvent.joiners)=<class ‘sqlalchemy.orm.collections.InstrumentedList’>, firstEvent.joiners=[<User nickname=u’\u793c\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png>]

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:184]:
eachJoiner=<User nickname=u’\u793c\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:193]:
firstUser=<User nickname=u’\u793c\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:196]:
type(firstUserEvents)=<class ‘sqlalchemy.orm.dynamic.AppenderBaseQuery’>, firstUserEvents=<sqlalchemy.orm.dynamic.AppenderBaseQuery object at 0x7f61650b9190>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_create [db_create.py:199]:
type(firstUserJoinedEvents)=<class ‘sqlalchemy.orm.collections.InstrumentedList’>, firstUserJoinedEvents=[<Event id=1 user_openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY title=u’\u8ba8\u8bba\u65e5\u5386\u7684\u516c\u4f17\u53f7\u540d\u5b57′>]

<div–<——————————————————————————

转载请注明:在路上 » [已解决]SQLAlchemy执行create_all初始化enum时出错:AttributeError: replace

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
62 queries in 0.102 seconds, using 18.88MB memory