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

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 }); }

Replies are listed 'Best First'.
Re^5: Catalyst: model & context object usage
by actualize (Monk) on Aug 11, 2008 at 16:12 UTC

    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;

        Your problem is that your tables are totally different. Your automatically generated schema data looks much different from mine. the columns are of different datatypes and two of your columns are allowed to be null. Here is what my schema file looks like:

        use strict; use warnings; use base 'DBIx::Class'; __PACKAGE__->load_components("Core"); __PACKAGE__->table("books"); __PACKAGE__->add_columns( "id", { data_type => "INTEGER", is_nullable => 0, size => undef }, "title", { data_type => "TEXT", is_nullable => 0, size => undef }, "rating", { data_type => "INTEGER", is_nullable => 0, size => undef }, ); __PACKAGE__->set_primary_key("id");

        Your problem is most probably that the SQL you used to create the tables does not work with the queries that the application is expecting from More Catalyst Basics:

        -- -- Create a very simple database to hold book and author informati +on -- CREATE TABLE books ( id INTEGER PRIMARY KEY, title TEXT , rating INTEGER ); -- 'book_authors' is a many-to-many join table between books & aut +hors CREATE TABLE book_authors ( book_id INTEGER, author_id INTEGER, PRIMARY KEY (book_id, author_id) ); CREATE TABLE authors ( id INTEGER PRIMARY KEY, first_name TEXT, last_name TEXT ); --- --- Load some sample data --- INSERT INTO books VALUES (1, 'CCSP SNRS Exam Certification Guide', + 5); INSERT INTO books VALUES (2, 'TCP/IP Illustrated, Volume 1', 5); INSERT INTO books VALUES (3, 'Internetworking with TCP/IP Vol.1', +4); INSERT INTO books VALUES (4, 'Perl Cookbook', 5); INSERT INTO books VALUES (5, 'Designing with Web Standards', 5); INSERT INTO authors VALUES (1, 'Greg', 'Bastien'); INSERT INTO authors VALUES (2, 'Sara', 'Nasseh'); INSERT INTO authors VALUES (3, 'Christian', 'Degu'); INSERT INTO authors VALUES (4, 'Richard', 'Stevens'); INSERT INTO authors VALUES (5, 'Douglas', 'Comer'); INSERT INTO authors VALUES (6, 'Tom', 'Christiansen'); INSERT INTO authors VALUES (7, 'Nathan', 'Torkington'); INSERT INTO authors VALUES (8, 'Jeffrey', 'Zeldman'); INSERT INTO book_authors VALUES (1, 1); INSERT INTO book_authors VALUES (1, 2); INSERT INTO book_authors VALUES (1, 3); INSERT INTO book_authors VALUES (2, 4); INSERT INTO book_authors VALUES (3, 5); INSERT INTO book_authors VALUES (4, 6); INSERT INTO book_authors VALUES (4, 7); INSERT INTO book_authors VALUES (5, 8);

        Your schema file also looks different. You auto generated it right?

        -Actualize