Support the ongoing development of Laravel.io →
posted 11 years ago
Database

Hi, Having a problem with using firstOrCreate with my Eloquent model

$progress = Progress::firstOrCreate(array('employee_id'=> 1, 'section_id' => 4));

throws the following error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (testdb.progress, CONSTRAINT progress_employee_id_foreign FOREIGN KEY (employee_id) REFERENCES employees (id)) (SQL: insert into progress (updated_at, created_at) values (2014-06-23 14:23:45, 2014-06-23 14:23:45))

There is a foreign key constraint that I have set up as defined above - both fields are the same type, size and unsigned. Employee with id 1 exists in the employees table.

The "insert into" sql in the error isn't correct - it should be: insert into progress ('employee_id','section_id',updated_at, created_at) values (1,4,2014-06-23 14:23:45, 2014-06-23 14:23:45)

the sql command in the line above using phpMyAdmin, the row inserts fine (I see a foreign key constraint violation if I use a employee_id that doesn't exist in the employees table so the foreign key constraint works as it should). But the sql in the error throws the FK constraint error because the employee_id and section_id aren't passed to the query.

At the moment all the fields in the table are set to fillable, as I initially thought this was the problem. The table has other fields in it that have default values set for new rows.

Is this a bug or am I doing something wrong in the firstOrCreate method. (Create function also throws the same error).

The firstOrCreate function will return an existing row properly.

Thanks for the help in advance....

Last updated 3 years ago.
0

Ok, I managed to get round this by using the findOrNew() function, assigning the values for employee_id and section_id on the new object created then calling save().

I think this is better practice as foreign keys shouldn't be mass assigned as far as I can make out. Visible, hidden, fillable, guarded with Foreign Keys

Might be of use to someone having a similar problem with create() or findOrCreate() in the future.

Last updated 3 years ago.
0

I have the same issue as you did, but when I use the findOrNew() function I don't get any kind of error, but it doesn't create a new record in the database.

I'm trying to update a profile of a user and if the profile isn't there create it.

$profile = Profile::findOrNew(['user_id' => $update->id], $cleanInput);

I get the following error with the above code

Column not found: 1054 Unknown column 'admin@example.com' in 'field list'

I have also tried firstOrNew with no luck.

$profile = Profile::firstOrNew(['user_id' => $update->id])->update(array_except($cleanInput, [
			'email',
			'alias',
			'username',
			'permissions',
			'last_login',
			'shooter_uuid',
			'first_name',
			'last_name',
			'password',
			'include',
		]));

The above just returns the ID of the last profile

If I don't use array_except on the input data I get the following error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'field list' 

How did you assign your variables and then save the data to the new record?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jol-bdcons jol-bdcons Joined 23 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.