Table Modify Assignment

Hello sir, part of the issue is that you have to point the correct dog owner.
Probably the error is in:

dogs.modify(iterator, account owner, [&](auto& row) {...

Instead “account owner”, it should point to “dog.owner”, why?
Simple, you have to point into the TABLE that you call dog, so if you point it to “dog.owner” it will look at all the dogs that is owned that the account owner in the dog table.

If your table looks like this one, then you should use dog.owner to correctly point to look at the owner of the dogs in that table.

//table struct dog
      TABLE dog{
      int id; //unique ID for index
      std::string dog_name;
      int age;
      name owner; //eos account name, owner of dogs
      //get primary key by ID variable
      uint64_t primary_key() const{return id;}
      //get dogs by owner index
      uint64_t by_owner() const{return owner.value;}
    };

Then you use the same action, but correct the little pointer, it should be like this:

      dogs.modify(iterator, dog.owner, [&](auto& row) {
        row.dog_name = new_dog_name;
        row.age = new_age;
      };

If i explain myself kinda confusing, let me know so i can try to explain myself better.
Hope this gives you a clear view of the subject, keep learning! :slight_smile:

Carlos Z.

1 Like

Thanks Carlos, this is well explained! :+1:

You are right, it should be dog.owner, it makes sense as dog is the TABLE. I checked Filip’s solution and he did use dog.owner.

Interesting, in the dogs.modify function, Filip used the same variables as in the table: dog_name and age instead of new variables such as new_dog_name and new_age, I think it makes more sense.

1 Like
ACTION modify(int dog_id, name dog_new_owner, name std::string dog_new_name, int dog_new_age){
          dog_index dogs(get_self(), get_self().value);
 
          auto dog = dogs.get(dog_id,“No such dog found in record to be modified.”);
          require_auth(dog.owner);

          auto iterator = dogs.find(dog_id);
            dogs.modify(iterator, dog.owner, [&](auto& dog){
            dog.dog_owner = dog_new_owner;                
            dog.dog_name = dog_new_name;
            dog.dog_age = dog_new_age;
            
          });
     }

I tried to insert also the option to change the owner name, which would only be possible for the current owner.

you forgot to add “std::string” to the variable for the new name of the dog.

name std::string dog_new_owner

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like

Thanks Carlos! :smiley:
Do you mean I should have written like this “dog.dog_name = dog_new_name;” ?

No sir, here was your action, the issue is on the parameter “name dog_new_owner”.

ACTION modify(int dog_id, name dog_new_owner, name std::string dog_new_name, int dog_new_age){
          dog_index dogs(get_self(), get_self().value);
 
          auto dog = dogs.get(dog_id,“No such dog found in record to be modified.”);
          require_auth(dog.owner);

          auto iterator = dogs.find(dog_id);
            dogs.modify(iterator, dog.owner, [&](auto& dog){
            dog.dog_owner = dog_new_owner;                
            dog.dog_name = dog_new_name;
            dog.dog_age = dog_new_age;
            
          });
     }

Now, it will be the same with just a correct parameter for the dot_new_owner. Like this:

ACTION modify(int dog_id, name std::string dog_new_owner, name std::string dog_new_name, int dog_new_age){
          dog_index dogs(get_self(), get_self().value);
 
          auto dog = dogs.get(dog_id,“No such dog found in record to be modified.”);
          require_auth(dog.owner);

          auto iterator = dogs.find(dog_id);
            dogs.modify(iterator, dog.owner, [&](auto& dog){
            dog.dog_owner = dog_new_owner;                
            dog.dog_name = dog_new_name;
            dog.dog_age = dog_new_age;
            
          });
     }

I just add “std::string” to the variable for the new name of the dog.

At the end of your action, all the variables that you want to modify should have the exact name has they are in your table.

//table struct dog
      TABLE dog{
      int id; //unique ID for index
      std::string dog_name;
      int age;
      name owner; //eos account name, owner of dogs
      //get primary key by ID variable
      uint64_t primary_key() const{return id;}
      //get dogs by owner index
      uint64_t by_owner() const{return owner.value;}
    };

so yeah, if you have the “dog_name” variable in your table with the same name, thats the correct way to modify it.

the “dog.something” is a pointer to let you know which variable your about to modify, but it must have the same name that is in the table.

Hope this gives you a clear view of the subject, keep learning! :slight_smile:
If you have any doubt, please let us know so we can help you!

Carlos Z.

1 Like

Ok it’s perfectly clear now, thank you again Carlos!

1 Like