I've gone through the tutorial and I'm seeing something odd with the  
foreign keys.

With this code:

     rscott = Director(name="Ridley Scott")
     glucas = Director(name="George Lucas")


     alien = Movie(title="Alien", year=1979 )
     swars = Movie(title="Star Wars", year=1977)
     brunner = Movie(title="Blade Runner", year=1982)
     esb = Movie(title="Empire Strikes Back",year=1980)


     # works if you do an append first, then set movie's director
     rscott.movies.append(alien)
     brunner.director = rscott

     # does not work if you just set movie's directory
     #   the movie gets the link to the director, but the
     #   movie is not added to the director
     swars.director = glucas
     esb.director = glucas

I get the following output (complete test program attached):

---------------------

> python test.py

Movie:
   director: '<Director: "Ridley Scott">'
   director_id: '2'
   id: '3'
   title: Alien
   year: '1979'

Movie:
   director: '<Director: "Ridley Scott">'
   director_id: '2'
   id: '4'
   title: Blade Runner
   year: '1982'

Movie:
   director: '<Director: "George Lucas">'
   director_id: '1'
   id: '6'
   title: Empire Strikes Back
   year: '1980'

Movie:
   director: '<Director: "George Lucas">'
   director_id: '1'
   id: '5'
   title: Star Wars
   year: '1977'

Director:
   id: '1'
   name: George Lucas

Director:
   id: '2'
   movies: '[<Movie: "Alien" (1979)>, <Movie: "Blade Runner" (1982)>]'
   name: Ridley Scott

----------------------

Note that glucas director has been added to the movies, but the glucas  
director doesn't have links the other direction to the movies.

I'm using Elixir-0.5.2, PyYAML-3.05, and sqlalchemy-0.4.6



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SQLElixir" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---

#!/usr/bin/python
# -*- coding: latin-1 -*-

from elixir import *

from yaml import load, dump
try:
    from yaml import CLoader as Loader
    from yaml import CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper


def _toYamlRep(ent):
    """
    Given an elixir entity, query the entity's members via its __dict__ 
    and return a dict suitable for dumping via YAML
    """
    ret = {}
    for (k,v) in ent.__dict__.items():
        if k.startswith('_') or k == 'row_type':
            # don't print out the 'hidden' keys
            continue
        if v:
            ret[k] = str(v)
    return ret

class YamlEntity(Entity):
    def toYamlRep(self):
        """
        Wrap the _toYamlRep() dict in another dict, use the class' name as the 
header.
        """
        return {self.__class__.__name__ : _toYamlRep(self) }

class Movie(YamlEntity):
    title = Field(String(30))
    year = Field(Integer())
    description = Field(Text())
    director = ManyToOne('Director')

    using_options(order_by=['title','year'])

    def __repr__(self):
        return '<Movie: "%s" (%d)>'%(self.title,self.year)
    def toYamlRep(self):
        """
        Wrap the _toYamlRep() dict in another dict, use the class' name as the 
header.
        """
        return {self.__class__.__name__ : _toYamlRep(self) }


class Director(YamlEntity):
    name = Field(String(60))
    movies = OneToMany('Movie')

    using_options(order_by=['name'])

    def __repr__(self):
        return '<Director: "%s">'%(self.name)


def main():
    metadata.bind = "sqlite://"
    setup_all()
    create_all()

    rscott = Director(name="Ridley Scott")
    glucas = Director(name="George Lucas")


    alien = Movie(title="Alien", year=1979 )
    swars = Movie(title="Star Wars", year=1977)
    brunner = Movie(title="Blade Runner", year=1982)
    esb = Movie(title="Empire Strikes Back",year=1980)
    

    # works if you do an append first, then set movie's director
    rscott.movies.append(alien)
    brunner.director = rscott

    # does not work if you just set movie's directory
    #   the movie gets the link to the director, but the
    #   movie is not added to the director
    swars.director = glucas
    esb.director = glucas

    session.flush()

    for m in Movie.query().all():
        print dump(m.toYamlRep(),Dumper=Dumper,default_flow_style=False)

    for d in Director.query().all():
        print dump(d.toYamlRep(),Dumper=Dumper,default_flow_style=False)

    cleanup_all()

if __name__ == '__main__':
    main()

Reply via email to