Take a look into talbe eav_attribute
, you will find out the (image,small_image,thumbnail) attribute. In magento 2.1.8, it take the id (87,88,89)
Use this command below:
UPDATE catalog_product_entity_media_gallery AS mg,
catalog_product_entity_media_gallery_value AS mgv,
catalog_product_entity_varchar AS ev
SET ev.value = mg.value
WHERE mg.value_id = mgv.value_id
AND mgv.entity_id = ev.entity_id
AND ev.attribute_id IN (87, 88, 89)
AND mgv.position = 1;
EDIT: If the query doesn't work, It means you have not had the small and thumbnail before, use this code:
use Magento\Catalog\Model\ProductFactory;
protected $_productFactory;
public function __construct(ProductFactory $productFactory){
$this->_productFactory = $productFactory;
}
//Change the image
$productCollection = $this->_productFactory->create()->getCollection();
foreach($productCollection as $product) {
$imagePath = $product->getMediaGalleryImages()->getFirstItem()->getFile();
if ($imagePath) {
$this->_objectManager->get('Magento\Catalog\Model\Product\Action')->updateAttributes([0 => $product->getId()], array("thumbnail" => $imagePath,"small_image" => $imagePath), 0);
}
}