[Catalyst] Catalyst / DBIx Class Relationship many_to_many problem

J. Shirley jshirley at gmail.com
Sun Feb 1 22:31:09 GMT 2009


On Sun, Feb 1, 2009 at 1:10 PM, Jakub Tutaj <jamz at wp.pl> wrote:
>
> Hello!
>
> I've started using Catalyst, and now I've started writing my second app
> (after 1st one - MyApp from tutorial)
>
> I've created db tables analogic to tutorial:
>
> courses (id, name)
> users (id, name, email, etc.)
> course_users (course_id, user_id)
>
> When trying to list all users from one course,
> in my tt view list.tt2 I do:
>
> [% FOREACH course IN courses %]
>  [% tt_users = [ ];
>  tt_users.push(user.name) FOREACH user = course.users %]
> [% END -%]
>
> And then I get error:
>
> "Couldn't render template "undef error -
> DBIx::Class::Relationship::ManyToMany::__ANON__(): DBI Exception:
> DBD::Pg::st execute failed: ERROR:  syntax error at or near "."
> LINE 1: SELECT user.id, user.name, user.email, user.pass, user.role ...
>                   ^ [for Statement "SELECT user.id, user.name, user.email,
> user.pass, user.role FROM course_users me  JOIN users user ON ( user.id =
> me.user_id ) WHERE ( me.course_id = ? )" with ParamValues: 1='1'] at
> /home/jtutaj/workspace/saps/root/src/courses/list.tt2 line 29"
>
> >From message I can see there's something with many_to_many relationship, but
> found nothing when comparing to working tutorial MyApp application.
>
> My Schema relationship parts are:
>
> Courses.pm:
> __PACKAGE__->has_many(course_users => 'saps::Schema::CourseUsers',
> 'course_id');
> __PACKAGE__->many_to_many(users => 'course_users', 'user');
>
> Users.pm:
> __PACKAGE__->has_many(course_user => 'saps::Schema::CourseUsers',
> 'user_id');
> __PACKAGE__->many_to_many(courses => 'course_user', 'course');
>
> CourseUsers.pm:
> __PACKAGE__->belongs_to(course => 'saps::Schema::Courses', 'course_id');
> __PACKAGE__->belongs_to(user => 'saps::Schema::Users', 'user_id');
>
> I already tried getting just simple data like user.name / course.name /
> course_user.user_id
> and it all works fine, but when
> FOREACH user = course.users
> error always occurs.
>
> Any help?
>
> Regards
> Jakub Tutaj
>

This is actually a DBIx::Class issue and not Catalyst, but the answer
to your problem is to simply enable quoting.  Since 'user' is a
reserved word, you must quote it.

See this section in the DBIx::Class cookbook:
http://search.cpan.org/~ash/DBIx-Class-0.08010/lib/DBIx/Class/Manual/Cookbook.pod#Setting_quoting_for_the_generated_SQL.

-J



More information about the Catalyst mailing list