首页  >  问答  >  正文

将标题重写为:将php laravel的sql代码转换为postgres格式

我正在将用 phplaravel 编写的 Web 应用程序重写为 JavaScript 堆栈。目前我正在重新设计数据库模式,似乎是 mysql 到 postgres。

我对以下 create table 命令的一些语法有点困惑

public function up()
    {
        Schema::create('sessions', function (Blueprint $table) {
            $table->string('id')->unique();
            $table->unsignedInteger('user_id')->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->text('payload');
            $table->integer('last_activity');
        });
    }

根据我的理解,上面的 postgres 等效项是

create table sessions (
    id text unique not null,
    user_id int references users,
    ip_address text,
    user_agent text,
    payload text,
    last_activity integer
    
);

但是我不确定我是否正确翻译了 $table->string('ip_address', 45)->nullable(); ,因为我不确定 string('ip_address', 45 ) 正在做。

我对 potgres 的转换是否正确,或者我需要更改什么才能在 postgres create 命令中获得等效的内容?

P粉252116587P粉252116587251 天前392

全部回复(1)我来回复

  • P粉184747536

    P粉1847475362024-01-17 09:24:02

    例如,您可以按照开发人员的预期利用 artisan 命令的迁移。它已在如何将 Laravel 迁移转换为原始 SQL 脚本中进行了解释 您可以使用:

    php artisan migrate --pretend

    但是,它有一个警告,您需要有一个可用的数据库服务器才能真正使其工作。它将在目标数据库中创建migrations表(如果不存在),但不会在迁移中创建任何表。它还将遵循 migrations 表,因此您可能需要在运行 pretend 之前使用新数据库或截断 migrations 表。

    或者,您可以深入研究 Laravel 的 SQL“语法”代码 并从中找出答案。遗憾的是,目前还没有人为其制作一个易于阅读的参考表。

    在你的情况下,它大致翻译如下:

    Laravel Postgre
    $table->string('id')->unique(); id varchar
    $table->unsignedInteger('user_id')->nullable(); user_id 整数 null
    $table->string('ip_address', 45)->nullable(); ip_address varchar(45) null
    $table->text('user_agent')->nullable(); user_agent varchar null
    $table->text('payload'); 有效负载文本
    $table->integer('last_activity'); last_activity 整数

    回复
    0
  • 取消回复