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

save() and create() not working as expected #16782

Closed
asanikovich opened this issue Dec 13, 2016 · 9 comments
Closed

save() and create() not working as expected #16782

asanikovich opened this issue Dec 13, 2016 · 9 comments

Comments

@asanikovich
Copy link

asanikovich commented Dec 13, 2016

  • Laravel Version: 5.3.26
  • PHP Version: 5.6.12
  • Database Driver & Version: MySQL 5.6.26

Description:

'exists' property on model wrong indicates if the model exists.
We need to know if a model really created.
You can check example: asanikovich/laravel@c3ad907
🔢 Config db, migrate and run 'php artisan test'

Steps To Reproduce:

Example migration for model buyer_users (user - default model):

        Schema::create('buyer_users', function (Blueprint $table) {
            $table->unsignedInteger('user_id')->primary();
            $table->text('favorite_json')->nullable();
        });

Model should be with:

    protected $primaryKey = 'user_id';
    public $incrementing = false;

And if we try to create a model through save():

            $buyer = new BuyerUser;
            $result = $buyer->save();
            var_dump($buyer->exists, $result); // both values will be 'true'

or create():

            $buyer = BuyerUser::create();
            var_dump($buyer->exists, !!$buyer); // both values will be 'true'

But the real row does not exists in DB.

@sagarrabadiya
Copy link

i think if you don't have given default values to your db table columns it will throw SQLSTATE exception and if you have assigned default values to your db table columns row will be created with that default value.

@asanikovich
Copy link
Author

@sagarrabadiya
You can see thay I have:
$table->unsignedInteger('user_id')->primary();
It has not default value.

@sagarrabadiya
Copy link

sagarrabadiya commented Dec 14, 2016

in that case it should throw SQLSTATE exception, are you getting that exception thrown?

@asanikovich
Copy link
Author

@sagarrabadiya No. I don't have exception.

@asanikovich
Copy link
Author

asanikovich commented Dec 15, 2016

@sagarrabadiya
You can check example: asanikovich/laravel@c3ad907
🔢 Config db, migrate and run 'php artisan test'

@themsaid
Copy link
Member

Unable to reproduce, please provide a simple example I can use on a fresh laravel 5.3 installation.

@themsaid
Copy link
Member

ping @allok

@asanikovich
Copy link
Author

Look example in my repository https://github.com/allok/laravel

@themsaid
Copy link
Member

Submitted a PR with a fix #16864

Thank you so much :)

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

3 participants