in reply to Re^2: Catalyst: model & context object usage
in thread Catalyst: model & context object usage

Your sub list is exactly the same as it is in the tutorial. After looking over the tutorial and thinking about the error, it looks like the id field for the author is not being passed to the object. There are a few reasons this could happen.

  • It could be because you are typing in the wrong url
  • Here's the right one: http://localhost:3000/books/url_create/TCPIP_Illustrated_Vol-2/5/4
  • It also could be because there is an error in the code I posted above for joining with the author id:
  • # Add a record to the join table for this book, mapping to # appropriate author $book->add_to_book_authors({author_id => $author_id}); # Note: Above is a shortcut for this: # $book->create_related('book_authors', {author_id => $author_id}); # Assign the Book object to the stash for display in the view $c->stash->{book} = $book;
  • It could be some errors in the DBIx::Class setup from "More Catalyst Basics".
  • If you happen to leave out a line or write an error in your schema files, your queries may not come out correctly.

    -Actualize

    Replies are listed 'Best First'.
    Re^4: Catalyst: model & context object usage
    by marscld (Beadle) on Aug 11, 2008 at 07:37 UTC
      Here is the error while accessing url http://localhost:3000/books/url_create/TCPIP_Illustrated_Vol-2/5/4.
      DBI Exception: DBD::Oracle::st execute failed: ORA-01400: cannot inser +t NULL into ("SDB2"."BOOKS"."ID") (DBD ERROR: OCIStmtExecute) [for St +atement "INSERT INTO books (rating, title) VALUES (?, ?)" with ParamV +alues: :p1='5', :p2='TCPIP_Illustrated_Vol-2']
      There is a way to bypass the error, assigning a value to id(Primary Key). Primary key is supposed to be self-increase, right ?
      sub url_create : Local { my ($self, $c, $title, $rating, $author_id) = @_; # Call create() on the book model object. Pass the table # columns/field values we want to set as hash values my $book = $c->model('DB::Books')->create({

      id => '10',

      title => $title, rating => $rating }); }

        I don't know Oracle but in theory if you set up your database so that book.id is a primary key and that it auto increments then you should be able to pass a NULL value as the id so that it automatically takes the next value.

        Are you in a different tutorial that uses Oracle? Otherwise I would recommend using sqlite and doing everything to the letter. That way you only have to worry about getting the web app running and not if the instructions for the sqlite database are compatible with your database.

        Also you might want to post your schema files.

        -Actualize
          Thank you Actualize. First of all, I use exactly the same Catalyst tutorial in CPAN, original database for tutorial is SQLite. Would you please tell me if there is an Oracle version tutorial ? As SQLite database is not able to process large scale data, so I'm trying to replace SQLite settings with Oracle settings to practise the Catalyst tutorial. Here is schema for table 'books'.
          package MyApp::Schema::Books; use strict; use warnings; use base 'DBIx::Class'; __PACKAGE__->load_components("Core"); __PACKAGE__->table("books"); __PACKAGE__->add_columns( "id", { data_type => "NUMBER", default_value => undef, is_nullable => 0, s +ize => 38 }, "title", { data_type => "VARCHAR2", default_value => undef, is_nullable => 1, size => 100, }, "rating", { data_type => "NUMBER", default_value => undef, is_nullable => 1, s +ize => 38 }, ); __PACKAGE__->set_primary_key("id"); # Created by DBIx::Class::Schema::Loader v0.04005 @ 2008-08-07 17:34:1 +2 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:t8NDS4p6epEhWCyorZl93Q # You can replace this text with custom content, and it will be preser +ved on regeneration # # Set relationships; # # has_many(): # args: # 1) Name of relationship, DBIC will create accessor with this +name # 2) Name of the model class referenced by this relationship # 3) Column name in *foreign* table __PACKAGE__->has_many(book_authors => 'MyApp::Schema::BookAuthors', 'b +ook_id'); # many_to_many(): # args: # 1) Name of relationship, DBIC will create accessor with this n +ame # 2) Name of has_many() relationship this many_to_many() is shor +tcut for # 3) Name of belongs_to() relationship in model class of has_man +y() above # You must already have the has_many() defined to use a many_to_ +many(). __PACKAGE__->many_to_many(authors => 'book_authors', 'author'); 1;