DBIx: Class - cannot find source for model

I am trying to use DBIx: Class. I successfully created the Schema class using DBIx: class :: Schema :: Loader.

I can also connect to the database.

#!/usr/bin/perl -w use Test::More tests => 5; use_ok('Models::ModelRole'); use_ok('Models::User'); my $model = Models::User->new(); cmp_ok($model->{ModelName}, 'eq', 'User', 'model name'); ok($model->connect(), "connect"); #works ok($model->{schema}->resultset('User')); 

The last test returns an error message:

 DBIx::Class::Schema::source(): Can't find source for User at ./tests/ModelsTests.pl line 29 

This is the structure of the generated class from DBIx: Class :: Schema :: Loader:

enter image description here

This is the user model class:

 package Models::User; use DB::Glued::Schema::Result::User; use Models::ModelRole; use Moose; with 'Models::ModelRole'; sub BUILD { my $self = shift; $self->{schema} = Glued::Schema::Result::User->new(); my @name = split('::', __PACKAGE__); $self->{ModelName} = $name[-1]; } 1; 

I hope this is enough.

+6
source share
1 answer

Circuits / models must be connected to the source. DBIC code describes only data and its relationships. This is completely agnostically related to the source / link.

So, you must connect to DB::Glued::Schema in order to be able to use the model. I think the best way to test is to connect to the database :memory: SQLite DB. Of course, the database will be empty. There are several options / approaches to fill if you need lights. If you do, find metacpan.

There is a good package that will simplify test connections: Test :: DBIx :: Class .

0
source

Source: https://habr.com/ru/post/976610/


All Articles