Discussion:
[sqlalchemy] How to enter value for foriegn key field via sqlalchemy
sudheesh ks
2015-06-22 19:44:48 UTC
Permalink
I have models:

{
class Post(db.Model):
id = db.Column(db.Integer, primary_key = True)
body = db.Column(db.String(2000))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
comments = db.relationship('Comment', backref='parent_post', lazy='dynamic')

class Comment(db.Model):
id = db.Column(db.Integer, primary_key = True)
body = db.Column(db.String(140))
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
}

When entering a post to database I do this:

{
if form.validate_on_submit():
post = Post(body=form.post.data, author=g.user)
db.session.add(post)
db.session.commit()
} This is working right.

But how can I enter a comment to database if I want to pass the 'post.id' value directly
instead of object 'post'. (Not able to 'pass' object via form in html)

{
if form.validate_on_submit():
comment = Comment(body=form.post.data, parent_post=form.p_id.data)
db.session.add(post)
db.session.commit()
}

currenntly p_id holds value post.id and it gives me error:
AttributeError: 'int' object has no attribute '_sa_instance_state'
Please help
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
Simon King
2015-06-22 19:50:23 UTC
Permalink
Post by sudheesh ks
{
id = db.Column(db.Integer, primary_key = True)
body = db.Column(db.String(2000))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
comments = db.relationship('Comment', backref='parent_post', lazy='dynamic')
id = db.Column(db.Integer, primary_key = True)
body = db.Column(db.String(140))
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
}
{
post = Post(body=form.post.data, author=g.user)
db.session.add(post)
db.session.commit()
} This is working right.
But how can I enter a comment to database if I want to pass the 'post.id' value directly
instead of object 'post'. (Not able to 'pass' object via form in html)
{
comment = Comment(body=form.post.data, parent_post=form.p_id.data)
db.session.add(post)
db.session.commit()
}
AttributeError: 'int' object has no attribute '_sa_instance_state'
Please help
The columns that you set up on your Comment class (including the
post_id foreign key column) can be used directly.

ie.

Comment(body=form.post.data, post_id=form.p_id.data)

Hope that helps,

Simon
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
sudheesh ks
2015-06-22 20:29:39 UTC
Permalink
So thanks... It worked. I made that mistake in code and had been trying to
solve that for 2 days.
Post by Simon King
Post by sudheesh ks
{
id = db.Column(db.Integer, primary_key = True)
body = db.Column(db.String(2000))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
comments = db.relationship('Comment', backref='parent_post', lazy='dynamic')
id = db.Column(db.Integer, primary_key = True)
body = db.Column(db.String(140))
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
}
{
post = Post(body=form.post.data, author=g.user)
db.session.add(post)
db.session.commit()
} This is working right.
But how can I enter a comment to database if I want to pass the 'post.id
'
Post by sudheesh ks
value directly
instead of object 'post'. (Not able to 'pass' object via form in html)
{
comment = Comment(body=form.post.data, parent_post=form.p_id.data)
db.session.add(post)
db.session.commit()
}
AttributeError: 'int' object has no attribute '_sa_instance_state'
Please help
The columns that you set up on your Comment class (including the
post_id foreign key column) can be used directly.
ie.
Comment(body=form.post.data, post_id=form.p_id.data)
Hope that helps,
Simon
--
You received this message because you are subscribed to a topic in the
Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/sqlalchemy/8QpC_kh6Yds/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
Loading...