Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Owner is not a required field.
But site crash when create/save a store if leave owner be empty
Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'uid' cannot be null: INSERT INTO {commerce_store_field_data} (store_id, type, langcode, uid, name, mail, default_currency, address__langcode, address__country_code, address__administrative_area, address__locality, address__dependent_locality, address__postal_code, address__sorting_code, address__address_line1, address__address_line2, address__organization, address__given_name, address__additional_name, address__family_name, default_langcode, prices_include_tax) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13, :db_insert_placeholder_14, :db_insert_placeholder_15, :db_insert_placeholder_16, :db_insert_placeholder_17, :db_insert_placeholder_18, :db_insert_placeholder_19, :db_insert_placeholder_20, :db_insert_placeholder_21); Array ( [:db_insert_placeholder_0] => 1 [:db_insert_placeholder_1] => online [:db_insert_placeholder_2] => en [:db_insert_placeholder_3] => [:db_insert_placeholder_4] => www [:db_insert_placeholder_5] => wwww@163.com [:db_insert_placeholder_6] => USD [:db_insert_placeholder_7] => [:db_insert_placeholder_8] => AF [:db_insert_placeholder_9] => [:db_insert_placeholder_10] => www [:db_insert_placeholder_11] => [:db_insert_placeholder_12] => [:db_insert_placeholder_13] => [:db_insert_placeholder_14] => www [:db_insert_placeholder_15] => www [:db_insert_placeholder_16] => [:db_insert_placeholder_17] => [:db_insert_placeholder_18] => [:db_insert_placeholder_19] => [:db_insert_placeholder_20] => 1 [:db_insert_placeholder_21] => 0 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 783 of /app/www/d86/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
Comment | File | Size | Author |
---|---|---|---|
#12 | prepared_owner_value_before_store_save_to_prevent_error-3020240-12.patch | 1.2 KB | init90 |
| |||
#7 | made_owner_field_required_to_prevent_error_during_store_save-3020240-7.patch | 626 bytes | init90 |
| |||
#4 | owner.png | 133.82 KB | lawxen |
Comments
Comment #2
lawxen CreditAttribution: lawxen at Sparkpad commentedComment #3
mglamanHow was this created? Over API or in the UI? We have a default value defined, which makes the UI work. Just curious.
But it does look like we need to mark the base field required.
Comment #4
lawxen CreditAttribution: lawxen at Sparkpad commentedBoth over api and the ui.
Example in the UI
Comment #5
lawxen CreditAttribution: lawxen at Sparkpad commentedExample in the api:
$store->set('uid', NULL);
$store->save();
Comment #6
mglamanHa! I never tried outright deleting the value. Yep. We should patch and set the flag
Comment #7
init90Set 'owner' field as required to prevent error during store save
Comment #8
mglamanComment #9
vdenis CreditAttribution: vdenis at Agiledrop - Your Trusted Drupal Teammates commentedNice patch tbh!
I've tested and patch applies cleanly. Good job.
Comment #10
init90Comment #11
bojanz CreditAttribution: bojanz at Centarro commentedThis needs more research.
None of Drupal's core entities have a required owner field.
Neither do products in Commerce, and yet deleting the author doesn't trigger a crash like it does for stores.
When you delete the value on a product, it saves "Anonymous (0)". When you do the same on a store, it saves NULL. And yet the field definitions look the same.
EDIT: I've also confirmed that doing ->set('uid', NULL) on a product, followed by a save, doesn't trigger a crash.
Comment #12
init90@bojanz thanks for the review.
For products, this problem was solved earlier. Related issue:
https://www.drupal.org/project/commerce/issues/2854193
Also, the same solution used for nodes.
In the patch, I reuse code from the issue above, but not sure if simple copy-paste proper way for it. Perhaps we should use a trait or something similar to avoid code duplication?
Comment #13
init90Comment #15
bojanz CreditAttribution: bojanz at Centarro commentedI like #12, since it matches products and others.
Let's fix the bug first and think about code reuse later. Thanks!