Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #769 (closed enhancement: fixed)

Opened 11 years ago

Last modified 10 years ago

[PATCH] Allow creation of foreign keys to same table

Reported by: turbo_NOSPAM_gears[@… Owned by: godoy
Priority: normal Milestone: 0.9a5
Component: Toolbox Version: 0.9a4
Severity: normal Keywords: quick fix
Cc: ronald@…

Description

SQLObjects allows you to have a ForeignKey? to the current class. In the example below, this makes it very easy to do threaded posts. CatWalk has no problems with making use of this either. I don't see why this functionality should be artificially disabled in the model designer.

# example model.py

class Post(SQLObject):

text = StringCol?() parent = ForeignKey?("Post") children = MultipleJoin?("Post",joinColumn='parent_id')

# example usage

Post.get(1)

<Post 1 text='First post' parentID=None>

Post.get(1).children

[<Post 2 text='First sub' parentID=1>, <Post 3 text='Second sub' parentID=1>]

Post.get(2).parent

<Post 1 text='First post' parentID=None>

Attachments

model_designer_same_table_join.patch Download (1.6 KB) - added by turbo_NOSPAM_gears[@… 11 years ago.
Patch to get desired functionality from model designer
model_designer_same_table_join.2.patch Download (1.6 KB) - added by Philip Walls 11 years ago.
Fixed version of patch

Change History

Changed 11 years ago by turbo_NOSPAM_gears[@…

Patch to get desired functionality from model designer

comment:1 Changed 11 years ago by turbo_NOSPAM_gears[@…

Forgot to mention, the patch is against SVN r1189

comment:2 Changed 11 years ago by Philip Walls (turbogears@…

Please pardon my Trac noobery. Here are the examples in the summary, but more readable:

Example Model

class Post(SQLObject):

        text = StringCol()
        parent = ForeignKey("Post")
        children = MultipleJoin("Post",joinColumn='parent_id')

Example usage

>>> Post.get(1)
<Post 1 text='First post' parentID=None>

>>> Post.get(1).children
[<Post 2 text='First sub' parentID=1>, <Post 3 text='Second sub' parentID=1>]

>>> Post.get(2).parent
<Post 1 text='First post' parentID=None>

comment:3 Changed 11 years ago by anonymous

  • Summary changed from Allow creation of foreign keys to same table to [PATCH] Allow creation of foreign keys to same table

comment:4 Changed 11 years ago by Philip Walls (turbogears@…

  • Keywords quick fix added

comment:5 Changed 11 years ago by godoy

The impossibility to create it in the toolbox is confirmed. I haven't tested the patch.

comment:6 Changed 11 years ago by Philip Walls

I (the submitter) tested this patch against SVN 1189, and have been using that patched version for a project I'm working on. Tested creating MultipleJoin?, SingleJoin?, and ForeignKey? columns to the same table and had no problems. I have not tested RelatedJoins? (seems like it should not be supported?).

Please let me know if there's anything else I can help with, or if you need any other information. I'm also available on IRC as malverian@freenode#turbogears

comment:7 Changed 11 years ago by Philip Walls (malverian)

I was curious if there was a reason this hasn't been committed to trunk yet? If the patch needs to be refactored in some way prior to commit, please let me know and I will take care of it. If the patch is entirely invalid, please resolve the ticket so I can stop getting my hopes up ;)

comment:8 Changed 11 years ago by godoy

  • Owner changed from anonymous to godoy

This applies cleanly to the trunk. If there's nothing against it, I can commit it.

comment:9 Changed 11 years ago by godoy

Ops, I spoke too soon. It applies cleanly but doesn't work. Firebug tells me that "m is not defined" at line 73 of modelDesigner.js. This is against the trunk. Could you check it please, Philip?

Changed 11 years ago by Philip Walls

Fixed version of patch

comment:10 Changed 11 years ago by Philip Walls

I apparently uploaded the wrong patch originally, as I had this problem fixed in my checkout. I apologize for the inconvenience.

One thing I was wondering about was whether or not to remove the designer.tableRowOtherClasses function since it doesn't appear to be in use elsewhere. I left it just in case such functionality is needed in the future.

comment:11 Changed 11 years ago by michele

  • Cc ronald@… added

Adding Ronald to cc, it would be nice to get this into 0.9a5.

comment:12 Changed 11 years ago by Philip Walls

Agreed, I'm a little confused as to why someone actually went out of their way to disallow this functionality?

comment:13 Changed 11 years ago by kevin

I committed this in [1274], but I actually haven't tested yet. Patch applied cleanly, but if someone can check to confirm that it's working well, I'd appreciate it.

comment:14 Changed 11 years ago by Philip Walls

Kevin, it is working fine here. Is this feature going to be merged into trunk for inclusion in 0.9a5? I noticed you had committed it only to the 1.0 branch.

There is a small problem I'm experiencing with the model designer when trying to use the "Write to model and create tables" feature, but it is not specific to the above patch. I've filed a separate bug about this problem in #793

comment:15 Changed 11 years ago by kevin

  • Status changed from new to closed
  • Resolution set to fixed

0.9a5 will come from the 1.0 branch. (0.9 is 1.0 in disguise).

I'm going to close this ticket, but I don't want to do 793 without Ronald taking a look.

Note: See TracTickets for help on using tickets.