dotnetcore / FreeSql

🦄 .NET aot orm, C# orm, VB.NET orm, Mysql orm, Postgresql orm, SqlServer orm, Oracle orm, Sqlite orm, Firebird orm, 达梦 orm, 人大金仓 orm, 神通 orm, 翰高 orm, 南大通用 orm, 虚谷 orm, 国产 orm, Clickhouse orm, QuestDB orm, MsAccess orm.
http://freesql.net
MIT License
3.99k stars 842 forks source link

postgres下插入只包含字符串的json会报错 #1770

Open gxrsprite opened 1 month ago

gxrsprite commented 1 month ago
using Newtonsoft.Json.Linq;

internal class Program
{
    static void Main(string[] args)
    {
        IFreeSql fsql = new FreeSql.FreeSqlBuilder()
            .UseConnectionString(FreeSql.DataType.PostgreSQL, @"Host=127.0.0.1;Port=5432;Username=postgres;Password=123; Database=freesqltest;")
            .UseMonitorCommand(cmd => Console.WriteLine($"Sql:{cmd.CommandText}"))
            .UseNoneCommandParameter(true)
            .UseAutoSyncStructure(true) 
            .Build();

        fsql.CodeFirst.SyncStructure<MyTestObj>();

        fsql.Insert(new MyTestObj() { Json = "aaabbb" }).ExecuteAffrows();
    }
}

class MyTestObj
{
    public JToken Json { get; set; }
}

包:

image

freesql生成的sql:INSERT INTO "MyTestObj"("Json") VALUES('aaabbb')

可以正确插入的sql应该为 INSERT INTO "MyTestObj"("Json") VALUES('"aaabbb"')

update同理

不过数值类型没问题

2881099 commented 1 month ago

https://github.com/dotnetcore/FreeSql/blob/1e089afe1f3ac7364aa8937b5da522804de2117f/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLAdo/PostgreSQLAdo.cs#L75

处理方式在这里,个人建议在使用当时上解决问题。

JToken 始终保存 Array 或 Json,基础类型可以选择 string 存储。

gxrsprite commented 1 month ago

https://github.com/dotnetcore/FreeSql/blob/1e089afe1f3ac7364aa8937b5da522804de2117f/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLAdo/PostgreSQLAdo.cs#L75

处理方式在这里,个人建议在使用当时上解决问题。

JToken 始终保存 Array 或 Json,基础类型可以选择 string 存储。

数值类型用起来没问题,只要针对string处理一下,JToken能存的数据就基本没问题了