Re: [sqlalchemy] inserting into array column sqlalchmey 1.3.17

2020-06-18 Thread Mike Bayer


On Thu, Jun 18, 2020, at 3:35 PM, Mariana Salgueiro wrote:
> 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!

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)1-", "(21)2-"]))
s.commit()


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

assert phones == ["(21)1-", "(21)2-"]







> 
> 

> --
>  SQLAlchemy - 
>  The Python SQL Toolkit and Object Relational Mapper
> 
> http://www.sqlalchemy.org/
> 
>  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+unsubscr...@googlegroups.com.
>  To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/1bfaefef-b886-48e8-81fd-38191fa350beo%40googlegroups.com
>  
> .
> 
> 
> 
> 
> *Attachments:*
>  * Auto Generated Inline Image 1
>  * Auto Generated Inline Image 2

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/9cdb71df-f3f0-4c9b-ad85-440486af088d%40www.fastmail.com.


[sqlalchemy] inserting into array column sqlalchmey 1.3.17

2020-06-18 Thread Mariana Salgueiro
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!

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/1bfaefef-b886-48e8-81fd-38191fa350beo%40googlegroups.com.