Lychee: Cannot add or update a child row: a foreign key constraint fails

Since i installed version 4.6.5 (i have only php 8.0 available on my NAS)and made the migration of “2022_12_10_183251_increment_user_i_ds.php” i have problems with importing photos to the DB

Go to a album, import photo via + and Upload photo, following errors occur immediately


2023-01-16 10:06:32 UTC -- error -- PDOStatement::execute -- 545 -- SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (lychee.photos, CONSTRAINT photos_owner_id_foreign FOREIGN KEY (owner_id) REFERENCES users (id`))

2023-01-16 10:06:32 UTC – error – App\Models\Photo::performInsert – 105 – SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (lychee.photos, CONSTRAINT photos_owner_id_foreign FOREIGN KEY (owner_id) REFERENCES users (id)); caused by

2023-01-16 10:06:32 UTC – error – Illuminate\Database\Connection::runQueryCallback – 760 – SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (lychee.photos, CONSTRAINT photos_owner_id_foreign FOREIGN KEY (owner_id) REFERENCES users (id)) (SQL: insert into photos (updated_at, created_at, title, description, tags, type, iso, aperture, make, model, lens, shutter, focal, taken_at, taken_at_orig_tz, latitude, longitude, altitude, img_direction, location, live_photo_content_id, is_public, is_starred, album_id, owner_id, original_checksum, checksum, id, legacy_id) values (2023-01-16 10:06:29.290315, 2023-01-16 10:06:29.290315, 0AFA2C8D-D15E-485B-8147-B91B6B7F0A5B, ?, ?, image/jpeg, 160, f/1.6, Apple, iPhone 13 mini, iPhone 13 mini back dual wide camera 5.1mm f/1.6, 1/49 s, 5 mm, 2022-06-05 19:18:10.000000, +02:00, ?, ?, ?, ?, ?, ?, 0, 0, IPgtfh4B-pccJuO6MP259Q_j, 0, b6cbbd81b9c745fde8c2a9859bd695c78cc2e43a, b6cbbd81b9c745fde8c2a9859bd695c78cc2e43a, V_iiFTohMnK7LzEoR3isRbxR, 16738635919078)); caused by

2023-01-16 10:06:32 UTC – error – App\Models\Photo::performInsert – 105 – SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (lychee.photos, CONSTRAINT photos_owner_id_foreign FOREIGN KEY (owner_id) REFERENCES users (id)) (SQL: insert into photos (updated_at, created_at, title, description, tags, type, iso, aperture, make, model, lens, shutter, focal, taken_at, taken_at_orig_tz, latitude, longitude, altitude, img_direction, location, live_photo_content_id, is_public, is_starred, album_id, owner_id, original_checksum, checksum, id, legacy_id) values (2023-01-16 10:06:29.290315, 2023-01-16 10:06:29.290315, 0AFA2C8D-D15E-485B-8147-B91B6B7F0A5B, ?, ?, image/jpeg, 160, f/1.6, Apple, iPhone 13 mini, iPhone 13 mini back dual wide camera 5.1mm f/1.6, 1/49 s, 5 mm, 2022-06-05 19:18:10.000000, +02:00, ?, ?, ?, ?, ?, ?, 0, 0, IPgtfh4B-pccJuO6MP259Q_j, 0, b6cbbd81b9c745fde8c2a9859bd695c78cc2e43a, b6cbbd81b9c745fde8c2a9859bd695c78cc2e43a, V_iiFTohMnK7LzEoR3isRbxR, 16738635919078)); caused by

2023-01-16 10:06:32 UTC – error – App\Models\Photo::performInsert – 122 – unable to persist model to DB after 5 unsuccessful attempts; caused by

2023-01-16 10:06:32 UTC – error – App\Exceptions\ModelDBException::create – 50 – Updating photo failed; caused by


The problem is, that the insert statement includes the owner_id 0, which is not available anymore. My workaround was a new user with user_id 0 that it works, but hopefully you can fix the insert statement for importing new pictures instead

Thanks in advance

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 20 (11 by maintainers)

Most upvoted comments

A chmod -R +x /volume1/web/lychee/.git does not help

It needs to be executable by the web-user not the current one, so probably chown -R www-data:www-data .git would fix it. Nevertheless quite an annoying bug. Would you mind sending me a dump of your DB so I can have a look at what is wrong in it?

Or:

git stash # put all the changes made to the side
git stash clear # remove all those (we won't need them anyway
git fetch # see what is behind
git checkout v4.6.5 # switch to version 4.6.5: Instead of git pull