Search code examples
rose-db-object

Why does $my_item->save fail with Rose::DB::Object?


I am trying to do a simple addition of data to a database table (PostgreSQL). At first, I couldn't even get a simple

$my_item = $_item_class->new(...);

to work. I discovered I had spelled a field differently in my code from what I had in my "model" code.

But, now, this is working, but when I try:

$my_item->save;

it seems an exception is thrown. All this is occurring in an eval {...} structure and I would like to catch the exception and see what is going wrong, but I don't know how to do that.

Why would something like the "save" be failing here? I have checked everything, and all seems right (of course!).

And, how do I catch the exception that seems to be being thrown?

Thank you!


Solution

  • I figured all this out myself. It was simple. I had duplicated a field in my class somehow when I had done an edit to it. That was all. The class just had two identically named fields specified in the hash table in the class, both with identical characteristics. When I removed one of these, the code worked.

    With regard to my second question about how to catch the exception, I had to learn how to have an

    if ($@) {
        .
        .
        .
    }
    

    right after my "eval {...}" structure. Because I am new to Perl, I didn't understand that. But, it was actually pretty easy to figure out. My problem was that I was working from some code as a model for me that didn't do that but named specific exceptions that were thrown in its "eval {...}" code. So, I thought that I had to have the names of exceptions that could be thrown by Rose::DB::Object calls, but I couldn't find any such exceptions in the documentation. When I learned about "if ($@) {...}", I was able to print out the reported exception in $@ and from that I was able to see the problem with the duplicate field I mentioned above.

    That was all there was to it. Everything is working just fine now.