Python AttributeError: 'int' 对象没有属性 '_sa_instance_state'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16151729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:53:26  来源:igfitidea点击:

AttributeError: 'int' object has no attribute '_sa_instance_state'

pythonsqlalchemyflaskone-to-manyflask-sqlalchemy

提问by Ganye

I'm working on forum template using Flask. When I attempt creating a new thread in the browser using forms, SQLAlchemy throws an AttributeError. The problem showed up when I tried implementing a one-to-many relationship with Forum-to-Thread and a one-to-many relationship with Thread-to-User.

我正在使用 Flask 开发论坛模板。当我尝试使用表单在浏览器中创建一个新线程时,SQLAlchemy 会抛出一个 AttributeError。当我尝试实现与论坛到线程的一对多关系以及与线程到用户的一对多关系时,问题就出现了。

models.py

模型.py

class User(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  username = db.Column(db.String(32), index=True, unique=True)
  password = db.Column(db.String(32), index=True)
  email = db.Column(db.String(120), index=True, unique=True)
  role = db.Column(db.SmallInteger, default=ROLE_USER)

  posts = db.relationship('Post', backref='author', lazy='dynamic')

class Forum(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  title = db.Column(db.String(128))
  description = db.Column(db.Text)

  threads = db.relationship('Thread', backref='forum', lazy='dynamic')

class Thread(db.Model):

  id = db.Column(db.Integer, primary_key=True)
  title = db.Column(db.String(128))
  author= db.Column(db.String(32))
  timestamp = db.Column(db.DateTime)
  forum_id = db.Column(db.Integer, db.ForeignKey('forum.id'))

  posts = db.relationship('Post', backref='thread', lazy='dynamic')

class Post(db.Model):

  id = db.Column(db.Integer, primary_key=True)
  body = db.Column(db.Text)
  timestamp = db.Column(db.DateTime)
  thread_id = db.Column(db.Integer, db.ForeignKey('thread.id'))
  user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

All the new posts/threads and handled within views.py

所有新帖子/线程并在 views.py 中处理

views.py

视图.py

@app.route('/forum/id=<id>/submit', methods=['GET','POST'])
@login_required
def new_thread(id):
  form = ThreadForm()
  forum = Forum.query.filter_by(id=id).first()
  if form.validate_on_submit():
    thread = Thread(title=form.title.data,
                    author=g.user.username,
                    timestamp=datetime.utcnow())
    db.session.add(thread)
    db.session.flush()
    post = Post(body=form.body.data,
                timestamp=datetime.utcnow(),
                thread=thread.id,
                author=g.user.id)
    db.session.add(post)
    db.session.commit()
    flash('Post successful.')
    return redirect(url_for('forum_index', id=id))
  return render_template('forum/thread_submit.html', title=forum.title, form=form) 

采纳答案by zzzeek

the problem is this:

问题是这样的:

post = Post(body=form.body.data,
            timestamp=datetime.utcnow(),
            thread=thread.id,
            author=g.user.id)

you want to work with ORM objects, not primary key columns:

你想使用 ORM 对象,而不是主键列:

post = Post(body=form.body.data,
            timestamp=datetime.utcnow(),
            thread=thread,
            author=g.user)

the error means that an integer is being interpreted as an ORM object.

该错误意味着一个整数被解释为一个 ORM 对象。