[CDBI] A many to many relationship question (getting error)

Giannis Economou gecon at di.uoa.gr
Thu Oct 5 14:03:08 BST 2006


Hello all,

It's the first time I'm trying to setup a many-to-many relationship
between my classes, but it is not working.
Please, can you provide some help?

The tables:
CREATE TABLE users_info (
  uid int primary key,
  uname varchar
)

CREATE TABLE groups (
  gid int primary key,
  group_name varchar
)

CREATE TABLE users_groups (
  uid int,
  gid int,
  PRIMARY KEY  (uid,gid)
)


The packages:

package UsersInfo;
#...
__PACKAGE__->table('users_info');
__PACKAGE__->columns( Primary   => qw/uid/ );
__PACKAGE__->columns( Essential => qw/uname/ );
__PACKAGE__->has_many(groups => [ 'UsersGroups' => 'gid' ]);



package Groups;
#...
__PACKAGE__->table('groups');
__PACKAGE__->columns( Primary   => qw/gid/ );
__PACKAGE__->columns( Essential => qw/group_name/ );
__PACKAGE__->has_many( users => [ 'UsersGroups' => 'uid' ] );



package UsersGroups;
#....
__PACKAGE__->table('users_groups');
__PACKAGE__->columns( Primary   => qw/uid/ );
__PACKAGE__->columns( Essential => qw/gid/ );
__PACKAGE__->has_a( uid => 'UsersInfo' );
__PACKAGE__->has_a( gid => 'Groups' );


and later on on another class:

my $group = new Groups->search( group_name => 'maintainers' )->first;
my $a_user = new UsersInfo->search( uname => $uname )->first;
$a_user->add_to_groups( { gid => $group } );

and I'm getting....

ERROR:
usersinfo is not a column of RemindMe::Data::UsersGroups at
/usr/local/share/perl/5.8.7/Class/DBI/Relationship/HasMany.pm line 112



Thank you...


Regards,
Giannis



More information about the ClassDBI mailing list