需要将原始元组保存到数据库中。
值:
conn.adapter = {Plug.Adapters.Cowboy.Conn, {:http_req, #Port<0.14529>, ranch_tcp, :keepalive, #PID<0.528.0>, "GET", :"HTTP/1.1", {{127, 0, 0, 1}, 2866}, "localhost", :undefined, 4000, "/click", :undefined, "campaign_id=44", undefined, [], [{"host", "localhost:4000"}, {"user-agent", "Mozilla/5.0 X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0"}, "accept", text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}, {"accept-anguage", "en-US,en;q=0.5"}, {"accept-encoding", "gzip, deflate"}, "cookie", _ga=GA1.1.1071807366.1485427699"}, {"connection", "keep-alive"}, {"upgrade-nsecure-requests", "1"}], [{"connection", ["keep-alive"]}], :undefined, [], waiting, "", :undefined, false, :waiting, [], "", :undefined}}
enter code here
试着,
field :field5, {:array, :string}
P1.Repo.insert!(%P1.TestTable{field1: "Header Track",field2: ip, field3: conn.host, field5: conn.adapter})
但它会抛出错误,
TestTable.field5` in `insert` does not match type {:array, :string}
或至少,需要将这些数据转换为字符串并保存在db
中答案 0 :(得分:2)
该元组不是字符串数组。如果您只是希望以后能够看到元组的内容并且不需要具有这些值的实际元组,则可以使用inspect/1
将元组转换为字符串表示并将其保存为字符串:
# Schema
field :field5, :string
# Controller
Repo.insert(%TestTable{..., field5: inspect(conn.adapter)})
如果您确实需要将元组作为元组,则可以使用:erlang.term_to_binary/1
进行序列化并使用:erlang.binary_to_term/1
进行反序列化。 (请务必阅读binary_to_term
文档中的警告部分。)
# Schema
field :field5, :binary
# Controller
Repo.insert(%TestTable{..., field5: :erlang.term_to_binary(conn.adapter)})
# Later
test = Repo.get(TestTable, 123)
adapter = :erlang.binary_to_term(test.field5, [:safe])