1

I'm trying to create a table in SQLAlchemy where the email column is an array of email addresses. However, I'm getting this error:

AttributeError: "'module' object has no attribute 'ARRAY'"

I looked up in the docs http://docs.sqlalchemy.org/en/latest/core/type_basics.html#sqlalchemy.types.ARRAY

but I guess I didn't understand the correct implementation.

import datetime
from sqlalchemy import schema, types
from sqlalchemy import orm


metadata = schema.MetaData()

def now():
    return datetime.datetime.now()

page_table = schema.Table('candidate', metadata,
    schema.Column('id', types.Integer, schema.Sequence('page_seq_id', optional=True), primary_key=True),
    schema.Column('email', types.ARRAY(String), nullable=False),
    schema.Column('first', types.Unicode(255), nullable=False),
    schema.Column('last', types.Unicode(255), nullable=False),
    schema.Column('company', types.Unicode(255), nullable=False),
    schema.Column('title', types.Unicode(255), nullable=False),
    schema.Column('linkedin', types.Unicode(255), nullable=False, unique=True),
)

class Candidate(object):
    pass

orm.mapper(Candidate, page_table)

Can any suggest how to make this work?

1
  • What is your sqlalchemy version? Commented Apr 29, 2016 at 17:11

2 Answers 2

2

sqlalchemy.types.ARRAY is new in 1.1.0, a version that has not yet been released. Unless you're on a development build of SQLAlchemy, you will not have access to that type.

Sign up to request clarification or add additional context in comments.

6 Comments

ahh ok thank you. Is there anything I can do in the short term?
@MorganAllen: If you're on Postgresql, sqlalchemy.dialects.postgresql.ARRAY may be an appropriate substitute. If you're not on Postgresql, SQLAlchemy doesn't seem to actually have support for SQL arrays, even with sqlalchemy.types.ARRAY.
I'm currently using SQLLite
@MorganAllen: I don't think SQLite has arrays.
I guess the answer would be to break out the email address's into its own table and create a foreign key?
|
1

I'm guessing you want to use this to store multiple emails for a user. Without an ARRAY type, this would traditionally be handled by creating another table -- Email -- with a one-to-many link to the Candidate table.

email_table = schema.Table('email', metadata,
    schema.Column('id', types.Integer, schema.Sequence('email_seq_id', optional=True), primary_key=True),
    schema.Column('email', types.Unicode(255), nullable=False),
    schema.Column('candidate_id', types.Integer, ForeignKey("candidate.id"), nullable=False),
)

class Email(object):
    pass

mapper(Email, email_table)

mapper(Candidate, page_table, properties={
    'emails' : relationship(Email, backref='candidate', order_by=email_table.c.id)
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.