Working Flyway with Postgresql Database

Asked

Viewed 687 times

0

Take a look at the repository.

Repository of the Algaworks Institution

This project is not mine, but my project is identical to this one with a single difference, I am using Postgresql database and the SQL commands are the ones that exist below;

========================================================================

  CREATE TABLE categoria (
        codigo SERIAL,
        nome VARCHAR(50) NOT NULL,
        PRIMARY KEY(codigo)
    );




INSERT INTO categoria (nome) values ('Lazer');
INSERT INTO categoria (nome) values ('Alimentação');
INSERT INTO categoria (nome) values ('Supermercado');
INSERT INTO categoria (nome) values ('Farmácia');
INSERT INTO categoria (nome) values ('Outros');

===========================================================

CREATE TABLE pessoa (
    codigo SERIAL,
    nome VARCHAR(50) NOT NULL,
    logradouro VARCHAR(30),
    numero VARCHAR(30),
    complemento VARCHAR(30),
    bairro VARCHAR(30),
    cep VARCHAR(30),
    cidade VARCHAR(30),
    estado VARCHAR(30),
    ativo BOOLEAN NOT NULL,
    PRIMARY KEY(codigo)

) ;


INSERT INTO pessoa (nome, logradouro, numero, complemento, bairro, cep, cidade, estado, ativo) values ('João Silva', 'Rua do Abacaxi', '10', null, 'Brasil', '38.400-12', 'Uberlândia', 'MG', true);
INSERT INTO pessoa (nome, logradouro, numero, complemento, bairro, cep, cidade, estado, ativo) values ('Maria Rita', 'Rua do Sabiá', '110', 'Apto 101', 'Colina', '11.400-12', 'Ribeirão Preto', 'SP', true);
INSERT INTO pessoa (nome, logradouro, numero, complemento, bairro, cep, cidade, estado, ativo) values ('Pedro Santos', 'Rua da Bateria', '23', null, 'Morumbi', '54.212-12', 'Goiânia', 'GO', true);
INSERT INTO pessoa (nome, logradouro, numero, complemento, bairro, cep, cidade, estado, ativo) values ('Ricardo Pereira', 'Rua do Motorista', '123', 'Apto 302', 'Aparecida', '38.400-12', 'Salvador', 'BA', true);
INSERT INTO pessoa (nome, logradouro, numero, complemento, bairro, cep, cidade, estado, ativo) values ('Josué Mariano', 'Av Rio Branco', '321', null, 'Jardins', '56.400-12', 'Natal', 'RN', true);
INSERT INTO pessoa (nome, logradouro, numero, complemento, bairro, cep, cidade, estado, ativo) values ('Pedro Barbosa', 'Av Brasil', '100', null, 'Tubalina', '77.400-12', 'Porto Alegre', 'RS', true);
INSERT INTO pessoa (nome, logradouro, numero, complemento, bairro, cep, cidade, estado, ativo) values ('Henrique Medeiros', 'Rua do Sapo', '1120', 'Apto 201', 'Centro', '12.400-12', 'Rio de Janeiro', 'RJ', true);
INSERT INTO pessoa (nome, logradouro, numero, complemento, bairro, cep, cidade, estado, ativo) values ('Carlos Santana', 'Rua da Manga', '433', null, 'Centro', '31.400-12', 'Belo Horizonte', 'MG', true);
INSERT INTO pessoa (nome, logradouro, numero, complemento, bairro, cep, cidade, estado, ativo) values ('Leonardo Oliveira', 'Rua do Músico', '566', null, 'Segismundo Pereira', '38.400-00', 'Uberlândia', 'MG', true);
INSERT INTO pessoa (nome, logradouro, numero, complemento, bairro, cep, cidade, estado, ativo) values ('Isabela Martins', 'Rua da Terra', '1233', 'Apto 10', 'Vigilato', '99.400-12', 'Manaus', 'AM', true);

If we were to take the Algaworks project and run it it would create the tables and insert the records as shown in one of the project templates, take a look below;

CREATE TABLE categoria (
        codigo SERIAL,
        nome VARCHAR(50) NOT NULL,
        PRIMARY KEY(codigo)
    );




INSERT INTO categoria (nome) values ('Lazer');
INSERT INTO categoria (nome) values ('Alimentação');
INSERT INTO categoria (nome) values ('Supermercado');
INSERT INTO categoria (nome) values ('Farmácia');
INSERT INTO categoria (nome) values ('Outros');

What’s wrong with me? When I will run using the database Postgresql it only creates the tables, it does not execute the Insert s, it does not record the records.

I wonder why that is! If it runs with the Mysql database it runs the Insert s, but with the Postgre database it only creates the tables and does not insert the records.

========================================================================

console message:

Hibernate: drop table if exists categoria cascade
Hibernate: drop table if exists lancamento cascade
Hibernate: drop table if exists pessoa cascade
Hibernate: create table categoria (codigo  bigserial not null, nome varchar(20) not null, primary key (codigo))
Hibernate: create table lancamento (codigo  bigserial not null, data_pagamento date, data_vencimento date not null, descricao varchar(255) not null, observacao varchar(255), tipo varchar(255) not null, valor numeric(19, 2) not null, codigo_categoria int8 not null, codigo_pessoa int8 not null, primary key (codigo))
Hibernate: create table pessoa (codigo  bigserial not null, ativo boolean not null, bairro varchar(255), cep varchar(255), cidade varchar(255), complemento varchar(255), estado varchar(255), logradouro varchar(255), numero varchar(255), nome varchar(255) not null, primary key (codigo))
Hibernate: alter table lancamento add constraint FK3oktdfripre9vo4ocu87op55a foreign key (codigo_categoria) references categoria
Hibernate: alter table lancamento add constraint FK9pqx360616efx77k3w4j297yw foreign key (codigo_pessoa) references pessoa
[2m2017-11-17 09:55:58.547[0;39m [32m INFO[0;39m [35m6652[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36morg.hibernate.tool.hbm2ddl.SchemaExport [0;39m [2m:[0;39m HHH000230: Schema export complete

NOTE: I am using Flyway for database migration.

1 answer

1

Doesn’t actually create the table.

You cannot take SQL from one database and use it in another, each one has a different syntax. Forget what you are doing.

This code more or less converted to Postgresql would look like this:

CREATE TABLE categoria (
    codigo SERIAL,
    nome VARCHAR(50) NOT NULL
);

INSERT INTO categoria (nome) values ('Lazer');
INSERT INTO categoria (nome) values ('Alimentação');
INSERT INTO categoria (nome) values ('Supermercado');
INSERT INTO categoria (nome) values ('Farmácia');
INSERT INTO categoria (nome) values ('Outros');

Behold working in the SQL Fiddle . Also put on the Github for future reference.

Not that it’s a perfect conversion, but just to work.

  • 2

    your suggestion didn’t work, be able to create the tables but not able to enter the records, I wonder what you think is happening?

  • I have no idea, I’ve shown it works.

  • 2

    Maybe I wasn’t clear, I’m not using the SQL code of Postgre direct in the database I’m using flyway, you know how to handle Java?

  • 2

    of course your code will actually work by putting SQL right into the database, you have uniquely shown that it works by putting code right into the database, but you have not shown it working in a Java application. I believe you will only understand what I am saying if you download the application if you test it yourself, if you download it and manage to work on your local computer, then I say I may have done something wrong.

  • There is no purpose of the site, Here we help with specific problems that can be answered, not solve the whole problem for the person.

  • 2

    I know it’s not the goal of the site, but you could do me a courtesy to help me that way, but you’re free not to, but in other posts there were other Stackoverflow collaborators who helped me this way and not so I was accommodated or my learning was deficient.

  • 2

    I’ll rephrase my question, because I’m sure someone else will show up to help me.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.