简体   繁体   English

Laravel 错误:SQLSTATE[42S01]:基表或视图已存在:1050 表“类别”已存在

[英]Laravel Error : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'categories' already exists

I cant figure out what the problem is, the 2 tables are not connecting for some reason, I read many articles and tried many things still not working.我无法弄清楚问题是什么,2个表由于某种原因没有连接,我阅读了很多文章并尝试了很多东西仍然无法正常工作。

I want to link post and category tables together, so when I can display the category chosen in the post made.我想将帖子和类别表链接在一起,这样当我可以显示在发布的帖子中选择的类别时。

  public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->string('name');
            $table->text('description');
            $table->integer('category_id');
            $table->integer('price');
            $table->integer('currency_id');
        });
    }

Category类别

 public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('name');
            $table->bigInteger('post_id')->unsigned();
            $table->foreign('post_id')->references('id')->on('posts');
        });
    }

This is the error I get:这是我得到的错误:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'categories' already exists (SQL: create table categories ( id bigint unsigned not null auto_increment primary key, created_at timestamp null, updated_at timestamp null, name varchar(255) not null, post_id bigint unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci') SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'categories' already exists (SQL: create table categories ( id bigint unsigned not null auto_increment primary key, created_at timestamp null, updated_at timestamp null, name varchar(255) not null , post_id bigint unsigned not null) 默认字符集 utf8mb4 collate 'utf8mb4_unicode_ci')

Try refreshing you database entirely using the migrate:refresh artisan command.尝试使用migrate:refresh artisan 命令完全刷新您的数据库。

php artisan migrate:refresh --seed

It may be that a database migration ran and failed before it could register in the migrations table of your database.可能是数据库迁移在它可以注册到数据库的migrations表中之前运行并失败。


Issues: (so far)问题:(到目前为止)

1) As per above, a migrate:refresh sorts out the original error 1)如上,一个migrate:refresh整理出原来的错误

2) $table->bigInteger('post_id')->unsigned(); 2) $table->bigInteger('post_id')->unsigned(); will not work as posts.id is an integer and not a bigInteger .将不起作用,因为posts.idinteger而不是bigInteger

Solution:解决方案:

Change your post_id definition to将您的post_id定义更改为

$table->integer('post_id')->unsigned();

It says the categories table already exists.它说类别表已经存在。 So what you have to do is, If you are in dev env, you can delete the table and try it or you can do like the below command.所以你要做的是,如果你在开发环境中,你可以删除表并尝试它,或者你可以像下面的命令一样做。

Run artisan like this, it will drop all tables and migrate fresher.像这样运行 artisan,它将删除所有表并迁移得更新鲜。

php artisan migrate:fresh

This worked for me.这对我有用。

The answer is very simple, you have to change $table->increments('id') in posts table to $table->id() , because the foreign key must refer to a primary key as the message says.答案很简单,您必须将帖子表中$table->increments('id')更改为$table->id() ,因为如消息所述,外键必须引用主键。

Here are some tips for you这里有一些提示给你

  • You have to use bigIncrements in the posts table instead integer because the length of integer is 4-byte but the bigIncrements is 8-byte and this may cause a problem in the future您必须在posts表中使用bigIncrements而不是integer因为integer的长度是4 字节,但bigIncrements8 字节,这可能会导致将来出现问题

  • You may love to use this line你可能喜欢使用这条线

$table->foreignId('post_id')->constrained();

instead反而

$table->bigInteger('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts');

for simplicity为简单起见

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE [42S01]:基本表或视图已存在或基本表或视图已存在:1050表 - SQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Table SQLSTATE[42S01]:基表或视图已存在:1050 表“付款”已存在(SQL:创建表“付款” - SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'payments' already exists (SQL: create table `payments` SQLSTATE [42S01]:基本表或视图已存在:1050表'weee_tax'已存在,查询为 - SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'weee_tax' already exists, query was Illuminate\Database\QueryException SQLSTATE[42S01]:基表或视图已存在:1050 表“发票”已存在 - Illuminate\Database\QueryException SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'invoices' already exists laravel中如何修复“ SQLSTATE [42S01]:基本表或视图已存在”错误 - How to fix 'SQLSTATE[42S01]:base table or view already exists' error in laravel 错误处理请求:文件错误:-SQLSTATE [42S01]:基表或视图已存在: - Error Processing Request: Error in file: - SQLSTATE[42S01]: Base table or view already exists: 处理请求时出错:SQLSTATE [42S01]:基表或视图已存在:在magento 1.9中 - Error processing your request: SQLSTATE[42S01]: Base table or view already exists: in magento 1.9 Laravel 5.5 错误基本表或视图已存在:1050 表“用户”已存在 - Laravel 5.5 Error Base table or view already exists: 1050 Table 'users' already exists Laravel Forge:基本表或视图已存在:1050表已存在 - Laravel Forge: Base table or view already exists: 1050 Table already exists 基本表或视图已存在:1050表'sales_flat_order'已存在 - Base table or view already exists: 1050 Table 'sales_flat_order' already exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM