Python Eve, SQLalchemy and ForeignKey

I work in a Python Eve-based RESTful service using SQLAlcemy. I have two models with a one to one relationship:

class User(CommonColumns): """Model of an user in the database""" __tablename__ = "user" id = Column(Integer, primary_key=True) username = Column(String, unique=True) email = Column(EmailType, unique=True) folders = relationship('Folder', backref='user') def __unicode__(self): return self.username class Folder(CommonColumns): """Model of an user in the database""" __tablename__ = "folder" id = Column(Integer, primary_key=True) name = Column(String, unique=True) user_id = Column(Integer, ForeignKey('user.id'), nullable=False) def __unicode__(self): return "{}/{}".format(self.user.username, self.name) 

CommonColumns defined as here

This works great when inserting, updating, and deleting users . However, I cannot get the correct attachment for folders :

 newfolder = { 'name':'FOLDER', 'user_id': 1, } response = requests.post("http://localhost:8080/api/v1.0/folders",data=newfolder) print response.json() {u'_error': {u'code': 422, u'message': u'Insertion failure: 1 document(s) contain(s) error(s)'}, u'_issues': {u'exception': u"'user'"}, u'_status': u'ERR'} 

This is a rather cryptic error message. I read the Python Eve documentation and I cannot figure out what I'm doing wrong. The only difference that I see between user and folder inserts is that it has foreign keys.

Any idea why this is happening?

+5
source share
1 answer

Well, it seems that the problem was that I did not have the same name for the resource and the table name ("users" for the resource, "user" for the table name). This seems to raise issues with foreign keys the day before, but does not interrupt with objects without a relationship.

Changing table names to resource names solved the problem.

I hope this will be useful to everyone.

+5
source

Source: https://habr.com/ru/post/1233694/


All Articles