Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用SQLAlchemy在Postgres中插入nil_uuid?_Python_Postgresql_Sqlalchemy - Fatal编程技术网

Python 如何使用SQLAlchemy在Postgres中插入nil_uuid?

Python 如何使用SQLAlchemy在Postgres中插入nil_uuid?,python,postgresql,sqlalchemy,Python,Postgresql,Sqlalchemy,使用Python和SQLAlchemy,是否可以在链接到主键(两者都存储为UUID)的Postgresql外键列中插入None/NIL_UUID/NULL值 None返回列None不存在: 更多详细信息 我使用SQLAlchemy经典映射(SQLAlchemy Core),我的表定义如下: tb_person = Table( "tb_person", metadata, Column( "pk_person",

使用Python和SQLAlchemy,是否可以在链接到主键(两者都存储为UUID)的Postgresql外键列中插入None/NIL_UUID/NULL值

  • None
    返回
    列None不存在
更多详细信息

我使用SQLAlchemy经典映射(SQLAlchemy Core),我的表定义如下:

tb_person = Table(
    "tb_person",
    metadata,
    Column(
        "pk_person",
        UUID(as_uuid=True), 
        default=uuid.uuid4,
        unique=True,
        nullable=False
    ),
    Column("first_name", String(255)),
    Column("last_name", String(255)),
    Column(
        "fk_person_parent", UUID(as_uuid=True),
        ForeignKey("tb_person.pk_person"),
        nullable=True
    )
)
    client_mapper = mapper(
        domain.model.Person,
        tb_person,
        properties={
            "child": relationship(domain.model.Person),
        },
    )
映射器的定义如下:

tb_person = Table(
    "tb_person",
    metadata,
    Column(
        "pk_person",
        UUID(as_uuid=True), 
        default=uuid.uuid4,
        unique=True,
        nullable=False
    ),
    Column("first_name", String(255)),
    Column("last_name", String(255)),
    Column(
        "fk_person_parent", UUID(as_uuid=True),
        ForeignKey("tb_person.pk_person"),
        nullable=True
    )
)
    client_mapper = mapper(
        domain.model.Person,
        tb_person,
        properties={
            "child": relationship(domain.model.Person),
        },
    )

当在pk_person字段中插入数据库中已经存在的UUID时,单元测试可以很好地工作。

我使用的是原始SQL,正如@Adriankaver所建议的,最好使用params-它解决了问题

# In the context of this unit test,
# we have to handle UUID generation here
uuid_1 = uuid.uuid4()
uuid_2 = uuid.uuid4()
uuid_3 = uuid.uuid4()

session.execute(
"""
INSERT INTO tb_person
(pk_person, first_name, last_name, fk_parent_person) 
VALUES
(:uuid_1, 'John', 'Doe', :uuid_none),
(:uuid_2, 'Jean', 'Dupont', :uuid_none)
(:uuid_3, 'Baby', 'Doe', :uuid_1)
""",
 {"uuid_1": uuid_1, "uuid_2", "uuid_3": uuid_3, ":uuid_none": None}
)

它在查询中有效地转换为NULL。

可以在
外键
字段中插入
NULL
值。您的
NIL\u UUID
值不是
NULL
,尽管如此,它需要在引用表的
主键中输入一个条目。@AdrianKlaver谢谢,您用我所用的较短术语提出了我的问题。当None和NIL_UUID都不起作用时,如何在UUID字段中插入NULL?您实际上并没有传入
None
,而是传入“None”,它被视为列名。如果
fk\u person\u parent
允许
NULL
,则不要在
插入中包含该字段,并且将为该值设置
NULL
。您真的应该使用参数来传递值,然后
None
将正确地适应
INSERT
上的
NULL
-它在查询中被插入为None(而不是“None”),但谢谢,我将使用参数重试。是的,但作为错误,
psycopg2.errors.UndefinedColumn:column“None”不存在
,显示由于它位于字符串中,所以被双引号引为标识符(本例中为列)。这也是为什么它会被关起来。