Hi list,
There's one problem : the error I'm getting is not on the foreign key
constraint it's on the NOT NULL contraint.
Now there's a weired thing happening :
I define my models like this :
<code>
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,nullable=False)
cities = OneToMany("City")
class City(BaseModel):
"""
"""
using_options(tablename = "cities")
name = Field(Unicode)
country = ManyToOne("Country",ondelete="restrict")
</code>
The table definition that elixir generates is correct : my foreign key
contraint is there :
<sql>
CREATE TABLE cities (
id SERIAL NOT NULL,
name VARCHAR,
country_id INTEGER,
PRIMARY KEY (id),
CONSTRAINT cities_country_id_fk FOREIGN KEY(country_id) REFERENCES
countries (id) ON DELETE restrict
)
</sql>
Here's the rest of the script
<code>
metadata.bind = "postgres://coriolis:coriolis234acc...@localhost/testdb"
metadata.bind.echo = True
setup_all()
drop_all()
create_all()
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()
def delete():
global algeria,algiers
# THIS DOSEN'T BREAK which is NOT what we want
algeria.delete()
session.commit()
create()
#delete()
session.commit()
</code>
First, I comment the delete() function call to let the created instances there
on the database. Then I go on phppgadmin and tries to delete the country but
get an error (table cities still referencing etc.), which is the desired
behaviour.
Now i re-run the script and uncomment the delete function call like this :
<code>
metadata.bind = "postgres://coriolis:coriolis234acc...@localhost/testdb"
metadata.bind.echo = True
setup_all()
drop_all()
create_all()
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()
def delete():
global algeria,algiers
# THIS DOSEN'T BREAK which is NOT what we want
algeria.delete()
session.commit()
create()
delete()
session.commit()
</code>
But the script dosen't raise the exception.
Now a solution proposed by Diez was to add a required=True on the country
relation, which kind of works, but is semantically acceptable. Furthermore, on
some of my models, certain relations are not required, so setting a
required=True dosen't make sens I guess, but I still want them to prevent a
deletion on the other side of the relation if they are still pointing to it.
What can I do to impement this ? shouldn't ondelete="restrict" be enough ?
Thank you for your precious help.
Y.Chaouche
--
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.