A
A
Alixx2019-01-30 03:32:09
PostgreSQL
Alixx, 2019-01-30 03:32:09

Switching to postgresql (with mysql) in laravel migrations?

I have windows 10, postgresql 11 (+pgAdmin 4), php 7.2.11, laravel 5.4, locally on my computer. I am in the process of migrating from mysql to postgresql, namely rewriting migrations. It turns out that Lara is not well suited for posqtgresql, which is why I ask for help to figure it out correctly.
1. autoincrement
$tsble->autoincrement('id'); creates an integer field with DEFAULT nextval('users_id_seq'::regclass);
But in postgresql there is a serial type, it seems like it was created specifically for auto-increment IDs. Then you need to use it? But there is no such type in laravel migrations. Then leave the auto-increment in the migration as it is, and then after the migration manually change the field type to serial?
2. unsigned
It turns out it is not in postgresql. Replace with CHECK (field_name >= 0)? Or any other options (if any, why are they better)? And again, there is no check() in Lara migrations - do you have to fix it manually later in postgresql itself?
3. arrays
For mysql, I used string and wrote data there as json. For postgresql, I chose directly json for now. It is available in migrations. Or should I choose an array type? What is the difference between arrays and json in postgresql?
Something laravel, as I see it so far, is not well adapted to work with postgresql. The questions may seem silly to some, but nevertheless they came to me, I read them - I remained incomprehensible. In the lara there is nothing at all about the differences from Postgresql, so please help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JhaoDa, 2019-01-30
@alixx

1.autoincrement
2.unsigned
The basic level is to add a field of the required type or change the field type manually through \DB::statement().
The next level is to extend schema builder and PostgresGrammar.
Overbrain level - expand and make a pull request to the Illuminate turnip.
Moreover, in PG 10 a more correct way of autoincrement appeared and Laravel 5.7 already supports it .
3. arrays
Believe it or not, they differ in that one is an array, and the other is json. And although they are sometimes similar, they are different data types. The documentation is in Russian, study.
In the lara there is nothing at all about the differences from Postgresql
This is the point. The choice of a database is not related to Laravel, ideally it should not matter to you what kind of database it is. Therefore, Laravel's little bits support a more or less common set of data types, replacing them with others, as in the case of json and old versions of mysql.
Everything that is not there and that you specifically need, you must implement. And make a PR, hoping that Taylor will not reject it :) Well, or if there is a critical mass of those who need it.

V
Vyacheslav Uspensky, 2019-07-24
@Kwisatz

Do not worry, as
Melkij correctly noted
As a rule, any non-standard things out of the box are supported a little less than nothing. I would even say everything that goes beyond the primitive. Moreover, in some languages, the drivers themselves impose restrictions. For example, PHP does not know how to work with PostgreSQL arrays, at all. As a rule, they bypass this matter like this, or immediately use jsonb

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question