On Tuesday, June 29, 2010 14:33:04 chaouche yacine wrote:
> Hi list,
>
> this is my first post on this list I guess, so please forgive my newbiness
> and my ignorance of this list etiquette (if any).
>
> So here's a little piece of code that illustrates my situation on foreign
> key constraints :
>
> (briefly : the delete function should break, but it doesen't)
>
> <code>
>
> from elixir import *
>
> class BaseModel(Entity):
> using_options(abstract=True)
>
> repr_attr = "name"
>
> def __repr__(self):
> """
> """
> return "<%s '%s' id='%s'>" %
> (self.__class__.__name__,getattr(self,self.repr_attr,id(self)),self.id)
>
> class Country(BaseModel):
> """
> """
> using_options(tablename = "countries")
>
> name = Field(Unicode)
> cities = OneToMany("City")
>
> class City(BaseModel):
> """
> """
> using_options(tablename = "cities")
>
> name = Field(Unicode)
> country = ManyToOne("Country",ondelete="RESTRICT")
>
> metadata.bind = "postgres://auser:somepas...@somedhost/somedb"
> # I use this to issue some raw sql (truncate not supported by sqla/elixir
> ?) session.bind = metadata.bind
> drop_all()
> setup_all(True)
>
> # This line will break if uncommented, because of fk constraints
> #session.execute("TRUNCATE TABLE cities")
> session.execute("TRUNCATE TABLE countries CASCADE")
> session.commit()
>
> # just to make them global
> algeria = None
> algiers = None
>
> def create():
> global algeria,algiers
> algeria = Country(name=u"Algeria")
> algiers = City(name=u"Algiers",country=algeria)
> session.commit()
> algeria.delete()
>
> def delete():
> global algeria,algiers
> # THIS DOSEN'T BREAK which is NOT what we want
> algeria.delete()
> session.flush()
>
> create()
> delete()
>
> </code>
>
>
> I'm sure I'm missing something, but where ?...
You don't make country a required field. So SQLAlchemy will set it to NULL,
which is perfectly fine from the model.
Add a "required=True" parameter to the ManyToOne-Country-declaration.
Diez
--
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.