I am using Flask extension for SQLAlchemy to define my database model. I want an id column to be int type and with auto_increment property but without making it a primary key. How do I achieve it?
I tried this:
from flask import Flask, jsonify
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:ajay@localhost/pydb'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
class Scenario(db.Model):
scid = db.Column(db.Integer, nullable=False, unique=True, autoincrement=True)
scenario_name = db.Column(db.String(100), primary_key=True)
scenario_description = db.Column(db.String(200), nullable=False)
image_id = db.Column(db.Integer, db.ForeignKey('images.id', onupdate='CASCADE', ondelete='CASCADE'))
def __init__(self, scenario_name, scenario_description, image_id=None):
self.scenario_name = scenario_name
self.scenario_description = scenario_description
self.image_id = image_id
def __repr__(self):
return '<Scenario %r, %r, %r>' % (self.scenario_name, self.scenario_description, self.image_id)
but this doesn't set the scid column as auto_increment.
You can add an AUTO_INCREMENT
not primary key but you can't have two AUTO_INCREMENT
fields
If you don't have AUTO_INCREMENT
you can add an AUTO_INCREMENT
and UNIQUE
whith something like this:
ALTER TABLE `items` ADD `AutoInc` INT NOT NULL AUTO_INCREMENT, ADD UNIQUE (`AutoInc`)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.