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

Different type of return: The Model Query return a object and the Query Builder return a list (orator -> masonite-orm) #420

Closed
marcelovieirahonorio opened this issue Mar 17, 2021 · 3 comments
Labels
bug An existing feature is not working as intended

Comments

@marcelovieirahonorio
Copy link

Describe the bug
With orator, I could write a query with Model Query ou Query Builder (DB.table) and the return is a object. With this, I could pass this return to only a function and the process is the same, independent of who call the function.
With masonite-orm, when I write a query with Model Query, I get a collection of objects, but, when I write a query with Query Builder, I get a list. With this, I need call different process, only to change the way of get the data.

To Reproduce
Steps to reproduce the behavior:
IN ORATOR

  1. Create a table and a model to table.
  2. Create same records
  3. Make a test with query:
  4. from app.User import User
  5. users = User.get()
  6. print(users)
  7. ustest01 = users.get(0)
  8. print(ustest01.name) ## object
  9. // # now with query builder
  10. from config.database import DB
  11. users2 = DB.table('users').select('id','name').where('id', '<=', 2).get()
  12. print(users2)
  13. ustest02 = users2.get(0)
  14. print(ustest02.name) ## object

IN MASONITE-ORM

  1. from app.User import User
  2. users = User.get()
  3. print(users)
  4. ustest01 = users.get(0)
  5. print(ustest01.name) ## object
  6. // # now with query builder
  7. from masoniteorm.query import QueryBuilder
  8. users2=QueryBuilder().table('users').select('id','name').where('id', '<=', 2).get()
  9. print(users2)
  10. ustest02 = users2.get(0)
    ** Not work **

To work, I need do:
ustest02 = users2[0]
print(ustest02['name'])

With this, I need a function to data from Model Query and another to data from Query Builder, however, the process is the same.

Expected behavior
What do you believe should be happening?
I think the return could (if possible) be the same type (object), with this, I could make only a process, independent of who call the function.

Screenshots or code snippets
Screenshots help a lot. If applicable, add screenshots to help explain your problem.

image

image

image

image

image

Desktop (please complete the following information):

  • OS: Linux
  • Version: Ubuntu 20.04

What database are you using?

  • Type: Postgres
  • Version: 12
  • Masonite ORM 1.0.40

Additional context

Thread on slack:
https://masoniteproject.slack.com/archives/CAKB7NARF/p1615647050007700

@marcelovieirahonorio marcelovieirahonorio added the bug An existing feature is not working as intended label Mar 17, 2021
@josephmancuso
Copy link
Member

I'm not sure how we can accomplish this without any breaking changes. One way to do this if you realy want to use the query builder is to pass the model in the query builder like this:

builder = QueryBuilder(model=User)

This is how the model and query builder work together under the hood. The model just passes itself to the query builder

@josephmancuso
Copy link
Member

Another way to do this as well is instead of trying to always the query builder and the model as an attribute on an object, just get both as a dictionary.

This works:

user = User.find(1)
user['name']

@marcelovieirahonorio
Copy link
Author

@josephmancuso , I made some tests and adaptations on my code... I think with this way, worked with a minimum impact. Thank you!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An existing feature is not working as intended
Projects
None yet
Development

No branches or pull requests

2 participants