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

Field 'foo' doesn't have a default value #17

Closed
dreamingmind opened this issue Jun 4, 2020 · 7 comments
Closed

Field 'foo' doesn't have a default value #17

dreamingmind opened this issue Jun 4, 2020 · 7 comments

Comments

@dreamingmind
Copy link
Contributor

dreamingmind commented Jun 4, 2020

After updating I started getting 'Field 'foo' doesn't have a default value' errors on previously passing tests.

Wasn't the system glossing over errors like this when make()ing records?

After updating to 0.2.14 (that's when I noticed it), tests that were previously running started throwing this error.

My code was in a bit of chaos at that point so I'm not 100% sure about this.

I'm on 0.2.15 now

@dreamingmind
Copy link
Contributor Author

As I look deeper I see both this section on validation behaior which says:

all validations are deactivated when creating CakePHP entities
and persisting them to the database.

and the comment on setDefaultTemplate():

* Defines the default values of you factory. Useful for
* not nullable fields. You may use methods of the factory here too.

So my tests may have been previously passing due to some interaction related to the old cake factories because I had both listeners turned on at that point.

@pabloelcolombiano
Copy link
Collaborator

pabloelcolombiano commented Jun 4, 2020

Let us know, if you are experiencing further issues there.

@dreamingmind
Copy link
Contributor Author

This is an ongoing issue. Where previously I did not have to create any code in setDefaultTemplate() now I must define defaults for columns that don't otherwise have them.

@pabloelcolombiano
Copy link
Collaborator

setDefaultTemplate() is there to assign default values of your factory, as you write.

What is the issue you are actually having? Is there anyting we can improve or solve?

@dreamingmind
Copy link
Contributor Author

This is my database schema. It has columns that do not have defaults but are not allowed to be NULL.

Screen Shot 2020-06-04 at 11 41 27 AM

This is my test class:

<?php

namespace App\Test\TestCase\Model\Table;

use App\Test\Factory\PersonFactory;

class PeopleTableTest extends \Cake\TestSuite\TestCase
{

    public function testMake()
    {
        $person = PersonFactory::make(1)->persist();
    }
}

Running the test results in this error.

Screen Shot 2020-06-04 at 11 47 55 AM

My expectation based on the documentation and my previous experience is that the entity could be created without error in this circumstance.

My factory is baked from the most recent version:

Screen Shot 2020-06-04 at 11 50 55 AM

@pabloelcolombiano
Copy link
Collaborator

pabloelcolombiano commented Jun 4, 2020

I do not know how your code was before, but this should normally not have worked before either.

In your case, you should have in your factory:

 protected function setDefaultTemplate(): void
    {
        $this->setDefaultData(function(Generator $faker) {
            return [
                'first_name' => $faker->firstName,
                'last_name' => $faker->lastName,
                'role' => $faker->text(50)
            ];
        });
    }

Your test could then look like this:

class PeopleTableTest extends \Cake\TestSuite\TestCase
{

    public function testMake()
    {
        $person = PersonFactory::make()->persist();
    }
}

or like that, if you want to control the value of the field role:

class PeopleTableTest extends \Cake\TestSuite\TestCase
{

    public function testMake()
    {
        $person = PersonFactory::make(['role' => 'Some role'])->persist();
    }
}

Does this make sense?

@dreamingmind
Copy link
Contributor Author

Perfect sense, thanks.

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