简体   繁体   English

使用Postgres在Laravel 5.5 Migrations中定义字段

[英]Defining fields in Laravel 5.5 Migrations with Postgres

Working with Laravel 5.5 Migrations with Postgres : 使用Laravel 5.5和Postgres进行迁移:

1) Defining field as : 1)将字段定义为:

$table->enum('is_applied', ['Y', 'N']) ->comment('Y => is_applied, N=> Is not applied');

I see in generated table next: 我接下来在生成的表中看到:

is_applied varchar(255) NOT NULL,
    CONSTRAINT cs_tmp_csvps_is_applied_check CHECK (((is_applied)::text = ANY ((ARRAY['Y'::character varying, 'N'::character varying])::text[]))),

actually I would like to get sql I used in Postgres with type: 实际上,我想获取我在Postgres中使用的sql类型:

CREATE TYPE type_cms_item_content_type AS ENUM (
    'P',
    'M',
    'H'
);

    content_type type_cms_item_content_type NOT NULL DEFAULT 'P'::type_cms_item_content_type,

Is it possible? 可能吗?

2) Defining field as : 2)将字段定义为:

$table->timestamps();

I have 2 fields created created_at and updated_at . 我创建了两个字段created_at和Updated_at。 As I need to create only created_at I could write : 因为我只需要创建created_at,所以我可以这样写:

 </ s> </ s> </ s>

$table->dateTime('created_at');

But is there is a way to set default time on server, like : 但是有一种方法可以在服务器上设置默认时间,例如:

created_at timestamp NOT NULL DEFAULT now(),

?

Thanks! 谢谢!

You can do: 你可以做:

$table->timestamp('created_at')->useCurrent();

for the timestamps issue. 有关时间戳记的问题。 If you really want exact sql changes you can always do a raw sql statement in a migration: 如果您确实想要确切的sql更改,则始终可以在迁移中执行原始sql语句:

Schema::table('content_type', function(Blueprint $table){
    $sql = 'CREATE TYPE type_cms_item_content_type AS ENUM (
    `P`,
    `M`,
    `H`
);

    content_type type_cms_item_content_type NOT NULL DEFAULT `P`::type_cms_item_content_type';
    DB::connection()->getPdo()->exec($sql);
});

1) Defining field as: 1)将字段定义为:

$table->enum('is_applied', ['Y', 'N']) ->comment('Y => is_applied, N=> Is not applied');

The method comment() it's only for MySQL, in the Migrations on Laravel. Laravel的Migrations中的comment()方法仅适用于MySQL。

See for more details: https://laravel.com/docs/5.5/migrations#creating-columns 有关更多详细信息,请参见: https : //laravel.com/docs/5.5/migrations#creating-columns

2) Defining field as: 2)将字段定义为:

The Answer of @alex-harris is a gob option @ alex-harris的答案是一个不错的选择

$table->timestamp('created_at')->useCurrent();

See: https://laravel.com/docs/5.5/migrations#column-modifiers for more details 请参阅: https//laravel.com/docs/5.5/migrations#column-modifiers了解更多详细信息

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM