[sqlalchemy] Re: M2M relationship

2008-07-15 Thread Heston James - Cold Beans

Hi Michael,

 create a file called something like globals.py, and in all other  
 modules that use SQLAlchemy, say import globals. A primer on  
 modules, packages and such is at http://www.python.org/doc/tut/node8.html 

Excellent! This seems to have done the job, I am now successfully saving and
pulling these objects from the database, very cool!

Thank you again.

Heston


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



[sqlalchemy] Re: M2M relationship

2008-07-14 Thread Heston James - Cold Beans

Hi Michael,

 what I see immediately is that you're declaring mutliple  
 declarative_bases and multiple MetaData objects.   All of the Table  
 objects which relate to one another need to share the same underlying  
 MetaData object, and the declarative_base() function also uses a  
 MetaData object which it creates for you, unless one is passed.

 So you need a global module everyone works from which starts with  
 something like:

Thank you for pointing this out, it certainly sounds like it could be the
problem, it would explain why none of the tables seem to know about one
another :-D

I have a singleton module which is a factory for my database connections
which gets passed around into the beans, I will place these metadata and
declarative base classes into that and see how that helps.

I'll give this a try in the next few hours and let you know how I get on.

Heston


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



[sqlalchemy] Re: M2M relationship

2008-07-14 Thread Heston James - Cold Beans

Hello Michael,

 what I see immediately is that you're declaring mutliple  
 declarative_bases and multiple MetaData objects.   All of the Table  
 objects which relate to one another need to share the same underlying  
 MetaData object, and the declarative_base() function also uses a  
 MetaData object which it creates for you, unless one is passed.

 So you need a global module everyone works from which starts with  
 something like:

 meta = MetaData()
 Base = declarative_base(metadata=meta)

 then every Table uses the above meta as its metadata argument,  
 every declared mapped class inherits from Base.

Thank you kindly for this concept, I really appreciate your advice thus far.
I'm still struggling with this same challenge though, I've got a horrible
dose of n00bitus I'm afraid.

Above you talk about a global module in the application which creates the
Base and metadata, but I don't understand how these can then be accessed by
other classes around the application?

Do you have any good sample code or a link to a decent tutorial? Seems all I
can find are examples which are based on the idea that all the classes are
defined in the same module, which isn't practical.

I really appreciate the help Michael, thanks.

Heston


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



[sqlalchemy] Re: M2M relationship

2008-07-14 Thread Michael Bayer


On Jul 14, 2008, at 11:02 AM, Heston James - Cold Beans wrote:


 Hello Michael,

 what I see immediately is that you're declaring mutliple
 declarative_bases and multiple MetaData objects.   All of the Table
 objects which relate to one another need to share the same underlying
 MetaData object, and the declarative_base() function also uses a
 MetaData object which it creates for you, unless one is passed.

 So you need a global module everyone works from which starts with
 something like:

 meta = MetaData()
 Base = declarative_base(metadata=meta)

 then every Table uses the above meta as its metadata argument,
 every declared mapped class inherits from Base.

 Thank you kindly for this concept, I really appreciate your advice  
 thus far.
 I'm still struggling with this same challenge though, I've got a  
 horrible
 dose of n00bitus I'm afraid.

 Above you talk about a global module in the application which  
 creates the
 Base and metadata, but I don't understand how these can then be  
 accessed by
 other classes around the application?

create a file called something like globals.py, and in all other  
modules that use SQLAlchemy, say import globals. A primer on  
modules, packages and such is at http://www.python.org/doc/tut/node8.html 
  .


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



[sqlalchemy] Re: M2M relationship

2008-07-14 Thread King Simon-NFHD78

Heston wrote:
 [SNIP]
 
 Above you talk about a global module in the application which 
 creates the
 Base and metadata, but I don't understand how these can then 
 be accessed by
 other classes around the application?
 
 Do you have any good sample code or a link to a decent 
 tutorial? Seems all I
 can find are examples which are based on the idea that all 
 the classes are
 defined in the same module, which isn't practical.
 

Hi Heston,

This is really a plain python question rather than an SQLAlchemy
question. You might find this section of the python tutorial useful:

http://docs.python.org/dev/tutorial/modules.html

But basically, if you put the following code in a module called
'base.py':

#-
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import MetaData

meta = MetaData()
Base = declarative_base(metadata=meta)
#-

Then in your other modules you can write:

#-
import base

class Post(base.Base):
__tablename__ = 'post'

# etc.

#-

Hope that helps,

Simon

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



[sqlalchemy] Re: M2M relationship

2008-07-13 Thread Heston James - Cold Beans

Hi Michael,

 declarative places a convenience __init__ that installs keywords as  
 attributes, but you're free to override this constructor with anything  
 you'd like.

Thank you for confirming this for me, I'd hoped I'd be able to override the
class constructor, I often use it for considerably more than basic property
setting and it would be a shame if declarative had upset that.

I'm still yet to solve this problem, don't have any ideas what I'm doing
wrong do you? Did you see the code examples I attached? Am I approaching
this in the correct manor?

Thanks,

Heston


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



[sqlalchemy] Re: M2M relationship

2008-07-13 Thread satoru

i'm sorry for my misleading reply;(
i was kind of too sleepy last night;P

On Jul 13, 5:29 pm, Heston James - Cold Beans
[EMAIL PROTECTED] wrote:
 Hi Michael,

  declarative places a convenience __init__ that installs keywords as  
  attributes, but you're free to override this constructor with anything  
  you'd like.

 Thank you for confirming this for me, I'd hoped I'd be able to override the
 class constructor, I often use it for considerably more than basic property
 setting and it would be a shame if declarative had upset that.

 I'm still yet to solve this problem, don't have any ideas what I'm doing
 wrong do you? Did you see the code examples I attached? Am I approaching
 this in the correct manor?

 Thanks,

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



[sqlalchemy] Re: M2M relationship

2008-07-13 Thread Heston James - Cold Beans

 i'm sorry for my misleading reply;(
 i was kind of too sleepy last night;P

No problem my man.

Heston.


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



[sqlalchemy] Re: M2M relationship

2008-07-13 Thread Michael Bayer


On Jul 13, 2008, at 5:29 AM, Heston James - Cold Beans wrote:


 Hi Michael,

 declarative places a convenience __init__ that installs keywords as
 attributes, but you're free to override this constructor with  
 anything
 you'd like.

 Thank you for confirming this for me, I'd hoped I'd be able to  
 override the
 class constructor, I often use it for considerably more than basic  
 property
 setting and it would be a shame if declarative had upset that.

 I'm still yet to solve this problem, don't have any ideas what I'm  
 doing
 wrong do you? Did you see the code examples I attached? Am I  
 approaching
 this in the correct manor?

what I see immediately is that you're declaring mutliple  
declarative_bases and multiple MetaData objects.   All of the Table  
objects which relate to one another need to share the same underlying  
MetaData object, and the declarative_base() function also uses a  
MetaData object which it creates for you, unless one is passed.

So you need a global module everyone works from which starts with  
something like:

meta = MetaData()
Base = declarative_base(metadata=meta)

then every Table uses the above meta as its metadata argument,  
every declared mapped class inherits from Base.

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



[sqlalchemy] Re: M2M relationship

2008-07-12 Thread satoru

i guess you just shouldn't override the constructor of DeclartiveBase,
because it did some keyword args magic inside the constructor.

On Jul 12, 12:20 am, Heston James - Cold Beans
[EMAIL PROTECTED] wrote:
  if u look up the stacktrace/traceback, u'll see which statement in
  your own code triggered the error. is it in the mapping-part or is
  still in table-declaration part?
  do all 3 tables use same metadata?

 Thank you for your comments so far, I appreciate you helping me out on this.
 The entire stack trace is below:

 Traceback (most recent call last):
   File test.py, line 9, in module
     my_device = post.post(3F8ADE52-4F63-11DD-9AF0-90BB55D89593, Title,
 Content)
   File /var/lib/python-support/python2.5/sqlalchemy/orm/attributes.py,
 line 1211, in init
     extra_init(class_, oldinit, instance, args, kwargs)
   File /var/lib/python-support/python2.5/sqlalchemy/orm/mapper.py, line
 798, in extra_init
     self.compile()
   File /var/lib/python-support/python2.5/sqlalchemy/orm/mapper.py, line
 350, in compile
     mapper.__initialize_properties()
   File /var/lib/python-support/python2.5/sqlalchemy/orm/mapper.py, line
 371, in __initialize_properties
     prop.init(key, self)
   File /var/lib/python-support/python2.5/sqlalchemy/orm/interfaces.py,
 line 374, in init
     self.do_init()
   File /var/lib/python-support/python2.5/sqlalchemy/orm/properties.py,
 line 467, in do_init
     self.__determine_joins()
   File /var/lib/python-support/python2.5/sqlalchemy/orm/properties.py,
 line 521, in __determine_joins
     self.secondaryjoin = _search_for_join(self.mapper,
 self.secondary).onclause
   File /var/lib/python-support/python2.5/sqlalchemy/orm/properties.py,
 line 514, in _search_for_join
     return sql.join(mapper.local_table, table)
   File /var/lib/python-support/python2.5/sqlalchemy/sql/expression.py,
 line 116, in join
     return Join(left, right, onclause, isouter)
   File /var/lib/python-support/python2.5/sqlalchemy/sql/expression.py,
 line 2275, in __init__
     self.onclause = self.__match_primaries(self.left, self.right)
   File /var/lib/python-support/python2.5/sqlalchemy/sql/expression.py,
 line 2317, in __match_primaries
     return sql_util.join_condition(primary, secondary)
   File /var/lib/python-support/python2.5/sqlalchemy/sql/util.py, line 74,
 in join_condition
     col = fk.get_referent(a)
   File /var/lib/python-support/python2.5/sqlalchemy/schema.py, line 755,
 in get_referent
     return table.corresponding_column(self.column)
   File /var/lib/python-support/python2.5/sqlalchemy/schema.py, line 788,
 in column
     foreign key % tname)
 sqlalchemy.exceptions.NoReferencedTableError: Could not find table 'post'
 with which to generate a foreign key

 I've also attached the two modules and the test script I'm trying to use, if
 you wouldn't mind taking a look an letting me know what I'm doing wrong, I
 would really appreciate it.

 Cheers,

 Heston

  keyword.py
 1KDownload

  post.py
 1KDownload

  test.py
 1KDownload

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



[sqlalchemy] Re: M2M relationship

2008-07-12 Thread Michael Bayer


On Jul 12, 2008, at 8:59 AM, satoru wrote:


 i guess you just shouldn't override the constructor of DeclartiveBase,
 because it did some keyword args magic inside the constructor.


declarative places a convenience __init__ that installs keywords as  
attributes, but you're free to override this constructor with anything  
you'd like.



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



[sqlalchemy] Re: M2M relationship

2008-07-11 Thread az

NameError's are thrown usualy by import'ing or similar mechanisms.
have a look on your code.
eventualy post the whole traceback?

On Friday 11 July 2008 12:14:12 Heston James - Cold Beans wrote:
 Good morning all,



 So, this morning's challenge has been learning many-to-many
 relationships, after reading through the tutorial I understand most
 of the core concepts of how it should work but I'm struggling to
 actually make it do so, I thought I would come and rely on you good
 people to help me in the right direction.



 I have 3 tables configured in my MySQL database, for arguments sake
 let's say they're called 'post', 'keyword' and 'post_keyword'. I'm
 declaring my 'post' class like so in a module called post.py:



 from sqlalchemy.ext.declarative import declarative_base

 from sqlalchemy import Table, Column, Integer, String, MetaData,
 ForeignKey

 from sqlalchemy.orm import relation, backref



 # Configure the delarative base for SQL Alchemy.

 Base = declarative_base()



 # Define the Remote Device class.

 class post(Base):



 # Define the table for SQL Aclchemy

 __tablename__ = post



 # Define the class properties for SQL Alchemy

 id = Column(String, primary_key=True)

 content = Column(String)



 keywords = relation(keyword, secondary=post_keyword,
 backref='keywords')



 I then have a pretty much identical class declaration for 'post'
 but with the obvious changes to its name and property. However,
 when trying to use this class I get an exception thrown by
 SQLAlchemy saying:



 NameError: name 'remote_device_message' is not defined



 Which is fair enough, as it isn't, I wonder if I'm meant to import
 it somehow into that post class?



 All the examples I've found seem to focus on the idea of A) having
 these two classes defined in the same file and B) using an in
 memory database where you 'create' the association table in the
 script, whereas with mine it already exists in the database. It's
 making me a little confused I think.



 I'd really appreciate your help on showing me how this implements.



 Cheers,



 Heston


 


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



[sqlalchemy] Re: M2M relationship

2008-07-11 Thread Heston James - Cold Beans
 NameError's are thrown usualy by import'ing or similar mechanisms.

 have a look on your code.

 eventualy post the whole traceback?

 

Hello Mate,

 

I think you're right, but the problem is that I don't know what I 'should'
be importing into the class. See, I have two files; Post.py and Keyword.py,
both of which contain a single class of the same name.

 

I want to form a M2M relationship between them. If I want to declare a
relationship in Post.py do I have to import Keyword.py? and what about the
joining/association table? Do I need to create a class which represents that
too?

 

Cheers,

 

Heston


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



[sqlalchemy] Re: M2M relationship

2008-07-11 Thread Michael Bayer

On Jul 11, 2008, at 9:59 AM, Heston James - Cold Beans wrote:

  NameError's are thrown usualy by import'ing or similar mechanisms.
  have a look on your code.
  eventualy post the whole traceback?

 Hello Mate,

 I think you’re right, but the problem is that I don’t know what I  
 ‘should’ be importing into the class. See, I have two files; Post.py  
 and Keyword.py, both of which contain a single class of the same name.

 I want to form a M2M relationship between them. If I want to declare  
 a relationship in Post.py do I have to import Keyword.py? and what  
 about the joining/association table? Do I need to create a class  
 which represents that too?


the association table is an instance of Table, and does not need its  
own class.   It's easiest to declare the association table in the same  
module as that which it is used, in this case post.py.

When you create post.py, that creates Post, and in order to create  
the relation either needs to import keyword.py directly, or, when a  
third module uses post.py it would have to also import keyword.py  
before using the Post class.

Basiclally nothing happens until the classes are first used.When  
the classes are first used, all dependencies must have been imported  
at some point.  It doesn't matter from where since they are all  
ultimately placing themselves in a SQLAlchemy-specific registry.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: M2M relationship

2008-07-11 Thread Heston James - Cold Beans

 the association table is an instance of Table, 
 and does not need its own class. It's easiest to declare 
 the association table in the same module as that which 
 it is used, in this case post.py.

Ok this sounds fine, I've done this now, declaring the table in the post.py
module.

 When you create post.py, that creates Post, and in order 
 to create the relation either needs to import keyword.py directly, 
 or, when a third module uses post.py it would have to also 
 import keyword.py before using the Post class.

That's fine too, I've now imports keyword into post.

I'm now getting a new error thrown at me though:

File /var/lib/python-support/python2.5/sqlalchemy/schema.py, line 788, in
column foreign key % tname) sqlalchemy.exceptions.NoReferencedTableError:
Could not find table 'post' with which to generate a foreign key

This is unusual as the table post does exist in the database, and is also
declared as the class in the post.py module.

Any ideas?

Thanks Michael,

Heston


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



[sqlalchemy] Re: M2M relationship

2008-07-11 Thread az

if u look up the stacktrace/traceback, u'll see which statement in 
your own code triggered the error. is it in the mapping-part or is 
still in table-declaration part?
do all 3 tables use same metadata?

On Friday 11 July 2008 17:31:31 Heston James - Cold Beans wrote:
  the association table is an instance of Table,
  and does not need its own class. It's easiest to declare
  the association table in the same module as that which
  it is used, in this case post.py.

 Ok this sounds fine, I've done this now, declaring the table in the
 post.py module.

  When you create post.py, that creates Post, and in order
  to create the relation either needs to import keyword.py
  directly, or, when a third module uses post.py it would have to
  also import keyword.py before using the Post class.

 That's fine too, I've now imports keyword into post.

 I'm now getting a new error thrown at me though:

 File /var/lib/python-support/python2.5/sqlalchemy/schema.py, line
 788, in column foreign key % tname)
 sqlalchemy.exceptions.NoReferencedTableError: Could not find table
 'post' with which to generate a foreign key

 This is unusual as the table post does exist in the database, and
 is also declared as the class in the post.py module.

 Any ideas?

 Thanks Michael,

 Heston


 


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



[sqlalchemy] Re: M2M relationship

2008-07-11 Thread Heston James - Cold Beans
 if u look up the stacktrace/traceback, u'll see which statement in 
 your own code triggered the error. is it in the mapping-part or is 
 still in table-declaration part?
 do all 3 tables use same metadata?

Thank you for your comments so far, I appreciate you helping me out on this.
The entire stack trace is below:

Traceback (most recent call last):
  File test.py, line 9, in module
my_device = post.post(3F8ADE52-4F63-11DD-9AF0-90BB55D89593, Title,
Content)
  File /var/lib/python-support/python2.5/sqlalchemy/orm/attributes.py,
line 1211, in init
extra_init(class_, oldinit, instance, args, kwargs)
  File /var/lib/python-support/python2.5/sqlalchemy/orm/mapper.py, line
798, in extra_init
self.compile()
  File /var/lib/python-support/python2.5/sqlalchemy/orm/mapper.py, line
350, in compile
mapper.__initialize_properties()
  File /var/lib/python-support/python2.5/sqlalchemy/orm/mapper.py, line
371, in __initialize_properties
prop.init(key, self)
  File /var/lib/python-support/python2.5/sqlalchemy/orm/interfaces.py,
line 374, in init
self.do_init()
  File /var/lib/python-support/python2.5/sqlalchemy/orm/properties.py,
line 467, in do_init
self.__determine_joins()
  File /var/lib/python-support/python2.5/sqlalchemy/orm/properties.py,
line 521, in __determine_joins
self.secondaryjoin = _search_for_join(self.mapper,
self.secondary).onclause
  File /var/lib/python-support/python2.5/sqlalchemy/orm/properties.py,
line 514, in _search_for_join
return sql.join(mapper.local_table, table)
  File /var/lib/python-support/python2.5/sqlalchemy/sql/expression.py,
line 116, in join
return Join(left, right, onclause, isouter)
  File /var/lib/python-support/python2.5/sqlalchemy/sql/expression.py,
line 2275, in __init__
self.onclause = self.__match_primaries(self.left, self.right)
  File /var/lib/python-support/python2.5/sqlalchemy/sql/expression.py,
line 2317, in __match_primaries
return sql_util.join_condition(primary, secondary)
  File /var/lib/python-support/python2.5/sqlalchemy/sql/util.py, line 74,
in join_condition
col = fk.get_referent(a)
  File /var/lib/python-support/python2.5/sqlalchemy/schema.py, line 755,
in get_referent
return table.corresponding_column(self.column)
  File /var/lib/python-support/python2.5/sqlalchemy/schema.py, line 788,
in column
foreign key % tname)
sqlalchemy.exceptions.NoReferencedTableError: Could not find table 'post'
with which to generate a foreign key

I've also attached the two modules and the test script I'm trying to use, if
you wouldn't mind taking a look an letting me know what I'm doing wrong, I
would really appreciate it.

Cheers,

Heston

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

# Class Imports
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey

# Configure the delarative base for SQL Alchemy.
Base = declarative_base()

# Define the Remote Device class.
class keyword(Base):

# Define the table for SQL Aclchemy
__tablename__ = keyword

# Define the class properties for SQL Alchemy
keyword_id = Column(Integer, primary_key=True)
word = Column(String)

# I'm the class constructor method.
def __init__(self, keyword_id=, word=):
self.keyword_id = keyword
self.word = word
# Class Imports
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.orm import relation, backref
import keyword

# Configure the delarative base for SQL Alchemy.
Base = declarative_base()

metadata = MetaData()

post_keyword = Table(post_keyword, metadata,
  Column(post_id, String, ForeignKey('post.post_id')),
  Column('keyword_id', Integer, 
ForeignKey('keyword.keyword_id'))
)

# Define the Remote Device class.
class post(Base):

# Define the table for SQL Aclchemy
__tablename__ = post

# Define the class properties for SQL Alchemy
post_id = Column(String, primary_key=True)
title = Column(String)
content = Column(String)

# many to many BlogPost-Keyword
keywords = relation(keyword.keyword, secondary=post_keyword, backref=post)

# I'm the class constructor method.
def __init__(self, post_id=, title=, content=):
self.post_id = post_id
self.title = title
self.content = contentimport post
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

engine = create_engine(connectionstring, echo=False)
Session = 

[sqlalchemy] Re: M2M relationship

2008-07-11 Thread az

i'm not very familiar with declarative but in any way i dont see where 
u bind a) the metadata to the engine, and b) the declarative-stuff to 
the metadata. maybe its something i'm missing but maybe read more on 
those.

On Friday 11 July 2008 19:20:21 Heston James - Cold Beans wrote:
  if u look up the stacktrace/traceback, u'll see which statement
  in your own code triggered the error. is it in the mapping-part
  or is still in table-declaration part?
  do all 3 tables use same metadata?

 Thank you for your comments so far, I appreciate you helping me out
 on this. The entire stack trace is below:

 Traceback (most recent call last):
   File test.py, line 9, in module
 my_device = post.post(3F8ADE52-4F63-11DD-9AF0-90BB55D89593,
 Title, Content)
   File
 /var/lib/python-support/python2.5/sqlalchemy/orm/attributes.py,
 line 1211, in init
 extra_init(class_, oldinit, instance, args, kwargs)
   File
 /var/lib/python-support/python2.5/sqlalchemy/orm/mapper.py, line
 798, in extra_init
 self.compile()
   File
 /var/lib/python-support/python2.5/sqlalchemy/orm/mapper.py, line
 350, in compile
 mapper.__initialize_properties()
   File
 /var/lib/python-support/python2.5/sqlalchemy/orm/mapper.py, line
 371, in __initialize_properties
 prop.init(key, self)
   File
 /var/lib/python-support/python2.5/sqlalchemy/orm/interfaces.py,
 line 374, in init
 self.do_init()
   File
 /var/lib/python-support/python2.5/sqlalchemy/orm/properties.py,
 line 467, in do_init
 self.__determine_joins()
   File
 /var/lib/python-support/python2.5/sqlalchemy/orm/properties.py,
 line 521, in __determine_joins
 self.secondaryjoin = _search_for_join(self.mapper,
 self.secondary).onclause
   File
 /var/lib/python-support/python2.5/sqlalchemy/orm/properties.py,
 line 514, in _search_for_join
 return sql.join(mapper.local_table, table)
   File
 /var/lib/python-support/python2.5/sqlalchemy/sql/expression.py,
 line 116, in join
 return Join(left, right, onclause, isouter)
   File
 /var/lib/python-support/python2.5/sqlalchemy/sql/expression.py,
 line 2275, in __init__
 self.onclause = self.__match_primaries(self.left, self.right)
   File
 /var/lib/python-support/python2.5/sqlalchemy/sql/expression.py,
 line 2317, in __match_primaries
 return sql_util.join_condition(primary, secondary)
   File /var/lib/python-support/python2.5/sqlalchemy/sql/util.py,
 line 74, in join_condition
 col = fk.get_referent(a)
   File /var/lib/python-support/python2.5/sqlalchemy/schema.py,
 line 755, in get_referent
 return table.corresponding_column(self.column)
   File /var/lib/python-support/python2.5/sqlalchemy/schema.py,
 line 788, in column
 foreign key % tname)
 sqlalchemy.exceptions.NoReferencedTableError: Could not find table
 'post' with which to generate a foreign key

 I've also attached the two modules and the test script I'm trying
 to use, if you wouldn't mind taking a look an letting me know what
 I'm doing wrong, I would really appreciate it.

 Cheers,

 Heston

 


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