【问题标题】:primary key column isn't autoincrmenting in sqlalchemy (IntegrityError)主键列在 sqlalchemy 中没有自动增加(IntegrityError)
【发布时间】:2015-12-07 21:26:37
【问题描述】:

我有一个 sqlalchemy 模型和一个向表中添加一些元素的方法。这是模型和功能:

class Roles(alchemyDB.Model):
    __tablename__ = 'roles'
    id = alchemyDB.Column(alchemyDB.Integer, primary_key = True)
    name = alchemyDB.Column(alchemyDB.String(64), unique = True)
    default = alchemyDB.Column(alchemyDB.Boolean, default = False, index = True)
    permissions = alchemyDB.Column(alchemyDB.Integer)
    users = alchemyDB.relationship('User', backref='role', lazy='dynamic')

    def __repr__(self):
        return '<Role $r>'%self.name

    @staticmethod
    def setRolesInDB(app):
        '''
            sets the basic roles in the database
        '''
        roles = {
                 'User' : (Permission.WRITE_ARTICLES, True),
                 'Moderator' : (Permission.WRITE_ARTICLES | Permission.MODERATE_COMMENTS, False),
                 'Administrator' : (0xff, False)
                 }
        with app.app_context():
            with alchemyDB.session.no_autoflush:
                for r in roles :
                    role = Roles.query.filter_by(name = r).first()            
                    if role == None :
                        role = Roles(name = r)
                    role.permissions = roles[r][0]
                    role.default = roles[r][1]
                    alchemyDB.session.add(role)
                alchemyDB.session.commit()


class User(UserMixin, alchemyDB.Model):
    __tablename__ = 'users'
    id = alchemyDB.Column(alchemyDB.Integer, primary_key = True)
    user_email = alchemyDB.Column(alchemyDB.String(64), unique = True, index = True)
    user_name = alchemyDB.Column(alchemyDB.String(64), unique = True, index = True)
    user_pass = alchemyDB.Column(alchemyDB.String(128))
    role_id = alchemyDB.Column(alchemyDB.Integer, alchemyDB.ForeignKey('roles.id'))
    communities = alchemyDB.Column(alchemyDB.String(64))
    articles_compared = alchemyDB.Column(alchemyDB.String(64))
    date_joined = alchemyDB.Column(alchemyDB.DateTime)
    user_tags =  alchemyDB.relationship('UsersAppliedTags',
                                        foreign_keys = [UsersAppliedTags.user_id],
                                        backref = alchemyDB.backref('users_who_have_used_this_tag_for_this_article', lazy ='joined'),
                                        lazy = 'dynamic',
                                        cascade = 'all, delete-orphan')

这是我调用函数的方式:

alchemyDB.init_app(app)
with app.app_context():
        # Extensions like Flask-SQLAlchemy now know what the "current" app
        # is while within this block. Therefore, you can now run........
    alchemyDB.create_all()
Roles.setRolesInDB(app)

我收到以下错误:

cursor.execute(statement, parameters) 

sqlalchemy.exc.IntegrityError: (IntegrityError) roles.id 可能不是 NULL u'INSER T INTO 角色(名称、“默认”、权限) VALUES (?, ?, ?)' ('版主', 0, 1 2)

这个表和其他表一样,我从来没有遇到过主键突然没有自动递增的问题。这里可能出了什么问题?

根据要求,数据库中的表如下所示:

sqlite> pragma table_info(Roles);
0|id|VARCHAR(64)|1||1
1|name|VARCHAR(64)|0||0
2|default|BOOLEAN|0||0
3|permissions|INTEGER|0||0

【问题讨论】:

  • 显示direct表结构(在数据库中)
  • Tomasz,你的意思是在我的帖子中显示我所有的表格吗?
  • 不,只有角色表
  • 我为角色表添加了表信息。我想我将模型从具有字符串主键(这是一个错误)更改为整数。如果您发现这是错误的,请发布,我将标记为答案。
  • Tomasz,我发现了问题,您的请求将我直接指向它,谢谢。

标签: python sqlalchemy


【解决方案1】:

id 列的类型更改为INTEGER 并使其成为autoincrement

【讨论】:

    猜你喜欢
    • 2023-03-13
    • 2018-06-05
    • 2021-03-11
    • 1970-01-01
    • 2016-05-13
    • 2022-01-25
    • 1970-01-01
    • 2012-07-14
    • 2013-09-30
    相关资源
    最近更新 更多