简体   繁体   English

烧瓶-(sqlite3.IntegrityError)NOT NULL约束即使在迁移后也失败

[英]Flask - (sqlite3.IntegrityError) NOT NULL constraint failed even after migration

I have created a one-to-many database relationship between the following: 我在以下之间创建了一对多数据库关系:

models.py models.py

class User(db.Model, UserMixin):
    __tablename__ = "user"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(60), nullable=False)

    histories = db.relationship('History', backref='admin', lazy=True)

    def save(self):
        db.session.add(self)
        db.session.commit()

    def __repr__(self):
        return f"User('{self.id}', '{self.username}', '{self.email})"


class History(db.Model):
    __tablename__ = "history"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    offender_ip = db.Column(db.String(100), nullable=False)
    offender_mac = db.Column(db.String(100), nullable=False)
    action = db.Column(db.String(100), nullable=False)
    classification = db.Column(db.String(100), nullable=False)
    case_id = db.Column(db.String(100), nullable=False)

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    def save(self):
        db.session.add(self)
        db.session.commit()

    def __repr__(self):
        return f"Post('{self.id}', {self.date_posted}', '{self.offender_ip}', '{self.offender_mac}', '{self.action}'," \
            f" '{self.classification}', '{self.case_id}')"


I get the error when creating an instance of 'History' here: 在此处创建“历史记录”实例时出现错误:

@app.route("/block/add/result", methods=['GET', "POST"])
def block_submit_add():
    print(current_user)
    get_user = "user"
    get_password = "password"

    get_mac = request.form["mac"]
    get_offender_ip = request.form["off_ip"]
    get_classification = request.form["classification"]
    get_case_id = request.form["case_id"]
    now = datetime.now()

    if current_user.is_authenticated:
        history = History(date_posted=now, offender_ip=get_offender_ip,
                          offender_mac=get_mac, action="BLOCKED", classification=get_classification, case_id=get_case_id)
        db.session.add(history)
        db.session.commit()

        user = History.query.filter_by(offender_mac=get_mac).first()

        print(user.offender_ip)
        print(user.classification)
        print(user.get_case_id)


I also have a 'load_user' function that uses 'user_loader' from Flask-Login: 我也有一个使用来自Flask-Login的'user_loader'的'load_user'函数:

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))


Here is the full error: 这是完整的错误:

sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: history.user_id
[SQL: INSERT INTO history (date_posted, offender_ip, offender_mac, action, classification, case_id, user_id) VALUES (?, ?, ?, ?, ?, ?, ?)]
[parameters: ('2019-08-01 23:35:01.351985', '10.0.0.0', '00:00:00:00:00:00', 'BLOCKED', 'Test', '123', None)]



Is there a reason why 'History.user_id' is None? 为什么“ History.user_id”为“无”? I have also tried to do a database migration/upgrade with Alembic and still receive the error. 我也尝试使用Alembic进行数据库迁移/升级,但仍然收到错误。

You never set user_id when you create the new History object: 创建新的History对象时,永远不会设置user_id

history = History(date_posted=now, offender_ip=get_offender_ip,
                  offender_mac=get_mac, action="BLOCKED", classification=get_classification, case_id=get_case_id)

It seems like offender_mac is what you use to identify a user by, if that is true I don't get why you also need a user_id . 似乎offender_mac是您用来标识用户的依据,如果是这样,我不知道为什么您还需要user_id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL 约束失败 - sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed 使用 flask sqlalchemy.exc.IntegrityError 时出错:(sqlite3.IntegrityError) UNIQUE 约束失败 - Error when working with flask sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed 获取 sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL 约束失败:user.image_file - Getting sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: user.image_file sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL 约束失败:user.id - sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: user.id 表未监听其他表中外键设置的值,IntegrityError: (sqlite3.IntegrityError) NOT NULL 约束失败 - Table not listening to value set for foreign key in other table, IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL 约束失败:user.words - sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: user.words web2py:sqlite3.IntegrityError&#39;&gt;(外键约束失败) - web2py: sqlite3.IntegrityError'>(foreign key constraint failed) sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: users.login - sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: users.login 如何修复Web2Py“ <class 'sqlite3.IntegrityError'> (外键约束失败)“? - How to fix Web2Py “<class 'sqlite3.IntegrityError'>(foreign key constraint failed)”? IntegrityError:NOT NULL约束失败PYTHON,SQLite3 - IntegrityError: NOT NULL constraint failed PYTHON, SQLite3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM