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

add onupdate option in field simply #532

Closed
wants to merge 17 commits into from
Closed

add onupdate option in field simply #532

wants to merge 17 commits into from

Conversation

ponytailer
Copy link
Contributor

@ponytailer ponytailer commented Jan 17, 2022

  • support option onupdate in field simply
  • fix lint
  • check the args in bulk-create

@codecov-commenter
Copy link

codecov-commenter commented Jan 17, 2022

Codecov Report

Merging #532 (5ee2196) into master (4ed267e) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #532   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          185       186    +1     
  Lines        15298     15368   +70     
=========================================
+ Hits         15298     15368   +70     
Impacted Files Coverage Δ
ormar/models/newbasemodel.py 100.00% <ø> (ø)
...test_model_methods/test_populate_default_values.py 100.00% <ø> (ø)
ormar/fields/base.py 100.00% <100.00%> (ø)
ormar/models/metaclass.py 100.00% <100.00%> (ø)
ormar/models/mixins/save_mixin.py 100.00% <100.00%> (ø)
ormar/models/model.py 100.00% <100.00%> (ø)
ormar/queryset/queryset.py 100.00% <100.00%> (ø)
...est_model_methods/test_populate_onupdate_values.py 100.00% <100.00%> (ø)
tests/test_queries/test_queryset_level_methods.py 100.00% <100.00%> (ø)

ormar/fields/base.py Outdated Show resolved Hide resolved
ormar/fields/base.py Outdated Show resolved Hide resolved
ormar/fields/base.py Outdated Show resolved Hide resolved
ormar/fields/base.py Outdated Show resolved Hide resolved
ormar/models/mixins/save_mixin.py Outdated Show resolved Hide resolved
ormar/models/mixins/save_mixin.py Outdated Show resolved Hide resolved
@ponytailer
Copy link
Contributor Author

ponytailer commented Jan 18, 2022

Cascade should be another option in field ? @collerek

The onupdate don't handle the relations.

such like the sqlalchemy, and the cascade should define in the database.

book = ormar.ForiegnKey(Book, cascade=CASCADE.DELETE)

@collerek
Copy link
Owner

Cascade should be another option in field ? @collerek

The onupdate don't handle the relations.

such like the sqlalchemy, and the cascade should define in the database.

book = ormar.ForiegnKey(Book, cascade=CASCADE.DELETE)

Yes, it does.
There are already onupdate and ondelete, but only for foreign keys.

def ForeignKey( # type: ignore # noqa CFQ002
to: "ToType",
*,
name: str = None,
unique: bool = False,
nullable: bool = True,
related_name: str = None,
virtual: bool = False,
onupdate: str = None,
ondelete: str = None,
**kwargs: Any,
) -> "T":

It's used to populate onupdate and ondelete foreign key options in sqlalchemy foreign key:

ormar/ormar/fields/base.py

Lines 242 to 252 in 8dc05d5

constraints = [
sqlalchemy.ForeignKey(
con.reference,
ondelete=con.ondelete,
onupdate=con.onupdate,
name=f"fk_{self.owner.Meta.tablename}_{self.to.Meta.tablename}"
f"_{self.to.get_column_alias(self.to.Meta.pkname)}_{self.name}",
)
for con in self.constraints
]
return constraints

Where you pass a string according to sqlalchemy core specification:
https://docs.sqlalchemy.org/en/14/core/constraints.html#on-update-and-on-delete

@ponytailer
Copy link
Contributor Author

ponytailer commented Jan 18, 2022

That's in foreignKey only.

when the model was updated, something to do auto, such as the updated_time, that's my purpose.

otherwise, I have to define the code in model_cls


async def update(self,  **kwargs):
     kwargs["updated_time"] = get_ts()
     super().update(**kwargs)

ormar/models/mixins/save_mixin.py Outdated Show resolved Hide resolved
ormar/models/mixins/save_mixin.py Outdated Show resolved Hide resolved
ormar/fields/base.py Outdated Show resolved Hide resolved
@ponytailer
Copy link
Contributor Author

Cascade should be another option in field ? @collerek
The onupdate don't handle the relations.
such like the sqlalchemy, and the cascade should define in the database.

book = ormar.ForiegnKey(Book, cascade=CASCADE.DELETE)

Yes, it does. There are already onupdate and ondelete, but only for foreign keys.

def ForeignKey( # type: ignore # noqa CFQ002
to: "ToType",
*,
name: str = None,
unique: bool = False,
nullable: bool = True,
related_name: str = None,
virtual: bool = False,
onupdate: str = None,
ondelete: str = None,
**kwargs: Any,
) -> "T":

It's used to populate onupdate and ondelete foreign key options in sqlalchemy foreign key:

ormar/ormar/fields/base.py

Lines 242 to 252 in 8dc05d5

constraints = [
sqlalchemy.ForeignKey(
con.reference,
ondelete=con.ondelete,
onupdate=con.onupdate,
name=f"fk_{self.owner.Meta.tablename}_{self.to.Meta.tablename}"
f"_{self.to.get_column_alias(self.to.Meta.pkname)}_{self.name}",
)
for con in self.constraints
]
return constraints

Where you pass a string according to sqlalchemy core specification: https://docs.sqlalchemy.org/en/14/core/constraints.html#on-update-and-on-delete

thanks for your suggestions.

@ponytailer
Copy link
Contributor Author

ponytailer commented Jan 19, 2022

This pr only include the update, not the bulk-update.
because

        for obj in objects:
            # when the obj.__setattr__, should be dirty for column
            # only load the kv from dirty fields, not all.
            new_kwargs = obj.dict()

Please review the #538 first, the orjson.dumps in bulk-create need fix.

@collerek
Copy link
Owner

collerek commented Feb 4, 2022

Can you restore/uncomment bulk_update changes?
For now let's keep the changes logic as you proposed, without keeping status of each field (dirty or not)

@ponytailer
Copy link
Contributor Author

Can you restore/uncomment bulk_update changes?

For now let's keep the changes logic as you proposed, without keeping status of each field (dirty or not)

sorry. I'm on vacation now. Maybe I can create the new pr for bulk-update in a few days.

@collerek
Copy link
Owner

collerek commented Feb 4, 2022

Can you restore/uncomment bulk_update changes?
For now let's keep the changes logic as you proposed, without keeping status of each field (dirty or not)

sorry. I'm on vacation now. Maybe I can create the new pr for bulk-update in a few days.

Ahh of course. Happy Lunar New Year! 🥳 🎉

No worries it can wait for your come back :)

@ponytailer
Copy link
Contributor Author

ponytailer commented Feb 8, 2022

Can you restore/uncomment bulk_update changes?
For now let's keep the changes logic as you proposed, without keeping status of each field (dirty or not)

sorry. I'm on vacation now. Maybe I can create the new pr for bulk-update in a few days.

Ahh of course. Happy Lunar New Year! 🥳 🎉

No worries it can wait for your come back :)

Sorry, I forgot something. In bulk-update, I don't know what field was updated, so, on_update don't work.
This pr only add on_update work in update method.

Next pr will add the dirty-columns, it can support bulk-update.

If this pr has not any other problems, maybe merge it now.

@collerek
Copy link
Owner

collerek commented Feb 8, 2022

Before you implement something describe what you want to do.
I told you that keeping the dirty status on fields might have a big performance influence and can be unreliable.

@ponytailer
Copy link
Contributor Author

Before you implement something describe what you want to do. I told you that keeping the dirty status on fields might have a big performance influence and can be unreliable.

Thanks for your advice !

@@ -1111,7 +1114,15 @@ async def bulk_update( # noqa: CCR001

columns = [self.model.get_column_alias(k) for k in columns]

# on_update_fields = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please either update the code or remove it -> we don't want commented out code in the code base

@vvanglro
Copy link

vvanglro commented Apr 7, 2023

Hi guys why is this PR not merged, I wish there was an onupdate like sqlalchemy.
column-insert-update-defaults

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants