Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: foreign keys use wrong table name, part 2 #112

Closed
mario-minati opened this issue Jun 13, 2020 · 3 comments
Closed

bug: foreign keys use wrong table name, part 2 #112

mario-minati opened this issue Jun 13, 2020 · 3 comments

Comments

@mario-minati
Copy link

There is an other line in Yancy::Backend::Dbic which needs fixing, acording to #84.

https://github.com/preaction/Yancy/blob/master/lib/Yancy/Backend/Dbic.pm#L350 needs to be fixed to my $self_table = $source->source_name;.

@preaction
Copy link
Owner

Okay, thanks, that seems simple enough. Not being as experienced in DBIC, how could I trigger that issue? Do I create a source with a name that's entirely different from the table name? I'd like to have a regression test for this.

@mario-minati
Copy link
Author

Our result classes have nothing special:

package Glue::DB::Schema::Core::Result::Project;

# ABSTRACT: Result class for project

use strict;
use warnings;
use utf8;
use base qw(DBIx::Class);

our $VERSION = 27;

__PACKAGE__->load_components( qw/PK::Auto Core/ );
__PACKAGE__->table( 'project' );
__PACKAGE__->add_columns(
    id => {
        data_type          => 'INT',
        is_auto_increment  => 1,
        is_numeric         => 1,
        retrieve_on_insert => 1,
    },
    name => {
        data_type          => 'VARCHAR',
        size               => 255,
    },
    abbreviation => {
        data_type          => 'VARCHAR',
        size               => 15,
    },
    description => {
        data_type          => 'MEDIUMTEXT',
        is_nullable        => 1,
    },
    projectnumber => {
        data_type          => 'VARCHAR',
        size               => 15,
    },
);
__PACKAGE__->set_primary_key( qw/ id / );

__PACKAGE__->has_many(project_costcenter => 'Glue::DB::Schema::Core::Result::ProjectCostcenter',
             { 'foreign.project_id' => 'self.id' });

1;
package Glue::DB::Schema::Core::Result::ProjectCostcenter;

# ABSTRACT: Result class for project_costcenter

use strict;
use warnings;
use utf8;
use base qw(DBIx::Class);

our $VERSION = 27;

__PACKAGE__->load_components( qw/PK::Auto Core ColumnDefault/ );
__PACKAGE__->table( 'project_costcenter' );
__PACKAGE__->add_columns(
    id => {
        data_type          => 'INT',
        is_auto_increment  => 1,
        is_numeric         => 1,
        retrieve_on_insert => 1,
    },
    name => {
        data_type          => 'VARCHAR',
        size               => 255,
    },
    description => {
        data_type          => 'MEDIUMTEXT',
        is_nullable        => 1,
    },
    is_blocked => {
        data_type          => 'TINYINT',
        size               => 1,
        default_value      => '0',
    },
    project_id => {
        data_type          => 'INT',
        is_numeric         => 1,
        is_foreign_key     => 1,
    },

);
__PACKAGE__->set_primary_key( qw/ id / );


__PACKAGE__->has_many(project_costcenter_abbreviation => 'Glue::DB::Schema::Core::Result::ProjectCostcenterAbbreviation',
             { 'foreign.project_costcenter_id' => 'self.id' });

__PACKAGE__->has_many(timerecording => 'Glue::DB::Schema::Core::Result::Timerecording',
             { 'foreign.project_costcenter_id' => 'self.id' });


__PACKAGE__->belongs_to(project => 'Glue::DB::Schema::Core::Result::Project',
             { 'foreign.id' => 'self.project_id' });

1;

Our table names are all snake case, we are developing with Linux Mint. So I have no clue why DBIC return CamelCase.

@preaction
Copy link
Owner

It returns camel case because it's the class name (the last part after ::Result::), not the table name. That's what Yancy uses to name the schemas because that's all it can use without breaking the encapsulation of the ORM. Now that I understand, I can fix this properly and write a test.

preaction added a commit that referenced this issue Jun 17, 2020
DBIx::Class result classes do not need the same name as the table,
though that's what we use in Yancy's tests to make the DBIx::Class
schema look exactly the same as all the other schemas. When the result
class has a different name, we want to use that name to avoid breaking
the encapsulation of the ORM (the underlying table is not useful
information, since it may change).

Thanks @mario-minati for help debugging this! Fixes #112
preaction added a commit that referenced this issue Jun 17, 2020
    [Fixed]

    - Fixed missing $match parameter in OpenAPI spec for list actions.
      Thanks @mario-minati for reporting this! [Github #114]
    - Added a few more i18n strings to the lexicon.
    - Fixed DBIx::Class backend not finding schemas and foreign keys
      when the result name did not match the table name. Thanks
      @mario-minati for help debugging this! [Github #112]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants