postgresql Flask SQLALCHEMY设置user.is_active = True最终显示为None [关闭]

disho6za  于 2024-01-07  发布在  PostgreSQL
关注(0)|答案(1)|浏览(140)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受回答。

这个问题是由错字或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
15天前关闭
Improve this question
使用Flask和Flask-SQLAlchemy

def log():
    login_form = LoginForm()
    if request.method == 'POST' and login_form.validate_on_submit():
        user = User.query.filter_by(username=login_form.login.data, password=login_form.password.data).first()
        print(f"{user.is_active} before before")
        if user != None:
            print(f"{user} {user.username} b")
            print(f"{user.is_active} before")
            user.is_active = True #ends up as 'None'
            print(f"{user} {user.username} a")
            print(f"{user.is_active} after ")
            try:
                db.session.commit()
                login_user(user) 
            except Exception as e:
                db.session.rollback()  
                print(f"Error updating user: {e}")
            return render_template('test.html', test = login_user(user))
        return f"{login_form.login.data}   {login_form.password.data}  /{user}"
    return "nothing"

字符串
这是我的models.py

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(100), nullable = False)
    password = db.Column(db.String(60), nullable = False)
    email = db.Column(db.String(40), unique = True)
    first_name = db.Column(db.String(33))
    last_name = db.Column(db.String(36))
    is_active = db.Column(db.Boolean, default=False, nullable = False)
    date_created = db.Column(db.DateTime, default = lambda: datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"), nullable = False)
    date_last_active = db.Column(db.DateTime, default = lambda: datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"), nullable = False)


在运行log()函数之后,

假之前之前
<User 1>莫迪B
之前为假
<User 1>该死
没有后
更新用户时出错:(psycopg2.errors.NotNullViolation)关系“user”的列“is_active”中的空值违反了非空约束 * 错误IL:失败行包含(1,Morddy,,nice,null,null,null,2023-12-20 20:24:41,2023- 12-20 20:24:41)。**
[SQL:UPDATE“user”SET is_active=%(is_active)s WHERE“user”.id = %(user_id)s][parameters:“is_active”:None,“user_id”:1}](此错误的背景位于:https://sqlalche.me/e/20/gkpj

我用的是PostgreSQL 16,调试后没发现什么问题,问题出在哪里?
我调试了很多次,重新启动,尝试了字符串和所有的东西,但都没有工作,在运行user.is_active = True后,它总是以None结束。在检查pgAdmin后,行被正确选择,但没有任何变化。
编辑:psql <your-db-name> -c '\d user'的输出

Tabulka "public.user"
     Sloupec      |             Typ             | Collation | Nullable |            Implicitne
------------------+-----------------------------+-----------+----------+----------------------------------
 id               | integer                     |           | not null | nextval('user_id_seq'::regclass)
 username         | character varying(100)      |           | not null |
 password         | character varying(60)       |           | not null |
 email            | character varying(40)       |           |          |
 first_name       | character varying(33)       |           |          |
 last_name        | character varying(36)       |           |          |
 is_active        | boolean                     |           | not null |
 date_created     | timestamp without time zone |           | not null |
 date_last_active | timestamp without time zone |           | not null |
Indexy:
    "user_pkey" PRIMARY KEY, btree (id)
    "user_email_key" UNIQUE CONSTRAINT, btree (email)

thigvfpy

thigvfpy1#

问题出在is_active的事件侦听器中,它没有返回任何东西,在返回值后,它按预期工作。

相关问题