php - Laravel 错误:SQLSTATE [42S01]:基表或视图已存在:1050 表“类别”已存在
问题描述
我无法弄清楚问题是什么,2个表由于某种原因没有连接,我阅读了很多文章并尝试了很多东西仍然无法正常工作。
我想将帖子和类别表链接在一起,这样当我可以显示在发布的帖子中选择的类别时。
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');
});
}
类别
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');
});
}
这是我得到的错误:
SQLSTATE [42S01]:基表或视图已存在:1050 表“类别”已存在(SQL:创建表
categories
(id
bigint unsigned not null auto_increment 主键,created_at
timestamp null,updated_at
timestamp null,name
varchar(255) not null,post_id
bigint unsigned not null ) 默认字符集 utf8mb4 collate 'utf8mb4_unicode_ci')
解决方案
migrate:refresh
尝试使用artisan 命令完全刷新数据库。
php artisan migrate:refresh --seed
可能是数据库迁移在注册到migrations
数据库表之前运行并失败了。
问题:(到目前为止)
1) 如上所述,amigrate:refresh
整理出原来的错误
2)$table->bigInteger('post_id')->unsigned();
不会像posts.id
aninteger
而不是 a那样工作bigInteger
。
解决方案:
post_id
将您的定义更改为
$table->integer('post_id')->unsigned();
推荐阅读
- javascript - 如果特定用户是@,则说一个短语
- html - 如何定位引导导航栏 (v4.5) 的 HTML 元素以使用 CSS 自定义样式
- python-3.x - 设置一个变量等于熊猫数据框中相邻列中的值
- javascript - 如何使用 Javascript 创建 JSON 对象?
- video - 上传到 AppStore Connect 的 App Preview 图像的云图标
- javascript - 如何在 v-calendar 中更改周末的颜色(周六和周日分别为不同的颜色)
- swagger - 大摇大摆地限制查询参数
- android - 如何实例化扩展 AndroidViewModel 的 ViewModel?
- python - 在我引用之前未分配全局变量的问题
- vega-lite - Vega-Lite X 轴标签未完整显示