Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

retrieving primary key from a DBIx::Class package

by tomgracey (Beadle)
on Apr 24, 2013 at 10:23 UTC ( #1030339=perlquestion: print w/replies, xml ) Need Help??
tomgracey has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I am wondering if there is a way (method?) to retrieve a table's primary key via a DBIx::Class table object. For example, ideally I would like to be able to do something like

my $pk = $schema->resultset('MyPackage')->primary_key;

or perhaps

my $pk =Schema::Result::MyPackage->primary_key;

... or something else?

I'm trying to create a method that outputs a certain data format when any general table name is input. (And, no, I can't really say I understand DBIx::Class terribly well!)

Your help very much appreciated!

Replies are listed 'Best First'.
Re: retrieving primary key from a DBIx::Class package
by pokki (Monk) on Apr 24, 2013 at 18:30 UTC

    You're almost there.

    What you need to do is 1. get your ResultSet's data source, i.e. what it gets the actual data from; in DBIC, this is a ResultSource object, and it's what you're describing when you're writing e.g.

    package MyApp::Schema::Result::Artist; use base qw/DBIx::Class::Core/; # this ResultSource is going to be based on a table, not a view __PACKAGE__->table('artist'); # the table has these columns __PACKAGE__->add_columns(qw/ artistid name /); # and this is the pkey __PACKAGE__->set_primary_key('artistid');

    and then 2. get the pkey info from the ResultSource. So we get:

    my $result_source = $schema->resultset('Artist')->result_source; my @list_of_columns_in_pkey = $result_source->primary_columns;

    Edit: I just realized, I assumed you were starting from a ResultSet object, but if you already know its name beforehand:

    my $result_source = $schema->source('Artist'); my @list_of_columns_in_pkey = $result_source->primary_columns;
      Great! Works perfectly - thanks pokki !

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1030339]
Front-paged by Arunbear
help
Chatterbox?
[Lady_Aleena]: See you, Discipulus.
[shmem]: Lady_Aleena: that's up to you. I only wanted to show you a path that might be more comforting than command line grep ;-)
[Lady_Aleena]: shmem, I don't think any of my modules could be converted to OO. They are too procedural.
[Lady_Aleena]: Now I will see if my perl-fu is as bad as I think it is.
[shmem]: ah, geany uses its own idea of tags. Ah well...
[Lady_Aleena]: My perl-fu is bad.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2017-04-27 12:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (506 votes). Check out past polls.