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

Model no attribute 'id' after creating #390

Closed
resmo opened this issue Feb 16, 2021 · 6 comments · Fixed by #393
Closed

Model no attribute 'id' after creating #390

resmo opened this issue Feb 16, 2021 · 6 comments · Fixed by #393
Labels
bug An existing feature is not working as intended

Comments

@resmo
Copy link
Contributor

resmo commented Feb 16, 2021

Describe the bug

Storing a new model does not allow to access the id attribute. Accessing in code after save() results in a
class model 'Category' has no attribute id

category.save()
request.session.flash('success', f'Saved: {category.id}')
return request.redirect_to('admin.category.table')

To Reproduce

create a simple store function:

from app.Category import Category

class CategoryController(Controller):

    def store(self, request: Request):
        category = Category()
        category.name = request.input('name')
        category.slug = request.input('slug')
        category.description = request.input('description')
        category.sorting = request.input('sorting')
        category.save()
        return category

Expected behavior
What do you believe should be happening?

{
  "id": 1,
  "name": "test",
  "slug": "test",
  "description": "test",
  "sorting": "1"
}

Actual behavior

{
  "name": "test",
  "slug": "test",
  "description": "test",
  "sorting": "1"
}

Screenshots or code snippets
What database are you using?

  • Type: SQLite 2.8.17
  • Masonite ORM: masonite-orm==1.0.27
@resmo resmo added the bug An existing feature is not working as intended label Feb 16, 2021
@Marlysson
Copy link
Contributor

@resmo Which python version are you using to come with this sqlite version?

@josephmancuso
Copy link
Member

@Marlysson that's a really good catch. That SQLite version is from 2005 ...

@Marlysson
Copy link
Contributor

Marlysson commented Feb 16, 2021

@resmo, I'm trying reproduce that here and I've notice some litte problem.
You're just saving the entity, but the inserted result is forgotten, you need to assign the result of the .save() operation back to category variable.

 def store(self, request: Request):
    category = Category()
    # operations
    category = category.save() # Assign back to variable.
    return category

With this you will be able to get the id of model.

@resmo
Copy link
Contributor Author

resmo commented Feb 16, 2021

@Marlysson I confirm, assigning back to a var works.

However, this assignment is totally not usual for an ORM, especially not how orator works https://orator-orm.com/docs/0.9/orm.html#insert-update-and-delete

@josephmancuso
Copy link
Member

josephmancuso commented Feb 16, 2021

@Marlysson @resmo is correct, His code should be working as he is intending it to work. I think the issue is we are just missing a fill.

            if self.is_loaded():
                result = builder.update(self.__dirty_attributes__)
            else:
                result = self.create(self.__dirty_attributes__, query=query)
            self.observe_events(self, "saved")
            self.fill(result) # <--- this is missing here. Assigns the result to the current model
            return result

@josephmancuso
Copy link
Member

@resmo releasing 1.0.29 now to fix this. Thanks for the report!

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

Successfully merging a pull request may close this issue.

3 participants