【问题标题】:MySql create simple foreign key between two tableMySql 在两个表之间创建简单的外键
【发布时间】:2016-05-07 23:28:23
【问题描述】:

在这个简单的两个表中,我想在amount_repositories.user_iduser_amounts_account.id 之间创建外键,但我得到了错误:

Mysql 创建表:

CREATE TABLE IF NOT EXISTS `report_transactions` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `amount` int(11) NOT NULL,
  `order_id` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `payment_order_id` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `token` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `reference_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `given_reference_id` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
  `description` text COLLATE utf8_unicode_ci NOT NULL,
  `redirect_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `type_result` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `result` tinyint(4) NOT NULL,
  `customer_id` int(10) unsigned NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`),
  KEY `report_transactions_customer_id_foreign` (`customer_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=232 ;

CREATE TABLE IF NOT EXISTS `amount_repositories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `amount` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`),
  KEY `amount_repositories_user_id_foreign` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `user_amounts_account` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `amount` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `type` tinyint(4) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;


ALTER TABLE `amount_repositories`
  ADD CONSTRAINT `amount_repositories_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `report_transactions` (`customer_id`);

ALTER TABLE `user_amounts_account`
  ADD CONSTRAINT `user_amounts_account_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);

ALTER TABLE `amount_repositories`
  ADD CONSTRAINT `amount_repositories_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `user_amounts_account` (`id`);

错误:

#1005 - Can't create table 'test.#sql-4c2_403' (errno: 150) (Details…) 

【问题讨论】:

  • 您的字段定义很可能不相同。你能发布用于创建这两个表的确切命令吗?
  • 表中是否已有数据?
  • @Atli 帖子已更新,谢谢
  • @GauravLad 不,先生,我的表现在是空的,我只想创建简单的关系
  • ALTER TABLE amount_repositories ADD CONSTRAINT amount_repositories_user_id_foreign FOREIGN KEY (user_id) REFERENCES user_amounts_account(id) its working n phpmyadmin

标签: php mysql laravel


【解决方案1】:

一般情况下,代码是这样的,

Schema::create('gigs', function($table)
{
    $table->increments('gig_id');

    $table->dateTime('gig_startdate');

    $table->integer('band_id')->unsigned();
    $table->integer('stage_id')->unsigned();
});

Schema::table('gigs', function($table)
{
    $table->foreign('band_id')
        ->references('band_id')->on('bands')
        ->onDelete('cascade');

    $table->foreign('stage_id')
        ->references('stage_id')->on('stages')
        ->onDelete('cascade');
});

【讨论】:

    【解决方案2】:

    创建 PHP 类文件 create_user_amounts_account_table.php 并放置以下代码

    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class UserAmountsAccount extends Migration
    {
    
       /**
       * Run the migrations.
       *
       * @return void
       */
       public function up()
       {
        Schema::create('UserAmountsAccount', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('amount');
            $table->boolean('type');
            $table->timestamps();
        });
    }
    
       /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
        Schema::drop('UserAmountsAccount');
      }
    
    }
    

    创建 PHP 类文件 create_amount_repositories_table.php 并放置以下代码

    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class AmountRepositories extends Migration
    {
    
       /**
       * Run the migrations.
       *
       * @return void
       */
       public function up()
       {
        Schema::create('AmountRepositories', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('amount');
            $table->timestamps();
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')
                ->references('id')
                ->on('user_amounts_account')->onDelete('cascade');
        });
    }
    
       /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
        Schema::drop('AmountRepositories');
      }
    
    }
    

    最后运行迁移。就是这样!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 2021-11-18
      • 1970-01-01
      相关资源
      最近更新 更多