已有Flask的sqlalchemy的数据库的表,
其中有个id的字段是:autoincrement,且是primary_key
class Event(db.Model): __tablename__ = “events” # Columns id = db.Column(db.Integer, primary_key = True, autoincrement = True) #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) 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 = “”): 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 |
此处的id是自增autoincrement的
但是对于自己的__init__
由于设置了id的初始化
-》导致新生成Event对象是,不传入id,就没法自动创建了。
所以要去搞清楚:
autoincrement如何使用
flask sqlalchemy autoincrement 如何自动增加
python – unable to create autoincrementing primary key with flask-sqlalchemy – Stack Overflow
Pyhton日记——Flask写一个简易的知乎日报API – 简书
flask开发restful api系列(3)–利用alembic进行数据库更改 – 月儿弯弯0204 – 博客园
class User(Base): 22 __tablename__ = ‘user’ 23 24 id = Column(‘id’, Integer, primary_key=True) 25 phone_number = Column(‘phone_number’, String(11), index=True) 26 password = Column(‘password’, String(30)) 27 nickname = Column(‘nickname’, String(30), index=True, nullable=True) 28 head_picture = Column(‘head_picture’, String(100), default=”) 29 register_time = Column(‘register_time’, DateTime, index=True, default=datetime.datetime.now) |
好像不用__init__就可以了?
模型声明 — Flask-SQLAlchemy 0.16 documentation
flask sqlalchemy how to use autoincrement
__init__时候不初始化?
然后去用:
requestForm = request.form app.logger.debug(“requestForm=%s”, requestForm) title = requestForm.get(“title”, “”) start_date = requestForm.get(“start_date”, “”) end_date = requestForm.get(“end_date”, “”) location = requestForm.get(“location”, “”) max_user_num = requestForm.get(“max_user_num”, “”) is_public = requestForm.get(“is_public”, “”) description = requestForm.get(“description”, “”) notification_time = requestForm.get(“notification_time”, “”) app.logger.debug(“title=%s, start_date=%s, end_date=%s, location=%s, max_user_num=%s, is_public=%s, description=%s, notification_time=%s”, title, start_date, end_date, location, max_user_num, is_public, description, notification_time) # 2016/8/26 10:09 start_date = datetime.strptime(start_date, “%Y/%m/%d %H:%M”) app.logger.debug(“start_date=%s”, start_date) end_date = datetime.strptime(end_date, “%Y/%m/%d %H:%M”) app.logger.debug(“end_date=%s”, end_date) #is_public = (is_public == “on”) ? True : False is_public = (is_public == “on”) app.logger.debug(“is_public=%s”, is_public) newEvent = Event(user_openid = curUserOpenId, title = title, location = location, start_date = start_date, end_date = end_date, cur_user_num = 1, max_user_num = max_user_num, is_public = is_public, description = description) app.logger.debug(‘newEvent=%s’, newEvent) db.session.add(newEvent) db.session.commit() app.logger.debug(“added new event=%s”, newEvent) return redirect(url_for(‘index’, curUser = curUser)) |
即可:
DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:362]: added new event=<Event id=2 user_openid= title=u’\u89c2\u81f4\u9700\u6c42′> |
转载请注明:在路上 » [已解决]Flask的sqlalchemy中数据库表的autoincrement字段如何实现自动增加