Answers.Body: <p>It looks like Craft is trying to delete a database field that has already been removed. I have no idea how you ended up in this situation but here's what I'd try to resolve the issue:</p>
  <ul>
  <li>first <strong>backup your database</strong>, in case something goes wrong!</li>
  <li>then execute the following command on your server, in the project directory. After each command, try removing you field again from the Control Panel, if it still fails, try the next one:
  <ul>
  <li><code>./craft migrate/all</code></li>
  <li><code>./craft project-config/sync</code></li>
  <li><code>./craft project-config/sync --force</code></li>
  </ul>
  </li>
  </ul>
  <p>If any of these commands returns an error, post the output.</p>
  <p>If no errors are returned by these commands but you still can't remove your field, just add the missing database column in your database as <code>VARCHAR</code> try again, this should do the trick.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-07T13:29:46.803Z
Answers.Text: thanks for your response. I get an error saying it can't find the database when running the first command. See update with output above.
Answers.UserDisplayName: Dan Lee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-06T12:59:18.447Z
Answers.LastActivityDate: 2020-10-06T12:59:18.447Z
Answers.UserDisplayName: nstCactus
Answers.Body: <p>Pardon me, I wrote this based on my memory.</p>
  <p>The table name is read from Yaml file.
  But the database doesn't have the table.
  -&gt; This means you did remove the table once, but due to project.yaml unchanged/reverted, the duplicated field returns.</p>
  <p>The root cause might be adding 2 fields with different UIDs but the same handle into project.yaml</p>
  <p>I have 2 solutions here:</p>
  <p>[Solution 1]</p>
  <ol>
  <li>(optional, I'm not sure whether Craft can handle this step or must do manually) Remove the duplicated field by searching the commented field name in project.yaml.</li>
  <li>Create an empty table with the same name. Import it then remove the
  field from Fields screen. (this helped me solve the issue)</li>
  </ol>
  <p>[Solution 2]</p>
  <ol>
  <li>Enable devMode, read stack trace.</li>
  <li>Open the .php file (ex: services/Field.php at line 000)</li>
  <li>Crack the code by ignoring error when accessing columns</li>
  </ol>
  <p>!!!Warning!!!
  You might lose the original field and its data when trying to delete duplicated version.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-24T07:30:08.790Z
Answers.LastActivityDate: 2022-06-24T07:30:08.790Z
Answers.UserDisplayName: Cskt Coop
Body: <p>I have somehow managed to duplicate some fields in my Craft install (see image of unused fields). When I delete the unused duplicates I create a server error and can't access Craft.</p>
  <p>To rectify I had to switch on and off my DigitalOcean droplet to get the server working again and reinstall a database backup. I want to remove the duplicates safely. How do I do this without bringing down the whole website?</p>
  <p>It would also be good to know any theories to why this happened in the first place so I can rectify.</p>
  <p><strong>Update 1</strong></p>
  <p>I deleted a field on my local version and have outputted the databased error below.</p>
  <p>Thanks in advance.</p>
  <p><strong>Database Error when deleting field on local version:</strong></p>
  <pre><code>Database Exception – yii\db\Exception
  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'content.field_projectDescription' in 'field list'
  The SQL being executed was: SELECT `users`.`password`, `elements`.`id`, `elements`.`fieldLayoutId`, `elements`.`uid`, `elements`.`enabled`, `elements`.`archived`, `elements`.`dateCreated`, `elements`.`dateUpdated`, `elements_sites`.`id` AS `siteSettingsId`, `elements_sites`.`slug`, `elements_sites`.`siteId`, `elements_sites`.`uri`, `elements_sites`.`enabled` AS `enabledForSite`, `users`.`username`, `users`.`firstName`, `users`.`lastName`, `users`.`email`, `users`.`unverifiedEmail`, `users`.`admin`, `users`.`locked`, `users`.`pending`, `users`.`suspended`, `users`.`lastLoginDate`, `users`.`lockoutDate`, `users`.`photoId`, `users`.`hasDashboard`, `content`.`id` AS `contentId`, `content`.`field_jobNotes`, `content`.`field_announcementBanner`, `content`.`field_announcementColor`, `content`.`field_announcementLink`, `content`.`field_announcementText`, `content`.`field_personBiography`, `content`.`field_careersForm`, `content`.`field_caseStudyClientName`, `content`.`field_caseStudyClientQuote`, `content`.`field_caseStudyClientAttribution`, `content`.`field_caseStudyUrl`, `content`.`field_eventEndDate`, `content`.`field_eventCost`, `content`.`field_eventHosted`, `content`.`field_eventLocation`, `content`.`field_eventUrl`, `content`.`field_expertOpinion`, `content`.`field_featured`, `content`.`field_fundDescription`, `content`.`field_fundUrl`, `content`.`field_fundingOpportunityAim`, `content`.`field_fundingOpportunityCurrency`, `content`.`field_fundingOpportunityDescription`, `content`.`field_fundingOpportunityFunder`, `content`.`field_fundingOpportunityFundingType`, `content`.`field_fundingOpportunityLimit`, `content`.`field_fundingOpportunityProjectStartDate`, `content`.`field_fundingOpportunityResultDate`, `content`.`field_fundingOpportunityScope`, `content`.`field_fundingOpportunityShowStartDate`, `content`.`field_fundingOpportunitySubmissionDate`, `content`.`field_fundingOpportunityURL`, `content`.`field_fundingOpportunityValue`, `content`.`field_headOffice`, `content`.`field_globalHeaderMeta`, `content`.`field_heading`, `content`.`field_hidden`, `content`.`field_imageVariants16by9`, `content`.`field_imageVariants16by9Parallax`, `content`.`field_imageVariantsThumb`, `content`.`field_innovationIntegratorAssetRef`, `content`.`field_intro`, `content`.`field_investorInsight`, `content`.`field_jobApplicationId`, `content`.`field_jobApplicationType`, `content`.`field_jobEligibilityNotice`, `content`.`field_jobReferenceNo`, `content`.`field_jobSalary`, `content`.`field_personJobTitle`, `content`.`field_latitude`, `content`.`field_linkedInProfile`, `content`.`field_locationAddress`, `content`.`field_locationPhone`, `content`.`field_logoVariants`, `content`.`field_longitude`, `content`.`field_mapLocation`, `content`.`field_partnerDescription`, `content`.`field_partnerUrl`, `content`.`field_personPostNominals`, `content`.`field_previewDescription`, `content`.`field_projectDescription`, `content`.`field_projectEndDate`, `content`.`field_projectNo`, `content`.`field_projectRegion`, `content`.`field_projectStartDate`, `content`.`field_projectUrl`, `content`.`field_seoSettings`, `content`.`field_eventStartDate`, `content`.`field_subtitle`, `content`.`field_taxonomyName`, `content`.`field_caseStudyChallenge`, `content`.`field_caseStudyOutcome`, `content`.`field_jobPerson`, `content`.`field_jobRole`, `content`.`field_thoughtLeadershipCampaign`, `content`.`field_timelineText`, `content`.`field_timelineUrl`, `content`.`field_timelineYear`, `content`.`field_personTitle`
  FROM (SELECT `elements`.`id` AS `elementsId`, `elements_sites`.`id` AS `elementsSitesId`, `content`.`id` AS `contentId`
  FROM `elements` `elements`
  INNER JOIN `users` `users` ON `users`.`id` = `elements`.`id`
  INNER JOIN `elements_sites` `elements_sites` ON `elements_sites`.`elementId` = `elements`.`id`
  INNER JOIN `content` `content` ON `content`.`elementId` = `elements`.`id`
  WHERE (`elements`.`id`=1) AND (`elements`.`archived`=FALSE) AND (`elements`.`dateDeleted` IS NULL) AND (`elements`.`draftId` IS NULL) AND (`elements`.`revisionId` IS NULL)
  ORDER BY `users`.`username`
  LIMIT 1) `subquery`
  INNER JOIN `users` `users` ON `users`.`id` = `subquery`.`elementsId`
  INNER JOIN `elements` `elements` ON `elements`.`id` = `subquery`.`elementsId`
  INNER JOIN `elements_sites` `elements_sites` ON `elements_sites`.`id` = `subquery`.`elementsSitesId`
  INNER JOIN `content` `content` ON `content`.`id` = `subquery`.`contentId`
  ORDER BY `users`.`username`
  Error Info: Array
  (
      [0] =&gt; 42S22
      [1] =&gt; 1054
      [2] =&gt; Unknown column 'content.field_projectDescription' in 'field list'
  )
  ↵
  Caused by: PDOException
  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'content.field_projectDescription' in 'field list'
  in /Users/danlee/Sites/Work/galileo/vendor/yiisoft/yii2/db/Command.php at line 1293
  </code></pre>
  <p><strong>Interface showing duplicated fields</strong></p>
  <p><img src="https://i.sstatic.net/oi25L.png" alt="screenshot" /></p>
  <p><strong>Update 2:</strong></p>
  <p>I get the following output when running <code>./craft migrate/all</code>. The odd thing is I can connect to the database fine and edit content without issue.</p>
  <pre><code>Craft can’t connect to the database. Check your connection settings.    
  
  Yii Migration Tool (based on Yii v2.0.38)
  
  Exception 'craft\errors\DbConnectException' with message 'Craft CMS can’t connect to the database with the credentials in config/db.php.'
  
  in /Users/danlee/Sites/Work/galileo/vendor/craftcms/cms/src/db/Connection.php:173
  
  Caused by: Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] No such file or directory'
  
  in /Users/danlee/Sites/Work/galileo/vendor/yiisoft/yii2/db/Connection.php:642
  
  Caused by: Exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory'
  
  in /Users/danlee/Sites/Work/galileo/vendor/yiisoft/yii2/db/Connection.php:710
  </code></pre>
  <p><strong>db.config file</strong></p>
  <pre><code>&lt;?php
  /**
   * Craft 3 Multi-Environment
   * Efficient and flexible multi-environment config for Craft 3 CMS
   *
   * $_ENV constants are loaded by craft3-multi-environment from .env.php via
   * ./web/index.php for web requests, and ./craft for console requests
   *
   * @author    nystudio107
   * @copyright Copyright (c) 2017 nystudio107
   * @link      https://nystudio107.com/
   * @package   craft3-multi-environment
   * @since     1.0.5
   * @license   MIT
   */
  
  /**
   * Database Configuration
   *
   * All of your system's database configuration settings go in here.
   * You can see a list of the default settings in src/config/DbConfig.php
   */
  
  return [
  
      // All environments
      '*' =&gt; [
          'database' =&gt; getenv('CRAFTENV_DB_DATABASE'),
          'driver' =&gt; getenv('CRAFTENV_DB_DRIVER'),
          'server' =&gt; getenv('CRAFTENV_DB_SERVER'),
          'user' =&gt; getenv('CRAFTENV_DB_USER'),
          'password' =&gt; getenv('CRAFTENV_DB_PASSWORD'),
          'schema' =&gt; getenv('CRAFTENV_DB_SCHEMA'),
          'tablePrefix' =&gt; getenv('CRAFTENV_DB_TABLE_PREFIX'),
          'port' =&gt; getenv('CRAFTENV_DB_PORT'),
      ],
  
      // Live (production) environment
      'live'  =&gt; [
      ],
  
      // Staging (pre-production) environment
      'staging'  =&gt; [
      ],
  
      // Local (development) environment
      'local'  =&gt; [
      ],
  ];
  </code></pre>
  <p><strong>env.php file</strong></p>
  <pre><code>&lt;?php
  /**
   * Craft 3 Multi-Environment
   * Efficient and flexible multi-environment config for Craft 3 CMS
   *
   * @author    nystudio107
   * @copyright Copyright (c) 2017 nystudio107
   * @link      https://nystudio107.com/
   * @package   craft3-multi-environment
   * @since     1.0.5
   * @license   MIT
   *
   * This file should be renamed to '.env.php' and it should reside in your root
   * project directory.  Add '/.env.php' to your .gitignore.  See below for production
   * usage notes.
   */
  
  // Determine the incoming protocol
  if (isset($_SERVER['HTTPS']) &amp;&amp; (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)
      || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &amp;&amp; strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0
  ) {
      $protocol = &quot;https://&quot;;
  } else {
      $protocol = &quot;http://&quot;;
  }
  // Determine the server hostname
  $httpHost = $_SERVER['HTTP_HOST'] ?? '';
  // The $craftEnvVars are all auto-prefixed with CRAFTENV_ -- you can add
  // whatever you want here and access them via getenv() using the prefixed name
  $craftEnvVars = [
      // The Craft environment we're running in ('local', 'staging', 'live', etc.).
      'CRAFT_ENVIRONMENT' =&gt; 'local',
  
      // The database driver that will used ('mysql' or 'pgsql')
      'DB_DRIVER' =&gt; 'mysql',
  
      // The database server name or IP address (usually this is 'localhost' or '127.0.0.1') // 185.119.174.157
      'DB_SERVER' =&gt; 'localhost',
  
      // The database username to connect with // einstein_cpi
      'DB_USER' =&gt; 'xxxx',
  
      // The database password to connect with // Market!ng123+
      'DB_PASSWORD' =&gt; 'xxxx',
  
      // The name of the database to select // einstein_franklin
      'DB_DATABASE' =&gt; 'xxxx',
  
      // The database schema that will be used (PostgreSQL only, usually 'public')
      'DB_SCHEMA' =&gt; 'public',
  
      // The prefix that should be added to generated table names (usually '', it's only necessary
      // if multiple things are sharing the same database)
      'DB_TABLE_PREFIX' =&gt; '',
  
      // The port to connect to the database with. Default ports are 3306 for MySQL and 5432 for PostgreSQL.
      'DB_PORT' =&gt; '3306',
  
      // The secure key Craft will use for hashing and encrypting data, see:
      // https://craftcms.com/docs/config-settings#validationKey
      'SECURITY_KEY' =&gt; 'xxxx',
  
      // The site url to use; it can be hard-coded as well
      'SITE_URL' =&gt; $protocol . $httpHost . '/',
  
      // The base url environmentVariable to use for Assets; it can be hard-coded as well
      // You will also need to configure `config/volumes.php` for your Asset Volumes
      'BASE_URL' =&gt; $protocol . $httpHost . '/',
  
      // The base path environmentVariable for Assets; it can be hard-coded as well
      // You will also need to configure `config/volumes.php` for your Asset Volumes
      'BASE_PATH' =&gt; realpath(dirname(__FILE__)) . '/web/content/',
  
  
      // The AWS API key with read/write access to Spaces
      'SPACES_API_KEY' =&gt; 'xxxx',
  
      // The AWS API key secret
      'SPACES_SECRET' =&gt; 'xxxx',
  
      // The region the S3 bucket is in
      'SPACES_ENDPOINT' =&gt; 'xxxx',
  
      // The region the S3 bucket is in
      'SPACES_REGION' =&gt; 'xxxx',
  
      // The name of the S3 bucket
      'SPACES_BUCKET' =&gt; 'xxxx',
  
      // The url of the S3 bucket
      'SPACES_URL' =&gt; 'xxxx',
  
      // Backblaze Settings
      'BACKBLAZE_B2_MASTER_KEY_ID' =&gt; 'xxxx',
      'BACKBLAZE_B2_MASTER_APP_KEY' =&gt; 'xxxx',
      'BACKBLAZE_B2_BUCKET_REGION' =&gt; 'xxxx',
      'BACKBLAZE_B2_BUCKET_NAME' =&gt; 'xxxx',
      'BACKBLAZE_B2_BUCKET_BACKUP_PREFIX' =&gt; 'xxxx',
      'BACKBLAZE_B2_BUCKET_SYNC_PREFIX' =&gt; 'xxxx',
  
  ];
  
  // Set all of the .env values, auto-prefixed with `CRAFTENV_`
  foreach ($craftEnvVars as $key =&gt; $value) {
      putenv(&quot;CRAFTENV_{$key}={$value}&quot;);
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-06T09:43:32.437Z
Comments.Text: It would help if you gave some details about your error. To get some details, you can enable [devMode](https://craftcms.com/docs/3.x/config/config-settings.html#devmode), configure you CP user to show detailed error even when devMode is off or go through the logs.
Comments.UserDisplayName: nstCactus
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-06T10:23:14.287Z
Comments.Text: @nstCactus See updated question above with error. Thanks for your interest.
Comments.UserDisplayName: Dan Lee
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-07T14:28:30.880Z
Comments.Text: Can you post the content of your _config/db.php_ and _.env_ file?
  Be sure to hide credentials though.
Comments.UserDisplayName: nstCactus
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-08T08:48:16.643Z
Comments.Text: Updated with file contents. I'm using NYStudio's multi-environment setup which uses a .env.php file.
Comments.UserDisplayName: Dan Lee
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-08T10:48:44.933Z
Comments.Text: Can you create a separate question to address your update 2? This is to make it easier to read for future viewers, as this is a completely different issue. Sorry, I should have suggested before asking you edit your post.
Comments.UserDisplayName: nstCactus
Tags: <craft3><database>
Title: Deleting Duplicate Fields Brings down Craft Website and Server
ThreadText: # Deleting Duplicate Fields Brings down Craft Website and Server
  
  (Asked by: username_0 on 2020-10-06)
  
  I have somehow managed to duplicate some fields in my Craft install (see image of unused fields). When I delete the unused duplicates I create a server error and can't access Craft.
  
  To rectify I had to switch on and off my DigitalOcean droplet to get the server working again and reinstall a database backup. I want to remove the duplicates safely. How do I do this without bringing down the whole website?
  
  It would also be good to know any theories to why this happened in the first place so I can rectify.
  
  **Update 1**
  
  I deleted a field on my local version and have outputted the databased error below.
  
  Thanks in advance.
  
  **Database Error when deleting field on local version:**
  
  ```
  Database Exception – yii\db\Exception
  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'content.field_projectDescription' in 'field list'
  The SQL being executed was: SELECT `users`.`password`, `elements`.`id`, `elements`.`fieldLayoutId`, `elements`.`uid`, `elements`.`enabled`, `elements`.`archived`, `elements`.`dateCreated`, `elements`.`dateUpdated`, `elements_sites`.`id` AS `siteSettingsId`, `elements_sites`.`slug`, `elements_sites`.`siteId`, `elements_sites`.`uri`, `elements_sites`.`enabled` AS `enabledForSite`, `users`.`username`, `users`.`firstName`, `users`.`lastName`, `users`.`email`, `users`.`unverifiedEmail`, `users`.`admin`, `users`.`locked`, `users`.`pending`, `users`.`suspended`, `users`.`lastLoginDate`, `users`.`lockoutDate`, `users`.`photoId`, `users`.`hasDashboard`, `content`.`id` AS `contentId`, `content`.`field_jobNotes`, `content`.`field_announcementBanner`, `content`.`field_announcementColor`, `content`.`field_announcementLink`, `content`.`field_announcementText`, `content`.`field_personBiography`, `content`.`field_careersForm`, `content`.`field_caseStudyClientName`, `content`.`field_caseStudyClientQuote`, `content`.`field_caseStudyClientAttribution`, `content`.`field_caseStudyUrl`, `content`.`field_eventEndDate`, `content`.`field_eventCost`, `content`.`field_eventHosted`, `content`.`field_eventLocation`, `content`.`field_eventUrl`, `content`.`field_expertOpinion`, `content`.`field_featured`, `content`.`field_fundDescription`, `content`.`field_fundUrl`, `content`.`field_fundingOpportunityAim`, `content`.`field_fundingOpportunityCurrency`, `content`.`field_fundingOpportunityDescription`, `content`.`field_fundingOpportunityFunder`, `content`.`field_fundingOpportunityFundingType`, `content`.`field_fundingOpportunityLimit`, `content`.`field_fundingOpportunityProjectStartDate`, `content`.`field_fundingOpportunityResultDate`, `content`.`field_fundingOpportunityScope`, `content`.`field_fundingOpportunityShowStartDate`, `content`.`field_fundingOpportunitySubmissionDate`, `content`.`field_fundingOpportunityURL`, `content`.`field_fundingOpportunityValue`, `content`.`field_headOffice`, `content`.`field_globalHeaderMeta`, `content`.`field_heading`, `content`.`field_hidden`, `content`.`field_imageVariants16by9`, `content`.`field_imageVariants16by9Parallax`, `content`.`field_imageVariantsThumb`, `content`.`field_innovationIntegratorAssetRef`, `content`.`field_intro`, `content`.`field_investorInsight`, `content`.`field_jobApplicationId`, `content`.`field_jobApplicationType`, `content`.`field_jobEligibilityNotice`, `content`.`field_jobReferenceNo`, `content`.`field_jobSalary`, `content`.`field_personJobTitle`, `content`.`field_latitude`, `content`.`field_linkedInProfile`, `content`.`field_locationAddress`, `content`.`field_locationPhone`, `content`.`field_logoVariants`, `content`.`field_longitude`, `content`.`field_mapLocation`, `content`.`field_partnerDescription`, `content`.`field_partnerUrl`, `content`.`field_personPostNominals`, `content`.`field_previewDescription`, `content`.`field_projectDescription`, `content`.`field_projectEndDate`, `content`.`field_projectNo`, `content`.`field_projectRegion`, `content`.`field_projectStartDate`, `content`.`field_projectUrl`, `content`.`field_seoSettings`, `content`.`field_eventStartDate`, `content`.`field_subtitle`, `content`.`field_taxonomyName`, `content`.`field_caseStudyChallenge`, `content`.`field_caseStudyOutcome`, `content`.`field_jobPerson`, `content`.`field_jobRole`, `content`.`field_thoughtLeadershipCampaign`, `content`.`field_timelineText`, `content`.`field_timelineUrl`, `content`.`field_timelineYear`, `content`.`field_personTitle`
  FROM (SELECT `elements`.`id` AS `elementsId`, `elements_sites`.`id` AS `elementsSitesId`, `content`.`id` AS `contentId`
  FROM `elements` `elements`
  INNER JOIN `users` `users` ON `users`.`id` = `elements`.`id`
  INNER JOIN `elements_sites` `elements_sites` ON `elements_sites`.`elementId` = `elements`.`id`
  INNER JOIN `content` `content` ON `content`.`elementId` = `elements`.`id`
  WHERE (`elements`.`id`=1) AND (`elements`.`archived`=FALSE) AND (`elements`.`dateDeleted` IS NULL) AND (`elements`.`draftId` IS NULL) AND (`elements`.`revisionId` IS NULL)
  ORDER BY `users`.`username`
  LIMIT 1) `subquery`
  INNER JOIN `users` `users` ON `users`.`id` = `subquery`.`elementsId`
  INNER JOIN `elements` `elements` ON `elements`.`id` = `subquery`.`elementsId`
  INNER JOIN `elements_sites` `elements_sites` ON `elements_sites`.`id` = `subquery`.`elementsSitesId`
  INNER JOIN `content` `content` ON `content`.`id` = `subquery`.`contentId`
  ORDER BY `users`.`username`
  Error Info: Array
  (
      [0] => 42S22
      [1] => 1054
      [2] => Unknown column 'content.field_projectDescription' in 'field list'
  )
  ↵
  Caused by: PDOException
  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'content.field_projectDescription' in 'field list'
  in /Users/username_0/Sites/Work/galileo/vendor/yiisoft/yii2/db/Command.php at line 1293
  
  ```
  
  **Interface showing duplicated fields**
  
  ![screenshot](https://i.sstatic.net/oi25L.png)
  
  **Update 2:**
  
  I get the following output when running `./craft migrate/all`. The odd thing is I can connect to the database fine and edit content without issue.
  
  ```
  Craft can’t connect to the database. Check your connection settings.    
  
  Yii Migration Tool (based on Yii v2.0.38)
  
  Exception 'craft\errors\DbConnectException' with message 'Craft CMS can’t connect to the database with the credentials in config/db.php.'
  
  in /Users/username_0/Sites/Work/galileo/vendor/craftcms/cms/src/db/Connection.php:173
  
  Caused by: Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] No such file or directory'
  
  in /Users/username_0/Sites/Work/galileo/vendor/yiisoft/yii2/db/Connection.php:642
  
  Caused by: Exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory'
  
  in /Users/username_0/Sites/Work/galileo/vendor/yiisoft/yii2/db/Connection.php:710
  
  ```
  
  **db.config file**
  
  ```
  <?php
  /**
   * Craft 3 Multi-Environment
   * Efficient and flexible multi-environment config for Craft 3 CMS
   *
   * $_ENV constants are loaded by craft3-multi-environment from .env.php via
   * ./web/index.php for web requests, and ./craft for console requests
   *
   * @author    nystudio107
   * @copyright Copyright (c) 2017 nystudio107
   * @link      https://nystudio107.com/
   * @package   craft3-multi-environment
   * @since     1.0.5
   * @license   MIT
   */
  
  /**
   * Database Configuration
   *
   * All of your system's database configuration settings go in here.
   * You can see a list of the default settings in src/config/DbConfig.php
   */
  
  return [
  
      // All environments
      '*' => [
          'database' => getenv('CRAFTENV_DB_DATABASE'),
          'driver' => getenv('CRAFTENV_DB_DRIVER'),
          'server' => getenv('CRAFTENV_DB_SERVER'),
          'user' => getenv('CRAFTENV_DB_USER'),
          'password' => getenv('CRAFTENV_DB_PASSWORD'),
          'schema' => getenv('CRAFTENV_DB_SCHEMA'),
          'tablePrefix' => getenv('CRAFTENV_DB_TABLE_PREFIX'),
          'port' => getenv('CRAFTENV_DB_PORT'),
      ],
  
      // Live (production) environment
      'live'  => [
      ],
  
      // Staging (pre-production) environment
      'staging'  => [
      ],
  
      // Local (development) environment
      'local'  => [
      ],
  ];
  
  ```
  
  **env.php file**
  
  ```
  <?php
  /**
   * Craft 3 Multi-Environment
   * Efficient and flexible multi-environment config for Craft 3 CMS
   *
   * @author    nystudio107
   * @copyright Copyright (c) 2017 nystudio107
   * @link      https://nystudio107.com/
   * @package   craft3-multi-environment
   * @since     1.0.5
   * @license   MIT
   *
   * This file should be renamed to '.env.php' and it should reside in your root
   * project directory.  Add '/.env.php' to your .gitignore.  See below for production
   * usage notes.
   */
  
  // Determine the incoming protocol
  if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)
      || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0
  ) {
      $protocol = "https://";
  } else {
      $protocol = "http://";
  }
  // Determine the server hostname
  $httpHost = $_SERVER['HTTP_HOST'] ?? '';
  // The $craftEnvVars are all auto-prefixed with CRAFTENV_ -- you can add
  // whatever you want here and access them via getenv() using the prefixed name
  $craftEnvVars = [
      // The Craft environment we're running in ('local', 'staging', 'live', etc.).
      'CRAFT_ENVIRONMENT' => 'local',
  
      // The database driver that will used ('mysql' or 'pgsql')
      'DB_DRIVER' => 'mysql',
  
      // The database server name or IP address (usually this is 'localhost' or '127.0.0.1') // 185.119.174.157
      'DB_SERVER' => 'localhost',
  
      // The database username to connect with // einstein_cpi
      'DB_USER' => 'xxxx',
  
      // The database password to connect with // Market!ng123+
      'DB_PASSWORD' => 'xxxx',
  
      // The name of the database to select // einstein_franklin
      'DB_DATABASE' => 'xxxx',
  
      // The database schema that will be used (PostgreSQL only, usually 'public')
      'DB_SCHEMA' => 'public',
  
      // The prefix that should be added to generated table names (usually '', it's only necessary
      // if multiple things are sharing the same database)
      'DB_TABLE_PREFIX' => '',
  
      // The port to connect to the database with. Default ports are 3306 for MySQL and 5432 for PostgreSQL.
      'DB_PORT' => '3306',
  
      // The secure key Craft will use for hashing and encrypting data, see:
      // https://craftcms.com/docs/config-settings#validationKey
      'SECURITY_KEY' => 'xxxx',
  
      // The site url to use; it can be hard-coded as well
      'SITE_URL' => $protocol . $httpHost . '/',
  
      // The base url environmentVariable to use for Assets; it can be hard-coded as well
      // You will also need to configure `config/volumes.php` for your Asset Volumes
      'BASE_URL' => $protocol . $httpHost . '/',
  
      // The base path environmentVariable for Assets; it can be hard-coded as well
      // You will also need to configure `config/volumes.php` for your Asset Volumes
      'BASE_PATH' => realpath(dirname(__FILE__)) . '/web/content/',
  
      // The AWS API key with read/write access to Spaces
      'SPACES_API_KEY' => 'xxxx',
  
      // The AWS API key secret
      'SPACES_SECRET' => 'xxxx',
  
      // The region the S3 bucket is in
      'SPACES_ENDPOINT' => 'xxxx',
  
      // The region the S3 bucket is in
      'SPACES_REGION' => 'xxxx',
  
      // The name of the S3 bucket
      'SPACES_BUCKET' => 'xxxx',
  
      // The url of the S3 bucket
      'SPACES_URL' => 'xxxx',
  
      // Backblaze Settings
      'BACKBLAZE_B2_MASTER_KEY_ID' => 'xxxx',
      'BACKBLAZE_B2_MASTER_APP_KEY' => 'xxxx',
      'BACKBLAZE_B2_BUCKET_REGION' => 'xxxx',
      'BACKBLAZE_B2_BUCKET_NAME' => 'xxxx',
      'BACKBLAZE_B2_BUCKET_BACKUP_PREFIX' => 'xxxx',
      'BACKBLAZE_B2_BUCKET_SYNC_PREFIX' => 'xxxx',
  
  ];
  
  // Set all of the .env values, auto-prefixed with `CRAFTENV_`
  foreach ($craftEnvVars as $key => $value) {
      putenv("CRAFTENV_{$key}={$value}");
  }
  
  ```
  
  **Tags:** craft3, database
  
  > **Comments:**
  > **username_1**: It would help if you gave some details about your error. To get some details, you can enable [devMode](https://craftcms.com/docs/3.x/config/config-settings.html#devmode), configure you CP user to show detailed error even when devMode is off or go through the logs.  
  > **username_0**: @username_1 See updated question above with error. Thanks for your interest.  
  > **username_1**: Can you post the content of your _config/db.php_ and _.env_ file? Be sure to hide credentials though.  
  > **username_0**: Updated with file contents. I'm using NYStudio's multi-environment setup which uses a .env.php file.  
  > **username_1**: Can you create a separate question to address your update 2? This is to make it easier to read for future viewers, as this is a completely different issue. Sorry, I should have suggested before asking you edit your post.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-10-06. Score: 2
  
  It looks like Craft is trying to delete a database field that has already been removed. I have no idea how you ended up in this situation but here's what I'd try to resolve the issue:
  
  * first **backup your database**, in case something goes wrong!
  * then execute the following command on your server, in the project directory. After each command, try removing you field again from the Control Panel, if it still fails, try the next one:
    + `./craft migrate/all`
    + `./craft project-config/sync`
    + `./craft project-config/sync --force`
  
  If any of these commands returns an error, post the output.
  
  If no errors are returned by these commands but you still can't remove your field, just add the missing database column in your database as `VARCHAR` try again, this should do the trick.
  
  > **Comments:**
  > **username_0**: thanks for your response. I get an error saying it can't find the database when running the first command. See update with output above.  
  
  ---
  
  ### Answer by username_2 on 2022-06-24. Score: 1
  
  Pardon me, I wrote this based on my memory.
  
  The table name is read from Yaml file.
  But the database doesn't have the table.
  -> This means you did remove the table once, but due to project.yaml unchanged/reverted, the duplicated field returns.
  
  The root cause might be adding 2 fields with different UIDs but the same handle into project.yaml
  
  I have 2 solutions here:
  
  [Solution 1]
  
  1. (optional, I'm not sure whether Craft can handle this step or must do manually) Remove the duplicated field by searching the commented field name in project.yaml.
  2. Create an empty table with the same name. Import it then remove the
     field from Fields screen. (this helped me solve the issue)
  
  [Solution 2]
  
  1. Enable devMode, read stack trace.
  2. Open the .php file (ex: services/Field.php at line 000)
  3. Crack the code by ignoring error when accessing columns
  
  !!!Warning!!!
  You might lose the original field and its data when trying to delete duplicated version.
  
  
  ---
  
  