sqlite - Flask, SQLAlchemy: How to make column autoincrement -
i'm defining model this:
class user(usermixin, db.model): __tablename__ = 'users' id = db.column(db.integer, primary_key=true) first = db.column(db.string(64)) last = db.column(db.string(64)) dob = db.column(db.date) street_addr1 = db.column(db.string(64)) street_addr2 = db.column(db.string(64)) city = db.column(db.string(64)) state = db.column(db.string(2)) zip = db.column(db.string(9)) gender = db.column(db.enum('m', 'f')) home_box = db.column(db.string(32)) username = db.column(db.string(64), unique=true, index=true) password_hash = db.column(db.string(128)) @property def password(self): raise attributeerror('password not readable attribute') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) def __repr__(self): return '<user %r>' % self.username @login_manager.user_loader def load_user(user_id): return user.query.get(int(user_id))
views.py:
@main.route('/', methods=['get', 'post']) def index(): form = registrationform() if form.validate_on_submit(): user = user() user.username = form.username.data user.first = form.first_name.data user.last = form.last_name.data user.city = form.city.data user.dob = form.dob.data user.gender = form.gender.data user.home_box = form.home_box.data user.state = form.state.data user.street_addr1 = form.street_addr1.data user.street_addr2 = form.street_addr2.data user.zip = form.zip.data user.password = form.password.data db.session.add(user) db.session.commit
right now, inserts failing because id not set:
sqlalchemy.exc.integrityerror: (integrityerror) constraint failed 'insert users (first, last, dob, street_addr1, street_addr2, city, state, zip, gender, home_box, username, password_hash) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' ('nate', 'reed', '1980-11-11', '', '', '', '', '', 'male', '', 'natereed', 'pbkdf2:sha1:1000$n8jhxedu$24a7724ae9edda81a73e6b92d23aa576ad8284aa')
how make id autoincrement? understanding has done @ table (not column) level. how do in code above?
i figured out problem was trying insert value gender not valid. it's enum values 'm' or 'f'.
it nice if form generated model, eliminate potential error. pointers on doing flask?
Comments
Post a Comment