why Spring-data-jpa didnot save the subclass object? - spring-data-jpa

The reason for this problem is a configuration. Hibernate have a configuration: hibernate.default_batch_fetch_size, so I just added it in application.properties as: spring.jpa.properties.hibernate.jdbc.batch_size(wrong), however you cannot find it in Appendix A. Common application properties. It works for me by deleting this property.
finally, why springboot didnot throw a execption, if he cannot understand this property? Why this error property can interference the commit of transaction?
update2 hibernate sql log
Update1 user and student classes code.
By the way, it was an old project and worked fine with spring 5.0.4 and JPA2.1(Hibernate 5.2.14). I am studying and trying to migrate this project to springboot and spring-data-jpa.
I had a very very weird problem. When I am trying to persit a subclass object to database, spring data jpa can only persist the superclass properties into superclass table, but nothing in the subclass table. And if persist two subclass object at one time, the first object can be saved to subclass table, but the second will be Ignored. However, the insert sql statement seem to be executed(at least it showed in the console).
spring-boot-starter-data-jpa 2.1.0
superclass
#Entity
#Inheritance(strategy = InheritanceType.JOINED)
#Data
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
#Column(unique = true)
private String number;
#JsonIgnore
private String password;
#ManyToOne
private Authority authority;
#Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
#JsonFormat(pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime insertTime;
}
subclass
#Entity
#Data
public class Student extends User{
private String clazz;
#OneToMany(mappedBy = "student")
#OrderBy(value ="id ASC")
private Set<CourseDetail> courseDetails;
#OneToMany(mappedBy = "student")
#OrderBy(value ="id ASC")
private Set<ExperimentDetail> experimentDetails;
}
StudentRepository
#Repository
public interface StudentRepository extends JpaRepository<Student, Long> {}
Service
#Service
#Transactional
#Slf4j
public class TestService {
#Autowired
private StudentRepository studentRep;
public void addStudentTest() {
Student s = new Student();
s.setName("1");
s.setClazz("1");
studentRep.save(s);
Student s1 = new Student();
s1.setName("2");
s1.setClazz("2");
studentRep.save(s1);
// int a = 1 / 0;
log.debug(String.valueOf(TransactionSynchronizationManager.getCurrentTransactionName()));
}
}
Test
#RunWith(SpringRunner.class)
#SpringBootTest
#Slf4j
public class AddUserTest {
#Autowired
private TestService testService;
#Test
public void addStudentTest() {
testService.addStudentTest();
}
}
application.properties
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/coursetest?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf-8
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
Console
Hibernate: insert into user (authority_id, insert_time, name, number, password) values (?, ?, ?, ?, ?)
Hibernate: insert into student (clazz, id) values (?, ?)
Hibernate: insert into user (authority_id, insert_time, name, number, password) values (?, ?, ?, ?, ?)
Hibernate: insert into student (clazz, id) values (?, ?)
2018-12-23 13:39:19 DEBUG com.se.courses.service.TestService.addStudentTest[43] - com.se.courses.service.TestService.addStudentTest
user table: https://i.ibb.co/MG0cNkF/01.png
student table: https://i.ibb.co/0qwv7R2/02.png
Why the console has outputted the insert student SQL statement twice, but user id=3 record not be inserted into the student table?? please help me.
add new loging properties
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=trace
2018-12-24 00:53:19 DEBUG org.springframework.boot.StartupInfoLogger.logStarting[53] - Running with Spring Boot v2.1.0.RELEASE, Spring v5.1.2.RELEASE
2018-12-24 00:53:19 INFO org.springframework.boot.SpringApplication.logStartupProfileInfo[675] - No active profile set, falling back to default profiles: default
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - create table authority (id bigint not null auto_increment, insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, name varchar(255), primary key (id)) engine=InnoDB
Hibernate: create table authority (id bigint not null auto_increment, insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, name varchar(255), primary key (id)) engine=InnoDB
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - create table course (id bigint not null auto_increment, insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, name varchar(255), teacher_id bigint, primary key (id)) engine=InnoDB
Hibernate: create table course (id bigint not null auto_increment, insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, name varchar(255), teacher_id bigint, primary key (id)) engine=InnoDB
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - create table course_detail (id bigint not null auto_increment, insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, course_id bigint, student_id bigint, primary key (id)) engine=InnoDB
Hibernate: create table course_detail (id bigint not null auto_increment, insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, course_id bigint, student_id bigint, primary key (id)) engine=InnoDB
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - create table experiment (id bigint not null auto_increment, file_extension varchar(255), insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, name varchar(255), course_id bigint, primary key (id)) engine=InnoDB
Hibernate: create table experiment (id bigint not null auto_increment, file_extension varchar(255), insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, name varchar(255), course_id bigint, primary key (id)) engine=InnoDB
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - create table experiment_detail (id bigint not null auto_increment, complete_time TIMESTAMP, file varchar(255), experiment_id bigint, student_id bigint, primary key (id)) engine=InnoDB
Hibernate: create table experiment_detail (id bigint not null auto_increment, complete_time TIMESTAMP, file varchar(255), experiment_id bigint, student_id bigint, primary key (id)) engine=InnoDB
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - create table homework (id bigint not null auto_increment, content varchar(255), insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, title varchar(255), course_id bigint, primary key (id)) engine=InnoDB
Hibernate: create table homework (id bigint not null auto_increment, content varchar(255), insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, title varchar(255), course_id bigint, primary key (id)) engine=InnoDB
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - create table homework_detail (id bigint not null auto_increment, complete_time TIMESTAMP, solution TEXT, homework_id bigint, student_id bigint, primary key (id)) engine=InnoDB
Hibernate: create table homework_detail (id bigint not null auto_increment, complete_time TIMESTAMP, solution TEXT, homework_id bigint, student_id bigint, primary key (id)) engine=InnoDB
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - create table student (clazz varchar(255), id bigint not null, primary key (id)) engine=InnoDB
Hibernate: create table student (clazz varchar(255), id bigint not null, primary key (id)) engine=InnoDB
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - create table teacher (id bigint not null, primary key (id)) engine=InnoDB
Hibernate: create table teacher (id bigint not null, primary key (id)) engine=InnoDB
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - create table user (id bigint not null auto_increment, insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, name varchar(255), number varchar(255), password varchar(255), authority_id bigint, primary key (id)) engine=InnoDB
Hibernate: create table user (id bigint not null auto_increment, insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, name varchar(255), number varchar(255), password varchar(255), authority_id bigint, primary key (id)) engine=InnoDB
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table user drop index UK_32852vmffyhhg5ov56amkcx7s
Hibernate: alter table user drop index UK_32852vmffyhhg5ov56amkcx7s
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table user add constraint UK_32852vmffyhhg5ov56amkcx7s unique (number)
Hibernate: alter table user add constraint UK_32852vmffyhhg5ov56amkcx7s unique (number)
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table course add constraint FKsybhlxoejr4j3teomm5u2bx1n foreign key (teacher_id) references teacher (id)
Hibernate: alter table course add constraint FKsybhlxoejr4j3teomm5u2bx1n foreign key (teacher_id) references teacher (id)
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table course_detail add constraint FKonm6feulb9dmgri1ywpvid8l2 foreign key (course_id) references course (id)
Hibernate: alter table course_detail add constraint FKonm6feulb9dmgri1ywpvid8l2 foreign key (course_id) references course (id)
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table course_detail add constraint FKdh9dl501ehcfblljjpmx28sya foreign key (student_id) references student (id)
Hibernate: alter table course_detail add constraint FKdh9dl501ehcfblljjpmx28sya foreign key (student_id) references student (id)
2018-12-24 00:53:22 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table experiment add constraint FK68ee92hji28v7m63lcbe49mxm foreign key (course_id) references course (id)
Hibernate: alter table experiment add constraint FK68ee92hji28v7m63lcbe49mxm foreign key (course_id) references course (id)
2018-12-24 00:53:23 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table experiment_detail add constraint FKtg40ugg4gkd1raph37jfjpko9 foreign key (experiment_id) references experiment (id)
Hibernate: alter table experiment_detail add constraint FKtg40ugg4gkd1raph37jfjpko9 foreign key (experiment_id) references experiment (id)
2018-12-24 00:53:23 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table experiment_detail add constraint FKqxlpw83x0k6mpn4401oiqosil foreign key (student_id) references student (id)
Hibernate: alter table experiment_detail add constraint FKqxlpw83x0k6mpn4401oiqosil foreign key (student_id) references student (id)
2018-12-24 00:53:23 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table homework add constraint FKaippn77cl5hhh944no78eone8 foreign key (course_id) references course (id)
Hibernate: alter table homework add constraint FKaippn77cl5hhh944no78eone8 foreign key (course_id) references course (id)
2018-12-24 00:53:23 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table homework_detail add constraint FK6w6svv21e1qib443omjcn6u4w foreign key (homework_id) references homework (id)
Hibernate: alter table homework_detail add constraint FK6w6svv21e1qib443omjcn6u4w foreign key (homework_id) references homework (id)
2018-12-24 00:53:23 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table homework_detail add constraint FKjqmtghar9welw7yyoan5n1516 foreign key (student_id) references student (id)
Hibernate: alter table homework_detail add constraint FKjqmtghar9welw7yyoan5n1516 foreign key (student_id) references student (id)
2018-12-24 00:53:23 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table student add constraint FKqytew32213tbnj8u0er377k3q foreign key (id) references user (id)
Hibernate: alter table student add constraint FKqytew32213tbnj8u0er377k3q foreign key (id) references user (id)
2018-12-24 00:53:23 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table teacher add constraint FKlicv62vmu1ydw117bbxqhkof1 foreign key (id) references user (id)
Hibernate: alter table teacher add constraint FKlicv62vmu1ydw117bbxqhkof1 foreign key (id) references user (id)
2018-12-24 00:53:23 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - alter table user add constraint FKokrgxdbtf7tirfx1d1qtg9n24 foreign key (authority_id) references authority (id)
Hibernate: alter table user add constraint FKokrgxdbtf7tirfx1d1qtg9n24 foreign key (authority_id) references authority (id)
2018-12-24 00:53:24 WARN org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration$JpaWebConfiguration$JpaWebMvcConfiguration.openEntityManagerInViewInterceptor[234] - spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2018-12-24 00:53:25 INFO org.springframework.boot.StartupInfoLogger.logStarted[59] - Started AddUserTest in 6.043 seconds (JVM running for 6.775)
2018-12-24 00:53:25 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - insert into user (authority_id, insert_time, name, number, password) values (?, ?, ?, ?, ?)
Hibernate: insert into user (authority_id, insert_time, name, number, password) values (?, ?, ?, ?, ?)
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[53] - binding parameter [1] as [BIGINT] - [null]
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[53] - binding parameter [2] as [TIMESTAMP] - [null]
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[65] - binding parameter [3] as [VARCHAR] - [1]
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[53] - binding parameter [4] as [VARCHAR] - [null]
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[53] - binding parameter [5] as [VARCHAR] - [null]
2018-12-24 00:53:25 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - insert into student (clazz, id) values (?, ?)
Hibernate: insert into student (clazz, id) values (?, ?)
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[65] - binding parameter [1] as [VARCHAR] - [1]
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[65] - binding parameter [2] as [BIGINT] - [1]
2018-12-24 00:53:25 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - insert into user (authority_id, insert_time, name, number, password) values (?, ?, ?, ?, ?)
Hibernate: insert into user (authority_id, insert_time, name, number, password) values (?, ?, ?, ?, ?)
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[53] - binding parameter [1] as [BIGINT] - [null]
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[53] - binding parameter [2] as [TIMESTAMP] - [null]
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[65] - binding parameter [3] as [VARCHAR] - [2]
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[53] - binding parameter [4] as [VARCHAR] - [null]
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[53] - binding parameter [5] as [VARCHAR] - [null]
2018-12-24 00:53:25 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement[94] - insert into student (clazz, id) values (?, ?)
Hibernate: insert into student (clazz, id) values (?, ?)
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[65] - binding parameter [1] as [VARCHAR] - [2]
2018-12-24 00:53:25 TRACE org.hibernate.type.descriptor.sql.BasicBinder.bind[65] - binding parameter [2] as [BIGINT] - [2]
2018-12-24 00:53:25 DEBUG com.se.courses.service.TestService.addStudentTest[39] - com.se.courses.service.TestService.addStudentTest

Related

How to remove doctrine migrations permanently?

How do I perform a migration and have just my existing Entities visualised?
The problem is:
I removed all versions files from migrations
I dropped schema public
I created new schema public
I made a make:migration
I made a doctrine:migrations:migrate
And old data appeared - I mean tables which don't have its entities and were dropped long time ago.
Schema is created, but with reduntant and unnecessary data.
How to totally reset migrations? Is there any cache which save dropped tables (not considering versions of migrations)?
Here is my latest migrations file - I want to make a migration and I don't have such tables as for instance user_to_offert_user.
Don't give me manual solutions, please - I know that I can modify it before migration, but it seems pointless to me doing that every time when I will change something.
If something is not clear, ask me please. I tried to specify problem as much as I can.
Additional info:
What I am exactly doing:
I removed all versions from migrations.
I truncated all data
I dropped my public schema
I created my new public schema
I run bin/console make:migration
I run bin/console doctrine:migrations:migrate
And here is my huge problem:
I have this kind of entities:
And when I run migrate all of this tables shows:
As as you can see, there are too many tables. Some of them I dropped a long time ago and they still appears in every migration.
I don't know how to get by and I am sooo desperate, because I have been spending on this problem 3th day.
I have my database on Heroku - I use Postgres and when I dropped all tablesthey doesn't show in heroku:
Here is code of this migration:
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210403092535 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SEQUENCE requirement_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE SEQUENCE skill_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE SEQUENCE "user_id_seq" INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE SEQUENCE user_to_offert_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE login_history (id SERIAL NOT NULL, user_id_id INT NOT NULL, login_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, login_status BOOLEAN NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_37976E369D86650F ON login_history (user_id_id)');
$this->addSql('CREATE TABLE offert (id SERIAL NOT NULL, event_firm_id INT NOT NULL, event_name VARCHAR(100) NOT NULL, date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, description VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_44229184CF8D1CF ON offert (event_firm_id)');
$this->addSql('CREATE TABLE requirement (id INT NOT NULL, requirement VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE TABLE requirement_user (requirement_id INT NOT NULL, user_id INT NOT NULL, PRIMARY KEY(requirement_id, user_id))');
$this->addSql('CREATE INDEX IDX_598D28707B576F77 ON requirement_user (requirement_id)');
$this->addSql('CREATE INDEX IDX_598D2870A76ED395 ON requirement_user (user_id)');
$this->addSql('CREATE TABLE skill (id INT NOT NULL, skill VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE TABLE skill_user (skill_id INT NOT NULL, user_id INT NOT NULL, PRIMARY KEY(skill_id, user_id))');
$this->addSql('CREATE INDEX IDX_CAD24AFB5585C142 ON skill_user (skill_id)');
$this->addSql('CREATE INDEX IDX_CAD24AFBA76ED395 ON skill_user (user_id)');
$this->addSql('CREATE TABLE "user" (id INT NOT NULL, user_details_id_id INT NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, account_type VARCHAR(100) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D64912595E8 ON "user" (user_details_id_id)');
$this->addSql('CREATE TABLE user_details (id SERIAL NOT NULL, name VARCHAR(255) NOT NULL, surname VARCHAR(255) NOT NULL, description VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE TABLE user_to_offert (id INT NOT NULL, hired BOOLEAN NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE TABLE user_to_offert_offert (user_to_offert_id INT NOT NULL, offert_id INT NOT NULL, PRIMARY KEY(user_to_offert_id, offert_id))');
$this->addSql('CREATE INDEX IDX_4AA1878F8C2827E4 ON user_to_offert_offert (user_to_offert_id)');
$this->addSql('CREATE INDEX IDX_4AA1878F3D478C97 ON user_to_offert_offert (offert_id)');
$this->addSql('CREATE TABLE user_to_offert_user (user_to_offert_id INT NOT NULL, user_id INT NOT NULL, PRIMARY KEY(user_to_offert_id, user_id))');
$this->addSql('CREATE INDEX IDX_7BE6CF748C2827E4 ON user_to_offert_user (user_to_offert_id)');
$this->addSql('CREATE INDEX IDX_7BE6CF74A76ED395 ON user_to_offert_user (user_id)');
$this->addSql('ALTER TABLE login_history ADD CONSTRAINT FK_37976E369D86650F FOREIGN KEY (user_id_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE offert ADD CONSTRAINT FK_44229184CF8D1CF FOREIGN KEY (event_firm_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE requirement_user ADD CONSTRAINT FK_598D28707B576F77 FOREIGN KEY (requirement_id) REFERENCES requirement (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE requirement_user ADD CONSTRAINT FK_598D2870A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE skill_user ADD CONSTRAINT FK_CAD24AFB5585C142 FOREIGN KEY (skill_id) REFERENCES skill (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE skill_user ADD CONSTRAINT FK_CAD24AFBA76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE "user" ADD CONSTRAINT FK_8D93D64912595E8 FOREIGN KEY (user_details_id_id) REFERENCES user_details (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE user_to_offert_offert ADD CONSTRAINT FK_4AA1878F8C2827E4 FOREIGN KEY (user_to_offert_id) REFERENCES user_to_offert (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE user_to_offert_offert ADD CONSTRAINT FK_4AA1878F3D478C97 FOREIGN KEY (offert_id) REFERENCES offert (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE user_to_offert_user ADD CONSTRAINT FK_7BE6CF748C2827E4 FOREIGN KEY (user_to_offert_id) REFERENCES user_to_offert (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE user_to_offert_user ADD CONSTRAINT FK_7BE6CF74A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE user_to_offert_offert DROP CONSTRAINT FK_4AA1878F3D478C97');
$this->addSql('ALTER TABLE requirement_user DROP CONSTRAINT FK_598D28707B576F77');
$this->addSql('ALTER TABLE skill_user DROP CONSTRAINT FK_CAD24AFB5585C142');
$this->addSql('ALTER TABLE login_history DROP CONSTRAINT FK_37976E369D86650F');
$this->addSql('ALTER TABLE offert DROP CONSTRAINT FK_44229184CF8D1CF');
$this->addSql('ALTER TABLE requirement_user DROP CONSTRAINT FK_598D2870A76ED395');
$this->addSql('ALTER TABLE skill_user DROP CONSTRAINT FK_CAD24AFBA76ED395');
$this->addSql('ALTER TABLE user_to_offert_user DROP CONSTRAINT FK_7BE6CF74A76ED395');
$this->addSql('ALTER TABLE "user" DROP CONSTRAINT FK_8D93D64912595E8');
$this->addSql('ALTER TABLE user_to_offert_offert DROP CONSTRAINT FK_4AA1878F8C2827E4');
$this->addSql('ALTER TABLE user_to_offert_user DROP CONSTRAINT FK_7BE6CF748C2827E4');
$this->addSql('DROP SEQUENCE requirement_id_seq CASCADE');
$this->addSql('DROP SEQUENCE skill_id_seq CASCADE');
$this->addSql('DROP SEQUENCE "user_id_seq" CASCADE');
$this->addSql('DROP SEQUENCE user_to_offert_id_seq CASCADE');
$this->addSql('DROP TABLE login_history');
$this->addSql('DROP TABLE offert');
$this->addSql('DROP TABLE requirement');
$this->addSql('DROP TABLE requirement_user');
$this->addSql('DROP TABLE skill');
$this->addSql('DROP TABLE skill_user');
$this->addSql('DROP TABLE "user"');
$this->addSql('DROP TABLE user_details');
$this->addSql('DROP TABLE user_to_offert');
$this->addSql('DROP TABLE user_to_offert_offert');
$this->addSql('DROP TABLE user_to_offert_user');
}
}
Doctrine MigrationsBundle has a table where it stores the information which migrations previously ran. If you reset your migrations you will also have to reset that table. You can use the rollup command for this:
php bin/console doctrine:migrations:rollup
If this does not work you might want to change the table manually, e.g. in a migration, but you should probably avoid this if possible. By default the table is called doctrine_migration_versions, but you can use the debug:config command to check if your configuration is different:
php bin/console debug:config doctrine_migrations storage
Here is how it should look by default:
doctrine_migrations:
# ...
storage:
# Default (SQL table) metadata storage configuration
table_storage:
table_name: 'doctrine_migration_versions'
version_column_name: 'version'
version_column_length: 1024
executed_at_column_name: 'executed_at'

postgresql: constaint to have unique values across 2 tables

I have 2 tables:
CREATE TABLE public."user"
(
id integer NOT NULL DEFAULT nextval('user_id_seq'::regclass),
username character varying(256) COLLATE pg_catalog."default",
CONSTRAINT user_pkey PRIMARY KEY (id)
)
CREATE TABLE public.user_tenant
(
user_id integer NOT NULL,
tenant_id integer NOT NULL,
CONSTRAINT user_fk FOREIGN KEY (user_id)
REFERENCES public."user" (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE CASCADE
)
I need to have unique values (user.username, user_tenant.tenant_id). How can I declare such a constraint?
I would make the username unique, just like the tenant that is in another table. When that is done, you can put a primary key on the user_id and tenant_id:
CREATE TABLE public."user"
(
id integer NOT NULL DEFAULT nextval('user_id_seq'::regclass),
username character varying(256) COLLATE pg_catalog."default" unique,
CONSTRAINT user_pkey PRIMARY KEY (id)
);
CREATE TABLE public.user_tenant
(
user_id integer NOT NULL,
tenant_id integer NOT NULL,
CONSTRAINT user_fk FOREIGN KEY (user_id)
REFERENCES public."user" (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE CASCADE,
CONSTRAINT user_tenant_pk PRIMARY KEY (user_id, tenant_id)
);
By the way, don't use reserved names like "user" for table names.
You can create a function which can check for uniqueness across multiple tables (example here: Postgres unique combination constraint across tables) but it looks like you may need to the structure of your tables or follow Frank Heikens' answer.
EDIT:
CREATE TABLE public."user"
(
id SERIAL,
username character varying(256) COLLATE pg_catalog."default",
PRIMARY KEY (id)
);
CREATE TABLE public.user_tenant
(
user_id integer NOT NULL,
tenant_id integer NOT NULL,
CONSTRAINT user_fk FOREIGN KEY (user_id)
REFERENCES public."user" (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE CASCADE
);
CREATE OR REPLACE FUNCTION public.check_user_tenant(user_id integer, tenant_id integer)
RETURNS boolean AS
$$
DECLARE b_result boolean;
BEGIN
SELECT (COUNT(*) = 0) INTO b_result
FROM public.user u
JOIN public.user_tenant ut ON ut.user_id IN (SELECT id
FROM public.user i_u
WHERE i_u.username = u.username)
WHERE u.id = $1 AND ut.tenant_id = $2;
RETURN b_result;
END
$$ LANGUAGE 'plpgsql';
ALTER TABLE public.user_tenant
ADD CONSTRAINT check_filename CHECK
(public.check_user_tenant(user_id, tenant_id));
-- Testing:
insert into public."user" (username) VALUES ('foo');
insert into public.user_tenant (user_id, tenant_id) VALUES (1,3);
insert into public."user" (username) VALUES ('foo');
-- Violates constraint:
insert into public.user_tenant (user_id, tenant_id) VALUES (2,3);

How to stop Go gorm from forcing a not null constraint on my self referencing foreign key in Postgres

I need to create a table that references itself in gorm, but cannot figure out why it is forcing a not null constraint on me. I am totally stumped. How can I get around this? I am using the AutoMigrate feature provided by gorm to create the table. The instant I remove the foreign key constraints the not null constraint goes away, but that's not what I want.
Edited: I am using the package "gorm.io/gorm" specifically, not the one on Github. This is also the only table giving me issues, any other tables which have foreign keys that reference other tables works as expected.
Go with foreign key
type User struct {
ID *int `gorm:"primaryKey; type:serial"`
Username string `gorm:"type: varchar(32) not null unique"`
Password string `gorm:"type: varchar(128) not null"`
ReferredBy *int
Referrer *User `gorm:"foreignKey:ReferredBy;constraint:OnUpdate:CASCADE,ONDELETE:SET NULL"`
}
Resulting SQL with foreign key according to pgAdmin
-- Table: public.users
-- DROP TABLE public.users;
CREATE TABLE public.users
(
id integer NOT NULL DEFAULT nextval('users_id_seq'::regclass),
username character varying(32) COLLATE pg_catalog."default" NOT NULL,
password character varying(128) COLLATE pg_catalog."default" NOT NULL,
referred_by integer NOT NULL DEFAULT nextval('users_referred_by_seq'::regclass),
CONSTRAINT users_pkey PRIMARY KEY (id),
CONSTRAINT users_username_key UNIQUE (username),
CONSTRAINT fk_users_referrer FOREIGN KEY (referred_by)
REFERENCES public.users (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE SET NULL
)
TABLESPACE pg_default;
ALTER TABLE public.users
OWNER to msmf;
Go without foreign key
// User Model. ReferredBy is self referencing Foreign Key
type User struct {
ID *int `gorm:"primaryKey; type:serial"`
Username string `gorm:"type: varchar(32) not null unique"`
Password string `gorm:"type: varchar(128) not null"`
ReferredBy *int
}
Resulting SQL without telling gorm there is a foreign key
-- Table: public.users
-- DROP TABLE public.users;
CREATE TABLE public.users
(
id integer NOT NULL DEFAULT nextval('users_id_seq'::regclass),
username character varying(32) COLLATE pg_catalog."default" NOT NULL,
password character varying(128) COLLATE pg_catalog."default" NOT NULL,
referred_by bigint,
CONSTRAINT users_pkey PRIMARY KEY (id),
CONSTRAINT users_username_key UNIQUE (username)
)
TABLESPACE pg_default;
ALTER TABLE public.users
OWNER to msmf;
Apparently type:serial tag does this. If you drop it the not null constraint won't be there either:
type User struct {
ID uint `gorm:"primarykey"`
Username string `gorm:"type: varchar(32) not null unique"`
Password string `gorm:"type: varchar(128) not null"`
ReferredBy *int
Referrer *User `gorm:"foreignKey:ReferredBy;constraint:OnUpdate:CASCADE,ONDELETE:SET NULL;"`
}
Reference github issue.

Find cause of error "constraint xxxx is not a foreign key constraint" in Postgresql

I have defined these tables:
CREATE TABLE "public".category (id BIGSERIAL NOT NULL, name varchar(255) NOT NULL, PRIMARY KEY (id));
CREATE UNIQUE INDEX category_name ON "public".category (name);
CREATE TABLE "public".clusters (id BIGSERIAL NOT NULL, name varchar(255) NOT NULL, PRIMARY KEY (id));
CREATE INDEX clusters_name ON "public".clusters (name);
CREATE TABLE "public".keywords (id BIGSERIAL NOT NULL, text varchar(255) NOT NULL, category_id int8 NOT NULL, top_results int4, cluster_id int8, month_requests int4, click_cost float8, PRIMARY KEY (id));
CREATE INDEX keywords_text ON "public".keywords (text);
ALTER TABLE "public".keywords ADD CONSTRAINT FKkeywords488682 FOREIGN KEY (cluster_id) REFERENCES "public".clusters (id);
ALTER TABLE "public".keywords ADD CONSTRAINT FKkeywords446526 FOREIGN KEY (category_id) REFERENCES "public".category (id) ON UPDATE CASCADE ON DELETE CASCADE;
added one record to category table:
INSERT INTO "public".category(id, name) VALUES (1, 'Test');
And now when I try to add record to keyword table
insert into "public"."keywords" ( "category_id", "text") values ( 1, 'testkey')
I got error:
ERROR: constraint 16959 is not a foreign key constraint
When I do
select * FROM pg_constraint;
I can't see constraint with this id. I can't understand what is the cause of this problem.

Postgresql Creating a Schema with tables in it

Here's my script.
CREATE SCHEMA testSchema
create table REF_PRODUCT (
id int8 not null,
created_date timestamp,
CODE varchar(255),
DESCRIPTION varchar(255),
LOCATION varchar(255),
MANUFACTURER varchar(255),
NAME varchar(255),
COST numeric(19, 2),
DEALERS_PRICE numeric(19, 2),
SUGGESTED_RETAIL_PRICE numeric(19, 2),
STOCK int4,
PRODUCT_TYPE varchar(255),
picture_id int8,
primary key (id)
)
create table TXN_LINE_ITEM (
id int8 not null,
created_date timestamp,
cancelled boolean,
cost numeric(19, 2),
dp numeric(19, 2),
itemCode varchar(255),
itemName varchar(255),
priceType varchar(255),
qty int4,
refunded boolean,
srp numeric(19, 2),
total numeric(19, 2),
transactionRecord_id int8,
primary key (id)
)
create table TXN_PRODUCT_PICTURE (
id int8 not null,
created_date timestamp,
picture bytea,
primary key (id)
)
create table TXN_TRANSACTION_RECORD (
id int8 not null,
created_date timestamp,
total numeric(19, 2),
transaction_date timestamp,
TRANSACTION_NUMBER varchar(255),
VALID boolean,
primary key (id)
)
alter table REF_PRODUCT
add constraint FK_sjugahpelk16qj5h3w8dli42l
foreign key (picture_id)
references TXN_PRODUCT_PICTURE;
alter table TXN_LINE_ITEM
add constraint FK_o5mslaahpil9d3g9rl2s22rpm
foreign key (transactionRecord_id)
references TXN_TRANSACTION_RECORD;
create table SEQ_ENTITY_ID (
sequence_name varchar(255),
sequence_next_hi_value int4
);
The problem is the fk relationships specified in alter table statement is not being created
Because of this error.
[WARNING ] CREATE SCHEMA testSchema
create table REF_PRODUCT (
id int8 not null,
created_date timestamp,
CODE varchar(255),
DESCRIPTION varchar(255),
LOCATION varchar(255),
MANUFACTURER varchar(255),
NAME varchar(255),
COST numeric(19, 2),
DEALERS_PRICE numeric(19, 2),
SUGGESTED_RETAIL_PRICE numeric(19, 2),
STOCK int4,
PRODUCT_TYPE varchar(255),
picture_id int8,
primary key (id)
)
create table TXN_LINE_ITEM (
id int8 not null,
created_date timestamp,
cancelled boolean,
cost numeric(19, 2),
dp numeric(19, 2),
itemCode varchar(255),
itemName varchar(255),
priceType varchar(255),
qty int4,
refunded boolean,
srp numeric(19, 2),
total numeric(19, 2),
transactionRecord_id int8,
primary key (id)
)
create table TXN_PRODUCT_PICTURE (
id int8 not null,
created_date timestamp,
picture bytea,
primary key (id)
)
create table TXN_TRANSACTION_RECORD (
id int8 not null,
created_date timestamp,
total numeric(19, 2),
transaction_date timestamp,
TRANSACTION_NUMBER varchar(255),
VALID boolean,
primary key (id)
)
alter table REF_PRODUCT
add constraint FK_sjugahpelk16qj5h3w8dli42l
foreign key (picture_id)
references TXN_PRODUCT_PICTURE
ERROR: syntax error at or near "alter"
LINE 53: alter table REF_PRODUCT
^
[WARNING ] alter table TXN_LINE_ITEM
add constraint FK_o5mslaahpil9d3g9rl2s22rpm
foreign key (transactionRecord_id)
references TXN_TRANSACTION_RECORD
ERROR: relation "txn_line_item" does not exist
[WARNING ] create table SEQ_ENTITY_ID (
sequence_name varchar(255),
sequence_next_hi_value int4
)
ERROR: relation "seq_entity_id" already exists
NOTE THAT: There's no existing tables on the said Schema I have made. What have I missed?
I think you have two ways of doing this.
The first one: don't run one create schema statement, but run individual statements for each part (this is what I prefer). You can still do that as a single transaction:
begin transaction;
CREATE SCHEMA testSchema; -- only create the namespace
-- make the new schema the default schema
-- so the the tables do not need to be full qualified
set search_path = testschema;
create table REF_PRODUCT (
...
);
create table TXN_LINE_ITEM (
...
);
create table TXN_PRODUCT_PICTURE (
...
);
create table TXN_TRANSACTION_RECORD (
...
);
alter table REF_PRODUCT
add constraint fk_product_picture
foreign key (picture_id)
references TXN_PRODUCT_PICTURE;
alter table TXN_LINE_ITEM
add constraint FK_line_item_trans_record
foreign key (transactionRecord_id)
references TXN_TRANSACTION_RECORD;
create table SEQ_ENTITY_ID (
sequence_name varchar(255),
sequence_next_hi_value int4
);
commit;
The other option is to move the foreign keys into the table definition, but then you need to re-order your create table statements:
CREATE SCHEMA testSchema
create table TXN_PRODUCT_PICTURE (
...
)
create table TXN_TRANSACTION_RECORD (
...
)
create table REF_PRODUCT (
...,
constraint fk_product_picture
foreign key (picture_id)
references TXN_PRODUCT_PICTURE
)
create table TXN_LINE_ITEM (
...
constraint FK_line_item_trans_record
foreign key (transactionRecord_id)
references TXN_TRANSACTION_RECORD
)
create table SEQ_ENTITY_ID (
sequence_name varchar(255),
sequence_next_hi_value int4
)
;
Unrelated, but:
What is this SEQ_ENTITY_ID table for? Why don't you use native Postgres sequences. They will be much faster, more scalable and more robust than anything you can do in your application to generate unique numbers