inserting into array column sqlalchmey 1.3.17

4 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Mariana Salgueiro

ungelesen,
18.06.2020, 15:35:3818.06.20
an sqlalchemy
Hello!

I have two Flask projects that connect to postgresql databases and i encountered an error while trying to insert a value into an array.

My first project was made back in 2018 and we were using SQLAlchemy 1.1.12 back then.
We had this database that had this phone column that was defined as: phone varchar(14) [] and we could insert into the table with no problems at all. 

My code is something like: 
table =Table(phone=form.phone.data) in the views.py file; 
Table being defined in my models.py file as: telefone =db.Column(ARRAY(db.CHAR(length=12)))

This project im working on now, we are using SQLAlchemy 1.3.17 but while trying to do the same thing as in the 2018's project, the database's insert its something like:


There's not much diference between the projects' code and im wondering if something is not working on SQLAlchemy 1.3.17?
Thank you in advance!

Mike Bayer

ungelesen,
18.06.2020, 16:47:1118.06.20
an noreply-spamdigest via sqlalchemy
clearly some way that you are passing your strings is causing the string itself to be interpreted as an array, rather than the array of strings.

we debug these issues using MCVEs.  Below is an MCVE. Verify it works for you, then see if you can tell what's different about your application from this test script.

from sqlalchemy import ARRAY
from sqlalchemy import CHAR
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session

Base = declarative_base()


class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)
    telefone =Column(ARRAY(CHAR(length=14)))


e = create_engine("postgresql://scott:tiger@localhost/test", echo='debug')
Base.metadata.drop_all(e)
Base.metadata.create_all(e)

s = Session(e)

s.add(A(telefone=["(21)11111-1111", "(21)22222-2222"]))
s.commit()


phones = s.query(A.telefone).first().telefone

assert phones == ["(21)11111-1111", "(21)22222-2222"]









--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.




Attachments:
  • Auto Generated Inline Image 1
  • Auto Generated Inline Image 2

Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten