LINUX.ORG.RU

[python][sqlalchemy] Не видит foreign key

 ,


0

0

Не можнт сделать join, выдает ошибку:

for a in \
    base.Session.query(DeviceType.label).\
    join(devices):
        print a
sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships between 'device_types' and 'devices'
Таблицы такие:
create table device_types ( 
  id integer not null auto_increment,
  name varchar(45) not null,
  python_object varchar(200) not null,
  container enum("configs", "units") not null,
  label varchar(45) not null,
  primary key(id)
) engine=InnoDB;

create table devices (
  id integer not null auto_increment,
  device_type integer not null,
  description text, primary key(id),
  foreign key(device_type) references device_types(id)
    on delete restrict 
    on update cascade
) Engine=InnoDB;

Замаппеные классы:


class devices(base.Base):
    __tablename__ = "devices"
    id = Column (MSInteger, primary_key = True)
    device_type = (MSInteger, ForeignKey(base.MetaData.tables["device_types"].c.id))
    description = Column (MSText, nullable = True)
    
    def __init__ (self, id,  description = ''):
        self.id = id
        self.description = description
        
    def __repr__ (self):
        return "<Devices ('%d', '%s', '%s')>" % (self.id, self.device_type, self.description)

class DeviceType (base.Base):
    __tablename__ = "device_types"

    id = Column (MSInteger, primary_key = True)
    name = Column (MSString(45), nullable = False)
    python_object = Column (MSString(200), nullable = False)
    container = Column (MSEnum("configs", "units"), nullable = False)
    label = Column (MSString(45), nullable = False)

    def __init__ (self, name, python_object, container, label):
        self.name = name
        self.python_object = python_object
        self.container.container
        self.label = label

    def __repr__ (self):
        return "<DeviceType ('%d', '%s', '%s', '%s', '%s')>" % (self.id, self.name, self.python_object, self.container, self.label)

Видимо я что-то делаю не так, не могу понять что.

Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.