Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relationship URLs for one-to-many not returning list #165

Closed
logandk opened this issue Mar 18, 2013 · 2 comments
Closed

Relationship URLs for one-to-many not returning list #165

logandk opened this issue Mar 18, 2013 · 2 comments
Labels

Comments

@logandk
Copy link
Contributor

logandk commented Mar 18, 2013

Here's a simple API that illustrates this issue:

from flask import Flask
from flask import request
from flask.ext import restless
from flask.ext import sqlalchemy

# Initialize the app
app = Flask(__name__)
db  = sqlalchemy.SQLAlchemy(app)
api = restless.APIManager(app, flask_sqlalchemy_db=db)

# Database configuration
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'

# Define data models
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

class Todo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    task = db.Column(db.String)

    user = db.relationship('User', backref=db.backref('todos', lazy='joined'))

# Seed database
db.create_all()

u = User(name='Jimi Hendrix')
t1 = Todo(task='Play guitar', user=u)
t2 = Todo(task='Burn guitar', user=u)

db.session.add(u)
db.session.commit()

# Define API
api.create_api(User, methods=['GET', 'POST', 'DELETE', 'PATCH'])

if __name__ == '__main__':
    app.run(debug=True)

Loading http://localhost:5000/api/user/1/todos returns:

{
  "task": "Play guitar", 
  "user_id": 1, 
  "id": 1, 
  "user": {
    "id": 1, 
    "name": "Jimi Hendrix"
  }
}

While http://localhost:5000/api/user/2/todos returns:

{
  "task": "Burn guitar", 
  "user_id": 1, 
  "id": 2, 
  "user": {
    "id": 1, 
    "name": "Jimi Hendrix"
  }
}

So the provided id parameter seems to be referring to the Todo instead of User.

Loading http://localhost:5000/api/user/1/todos should return something like:

{
  "total_pages": 1, 
  "objects": [
    {
      "task": "Play guitar", 
      "user_id": 1, 
      "id": 1, 
      "user": {
        "id": 1, 
        "name": "Jimi Hendrix"
      }
    }, 
    {
      "task": "Burn guitar", 
      "user_id": 1, 
      "id": 2, 
      "user": {
        "id": 1, 
        "name": "Jimi Hendrix"
      }
    }
  ], 
  "num_results": 2, 
  "page": 1
}
@jfinkels
Copy link
Owner

You are right, this is a bug. I'll look into it now.

jfinkels added a commit that referenced this issue Mar 27, 2013
@jfinkels
Copy link
Owner

jfinkels commented Apr 3, 2013

Closing this issue, as it is fixed by c8750be.

@jfinkels jfinkels closed this as completed Apr 3, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants