首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >org.postgresql.util.PSQLException:错误:运算符不存在: character varying = bytea

org.postgresql.util.PSQLException:错误:运算符不存在: character varying = bytea
EN

Stack Overflow用户
提问于 2021-08-11 16:37:53
回答 1查看 401关注 0票数 0

我正在尝试将Excel文件保存到我的数据库中.我得到了这个错误:

代码语言:javascript
运行
复制
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = bytea
Hint: No operator matches the given name and argument types. You might need to add explicit type casts. 

这是我的控制器post方法:

代码语言:javascript
运行
复制
@PostMapping
public ResponseEntity<DocumentCreateResponse> addDocument(@RequestParam("file") MultipartFile file) throws IOException {
    Path copyLocation = Paths.get(uploadDir, Objects.requireNonNull(file.getOriginalFilename()));
    Files.copy(file.getInputStream(), copyLocation, StandardCopyOption.REPLACE_EXISTING);

    Document savedDocument = documentService.save(copyLocation.toAbsolutePath().toFile());
    if (savedDocument == null){
        return new ResponseEntity<>(new DocumentCreateResponse(null), null, HttpStatus.NOT_ACCEPTABLE);
    }
    return new ResponseEntity<>(new DocumentCreateResponse(savedDocument.getId()), null, HttpStatus.ACCEPTED);
}

这是document类:

代码语言:javascript
运行
复制
@Entity
public class DocumentEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name ="document_id")
    private Long id;

    @Column(name ="document_file")
    private File file;


    public DocumentEntity() {
    }

    public DocumentEntity(File file) {
        this.file = file;
    }
}

这是我的模式创建文件(我使用Flyway和PostgreSQL)

代码语言:javascript
运行
复制
create table if not exists document_entity
(
    document_id bigserial
    constraint document_entity_document_id
    not null primary key,
    document_file varchar not null
);

我应该在哪里添加更改,以便正确保存我的文件,而不会导致此错误?我已经阅读了文章并看到了这个错误,但是我不知道我应该在我的代码中修改什么。

代码语言:javascript
运行
复制
public class DocumentService implements DocumentServicePort {
    private final DocumentRepository documentRepository;

    public DocumentService(DocumentRepository documentRepository) {
        this.documentRepository = documentRepository;
    }

    @Override
    public Document save(File file) {
        Document document = new Document(file);
        if (document.hasValidExtension(document)) {
            documentRepository.saveDocument(document);
            return document;
        }
        return null;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-08-11 17:20:10

您已在数据库中将文件声明为varchar,而在应用程序中已将该字段声明为File。

您可以做一些事情,但是对于您提到的情况,请更改数据库中列的类型。它应该是'bytea`

在db上运行此命令(如果表为空会更好):

代码语言:javascript
运行
复制
ALTER TABLE document_entity ALTER COLUMN document_file TYPE bytea USING document_file::TEXT::BYTEA;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68745887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档