2016-04-29 84 views
0

我想在SQLAlchemy中創建一個表,其中電子郵件列是一個電子郵件地址數組。不過,我得到這個錯誤:AttributeError:「'模塊'對象沒有屬性'ARRAY'」

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

我的文檔http://docs.sqlalchemy.org/en/latest/core/type_basics.html#sqlalchemy.types.ARRAY

擡頭,但我想我不明白的正確實施。

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) 

任何建議如何使這項工作?

+0

什麼是您的sqlalchemy版本? – alpert

回答

1

我猜你想用它來存儲用戶的多個電子郵件。如果沒有ARRAY類型,傳統上可以通過創建另一個表(Email)來處理這個問題,該表具有一對多鏈接到Candidate表。

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) 
}) 
+0

謝謝,SUPER幫助! –

1

sqlalchemy.types.ARRAY是1.1.0版本中的新功能,該版本尚未發佈。除非你在開發SQLAlchemy版本,否則你將無法訪問該類型。

+0

啊好吧謝謝。短期內我能做些什麼嗎? –

+0

@MorganAllen:如果你使用的是Postgresql,['sqlalchemy.dialects.postgresql.ARRAY'](http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#sqlalchemy.dialects.postgresql.ARRAY )可能是一個合適的替代品。如果您不在Postgresql上,即使使用'sqlalchemy.types.ARRAY',SQLAlchemy似乎也不支持SQL數組。 – user2357112

+0

我目前使用SQLLite –

相關問題