Morgan Allen Morgan Allen - 7 months ago 71
Python Question

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

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?

Answer

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