[英]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.id
是integer
而不是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()
,因为如消息所述,外键必须引用主键。
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 字节,但bigIncrements
是8 字节,这可能会导致将来出现问题
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.