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: CI4 set ID to NULL when I try insert a row #2557

Closed
MashinaMashina opened this issue Feb 14, 2020 · 6 comments
Closed

Bug: CI4 set ID to NULL when I try insert a row #2557

MashinaMashina opened this issue Feb 14, 2020 · 6 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@MashinaMashina
Copy link
Contributor

MashinaMashina commented Feb 14, 2020

This code:

if (! empty($properties) && ! empty($primaryKey) && ! in_array($primaryKey, $properties))
{
	$properties[$primaryKey] = $data->{$primaryKey};
}

breaks all my app. When I try to insert a row, in example this row:
name: John
age: 21

CodeIgniter add field from primaryKey and fill value to NULL. And postgres got error because id can`t be null

Code for reproduction:

$user = new App\Entities\User;
$user->fill([
	'name' => 'John',
	'age'  => 21,
]);

$userModel->save($user);
// and we get error because primary key of userModel can`t be null
@MashinaMashina MashinaMashina added the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 14, 2020
@lonnieezell
Copy link
Member

Make your primary key auto-increment. The Entity expects an auto-incrementing primary key. I thought we had gone back and made that explicit in the user guide but I'm not finding it so that's something we should add. Not a bug, though. It's intended this way.

@MashinaMashina
Copy link
Contributor Author

MashinaMashina commented Feb 14, 2020

@lonnieezell it is autoincrement, but for autoincrement work, we don`t must set field

http://prntscr.com/r2gkhf http://prntscr.com/r2gm8v

I have postgres

@lonnieezell
Copy link
Member

I'm not the most familiar with working with Postgres, but I thought auto-incrementing was only for the serial types? Or do I have that wrong?

@MashinaMashina
Copy link
Contributor Author

MashinaMashina commented Feb 14, 2020

@MashinaMashina
Copy link
Contributor Author

@lonnieezell this is still a bug

@MashinaMashina
Copy link
Contributor Author

MashinaMashina commented Feb 23, 2020

@lonnieezell @MGatner open pls this issue.

Can we add this check?

&& isset($data->{$primaryKey})

to

if (! empty($properties) && ! empty($primaryKey) && ! in_array($primaryKey, $properties))
{
	$properties[$primaryKey] = $data->{$primaryKey};
}

File https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/Model.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

2 participants