在MariaDB中保存UUID以与蒸气一起使用

问题描述 投票:0回答:1

我想在Vapor中使用FluentMySQL保存数据,但我无法正确读取UUID。

如果我使用MariaDB提供的UUID()函数,一切都很好,但是如果我使用FluentMySQL,则UUID会混乱:(第一条记录:UUID()的用法,第二条记录:蒸气)

MariaDB [someDB]> select * from Poll;
+--------------------------------------+-------+---------+---------+--------+--------+
| id                                   | title | option1 | option2 | votes1 | votes2 |
+--------------------------------------+-------+---------+---------+--------+--------+
| 88a18a58-2fcd-11ea-9f62-e283e8014c79 | test  | bla     | bla     |      0 |      0 |
| 7??/.?E??*_P?v                     | bla   | option1 | option2 |      1 |      2 |
+--------------------------------------+-------+---------+---------+--------+--------+
2 rows in set (0.00 sec)

这是我的模特:

import Foundation
import FluentMySQL
import Vapor

struct Poll: Content, MySQLUUIDModel, Migration {
    typealias ID = UUID
    static let entity: String = "Poll"

    var id: UUID?
    var title: String
    var option1: String
    var option2: String
    var votes1: Int
    var votes2: Int
}

这是我的桌子:

MariaDB [someDB]> describe Poll;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | varchar(191) | YES  |     | NULL    |       |
| title   | varchar(191) | YES  |     | NULL    |       |
| option1 | varchar(191) | YES  |     | NULL    |       |
| option2 | varchar(191) | YES  |     | NULL    |       |
| votes1  | int(10)      | YES  |     | NULL    |       |
| votes2  | int(10)      | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

但是如果使用蒸气来获取数据,一切似乎都很好。我在做什么错?

通过curl输出:

curl http://locurl http://localhost:8080/polls/list
[{"option1":"bla","id":"38386131-3861-3538-2D32-6663642D3131","title":"test","option2":"bla","votes1":0,"votes2":0},{"option1":"option1","id":"373F3F2F-2E3F-453F-3F0E-2A5F503F7607","title":"bla","option2":"option2","votes1":1,"votes2":2}]

谢谢您的帮助。

swift mariadb fluent vapor
1个回答
0
投票

如果使用蒸汽本身创建UUID字段,它将创建一个数据类型为varbinary(16)而不是varchar(191)的字段。这与MariaDB / MySQL中UUID字段的默认格式一致。

我假设这是一个旧应用程序,其数据是在Vapor外部创建的?看起来您正在存储UUID值而不是基础二进制值的字符串表示形式。如果假定所有值都是可打印的,则在varchar字段中存储二进制值只会导致问题(如您所发现的)。但是,在同一列中混合使用字符串和二进制格式的值可能会引起问题。

© www.soinside.com 2019 - 2024. All rights reserved.