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

Creating a model instance with a relation but generating extra data without relation #60

Closed
onlyu-bot opened this issue Jul 16, 2020 · 7 comments

Comments

@onlyu-bot
Copy link

onlyu-bot commented Jul 16, 2020

Hi,
that's say I create n model instances and each with a relation, but there output 2n rows of data in the table, and n rows in the relation table.

If I run ProductFactory::new()->withFactory(InfoFactory::new(),'info')->create(); from tinker.
The products table:

id    name    price
1    wine    100
2    bread    50

The infos table:

id    product_id    description
1    1    lkdakdlkdl;ks

If I run ProductFactory::new()->withFactory(InfoFactory::new(),'info',1)->times(10)->create(); from tinker.
The products table:

id    name    price
1    wine    100
2    bread    50
3    meat    15
...(skip 4 to 18)...
19 fish 20
20 pizza 80

The infos table:

id    product_id    description
1    1    yummy
2    3    good
3    5    soft
4    7    bad
...(skip 5 to 7)
8    15    healthy
9    17    funny
10    19    delicious

These are two models:

class Product extends Model
{
    public function info()
    {
        return $this->hasMany(Info::class);
    }
}
class Info extends Model
{
    public function product(){
        return $this->belongsTo(Product::class);
    }
}

The return of getDefaults method of ProductFactory:

return [
    'name' => $faker->name,
    'price' => $faker->numberBetween(1, 100),
];

The return of getDefaults method of InfoFactory:

return [
    'product_id' => ProductFactory::new(),
    'description' => $faker->text(15),
];

I don't know why, any idea?

@christophrumpel
Copy link
Owner

Hey, I will give it a look today 👍

@christophrumpel
Copy link
Owner

Just tested similar code on my machine and only models created like expected, so no duplicate ones.

I also don't an error in your examples. Is there like a whole Laravel app that you can share for me to test?

@onlyu-bot
Copy link
Author

Hi, @christophrumpel thank you so much for help.

I installed a new laravel and it goes the same, and I start to wonder does it is caused by the conflict between dependencies?

My project:
https://github.com/onlyu-bot/test-factory

There are routes for you to check the result.(describe on readme)

Thanks again!

@christophrumpel
Copy link
Owner

Thanks for getting back to me. I'm currently not at work this week but I will come back when I'm back at work.

@christophrumpel
Copy link
Owner

So back again here. Just gave it a quick look. I think it's because of this https://github.com/onlyu-bot/test-factory/blob/master/tests/Factories/InfoFactory.php#L26

You create a new product for every Info model inside the InfoFactory.

@onlyu-bot
Copy link
Author

@christophrumpel thank you so much! Yes, it is the root cause of generating extra data. I thought that I need to manually create the parent_id in the children factory, now I know that I don't need to do that! Thank you very much!

@christophrumpel
Copy link
Owner

thanks and glad it is working now. Maybe I need to make this more obvious through the docs.

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