Schreiben Sie den Titel wie folgt um: Konvertieren Sie den SQL-Code von PHP Laravel in das Postgres-Format
P粉252116587
P粉252116587 2024-01-16 13:47:34
0
1
389

Ich schreibe eine in phplaravel geschriebene Webanwendung in einen JavaScript-Stack um. Derzeit überarbeite ich mein Datenbankschema, das scheinbar MySQL für Postgres ist.

Ich bin etwas verwirrt über die Syntax der folgenden create tableBefehle

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');
        });
    }

Nach meinem Verständnis ist das Äquivalent von postgres oben

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

Aber ich bin mir nicht sicher, ob ich es richtig übersetze $table->string('ip_address', 45)->nullable(); ,因为我不确定 string('ip_address', 45 ).

Ist meine Konvertierung in Potgres korrekt oder muss ich etwas ändern, um das Äquivalent im Postgres-Erstellungsbefehl zu erhalten?

P粉252116587
P粉252116587

Antworte allen(1)
P粉184747536

例如,您可以按照开发人员的预期利用 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 整数
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!