获取消息"尝试定义第二个主键"。运行@DataJpaTest时

时间:2017-06-30 15:38:55

标签: spring-boot junit h2 flyway

我不确切知道原因,但我从H2数据库中的Flyway evolution获得了这条消息。它是 @DataJpaTest 春季启动测试。

似乎某些方言的东西没有受到尊重。

尝试定义第二个主键; SQL语句:

@RunWith(SpringRunner.class)
@DataJpaTest
@FlywayTest

public class ParagraphStoreTest {

@Autowired
private TestEntityManager entityManager;

@Autowired
private ParagraphStore paragraphStore;

@Test
public void testExample() throws Exception {

    long scanId = 123;
    long userId = 1234;
    int minParagraphLength = 1;
    int pageNumber = 1;
    int pageSize = 50;

    Pageable pageable = new PageRequest(pageNumber, pageSize, Direction.ASC, "paragraphIndex");

    this.entityManager.persist(Paragraph.builder().scan(Scan.builder().id(scanId).urlId(userId).build())
            .paragraphIndex(1).wordCount(1));

    Page<Paragraph> paragraphs = this.paragraphStore.findAllParagraphStatsByScanIdLimit(scanId, userId,
            minParagraphLength, pageable);

    assertNotNull(paragraphs);
}

}

application.yml

异常

    rror starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-06-30 16:21:32 [main] ERROR o.s.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException: 

Migration V1__Init.sql failed
-----------------------------
SQL State  : 90017
Error Code : 90017
Message    : Attempt to define a second primary key; SQL statement:
CREATE TABLE users (
    id serial NOT NULL,
    user_name varchar(160) NOT NULL,
    user_pass varchar(160) NOT NULL,
    first_name varchar(160) NULL,
    last_name varchar(160) NULL,
    last_sign_in date NULL,
    preferences text NOT NULL DEFAULT '{}'::text,
    CONSTRAINT users_pkey PRIMARY KEY (user_name)
) [90017-148]
Location   : db/migration/V1__Init.sql (C:\Users\victo\Documents\dev\projects\readability-dev\bin\db\migration\V1__Init.sql)
Line       : 1
Statement  : CREATE TABLE users (
    id serial NOT NULL,
    user_name varchar(160) NOT NULL,
    user_pass varchar(160) NOT NULL,
    first_name varchar(160) NULL,
    last_name varchar(160) NULL,
    last_sign_in date NULL,
    preferences text NOT NULL DEFAULT '{}'::text,
    CONSTRAINT users_pkey PRIMARY KEY (user_name)
)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException: 

User.java

@Entity
@Table(name="users")
@AllArgsConstructor(access = AccessLevel.PROTECTED)

@NoArgsConstructor

@ToString(callSuper=true)
@Builder
public class User {

@Id
@Getter @Setter
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="users_id_seq")
@SequenceGenerator(name="users_id_seq", sequenceName="users_id_seq")
private Long id;

@Getter @Setter
private String socialId;

@Getter @Setter
private boolean IsEmailActivationRequired;

@Getter @Setter
private String firstName;

@Getter @Setter
private String lastName;

@Getter @Setter
private String userName;

@Getter @Setter
private String userPass;

@Email
@NotNull
@NotEmpty
@Getter @Setter
private String userEmail;

}

1 个答案:

答案 0 :(得分:0)

根据我的记忆,@ DataJpaTest不会引发spring的上下文,因此@Autowired必须存在问题

否则,您可以使用h2语法在基础中创建另一个脚本

flyway:
 locations: classpath:test/db/migration

使用sql server,我遇到了这个问题,因为没有在最后一个参数和查询之间使用逗号,而是遵循功能脚本

CREATE TABLE account.embossing_blacklist(
  id_product BIGINT NOT NULL,
  PRIMARY KEY (id_product)
);