OneToMany() causing NOT NULL fields to be set to NULL

3 views
Skip to first unread message

Randy Syring

unread,
Sep 28, 2008, 7:01:14 PM9/28/08
to SQLElixir
I have a ManyToOne() relationship defined with both required=True, and
ondelete='cascade'. When deleting a record from the parent table,
Elixir is trying to update related records in the child table and set
the parent_id = Null, which is invalid since it is a NOT NULL column.
If I remove the corresponding field in the parent object, I don't have
the problem any more. Any ideas?

My object defs:

class ContentBaseAttributeCategory(Entity):

name = Field(Unicode(255), required=True, unique=True, index=True)
display = Field(Unicode(1000))
inactive = Field(Boolean, required=True, server_default=text("0"))
created = Field(DateTime, required=True,
default=datetime.datetime.now)
last_edited = Field(DateTime, onupdate=datetime.datetime.now)
## if uncommented, SQL errors are generated
#attributes = OneToMany('ContentBaseAttribute')

using_options(tablename="contentbase_attribute_categories")

class ContentBaseAttribute(Entity):

category = ManyToOne('ContentBaseAttributeCategory',
required=True, ondelete='cascade')
name = Field(Unicode(255), required=True)
display = Field(Unicode(1000))
sort_order = Field(Integer, required=True,
server_default=text("0"))
inactive = Field(Boolean, required=True, server_default=text("0"))
created = Field(DateTime, required=True,
default=datetime.datetime.now)
last_edited = Field(DateTime, onupdate=datetime.datetime.now)
items = ManyToMany('ContentBaseItem',
tablename='contentbase_attribute_item_map', ondelete='cascade')

using_options(tablename="contentbase_attributes")

def get_catname(self):
return self.category.name

catname = property(get_catname)

Sok Ann Yap

unread,
Sep 29, 2008, 3:31:11 AM9/29/08
to sqle...@googlegroups.com

Try adding cascade='all, delete-orphan' to the OneToMany relationship.
You can read more about this here:
http://www.sqlalchemy.org/docs/04/documentation.html#unitofwork_cascades

Randy Syring

unread,
Sep 30, 2008, 2:08:44 AM9/30/08
to SQLElixir
Thank you for the suggestion, but that doesn't change anything. I
still get an UPDATE statement issued that tries to set the FK id to
NULL, which is invalid and causes SQL logic errors.

Suggestions on where I can go from here would be really appreciated.
I am really stuck on this. All I need to do is shut that off and
things would work fine.

On Sep 29, 3:31 am, "Sok Ann Yap" <sok...@gmail.com> wrote:

Randy Syring

unread,
Sep 30, 2008, 2:18:26 AM9/30/08
to SQLElixir
Sorry, one follow-up. I can actually get this to work by setting
cascade='all, delete-orphan'.

New thread here: http://groups.google.com/group/sqlalchemy/browse_thread/thread/4740463f8474ff74

since I would still like SQLAlchemy to not issue any Deletes or
Updates, since my FK will handle it.

Randy Syring

unread,
Sep 30, 2008, 12:50:06 PM9/30/08
to SQLElixir
Reply all
Reply to author
Forward
0 new messages