Hello,

I've attached a simple example where I'm attempting to auto load two tables
with a manytoone/onetomany relationship.  In the given schema I'm working
with there is no constraint on the table.

The code attached has a line that will modify the table to include a
constraint, and with it, everything works nicely.  Without it, during
setup_all() create_keys throws the exception:
Exception: Couldn't find a foreign key constraint in table 'media' using the
following columns: userid.

I'm wondering if there's a way to autoload these tables without modifying
this schema (it's not mine, after all :-)?

Thank you,
Stephen.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

#!/usr/bin/python

from elixir import Entity, Field, OneToMany, ManyToOne, ManyToMany
from elixir import using_options, setup_all
from elixir import String, Unicode, Integer, DateTime
from elixir import metadata, session

from sqlalchemy import Table, create_engine

db = create_engine('postgres://localhost/testdb')
db.echo = True
metadata.bind = db

conn = db.connect()
conn.execute("DROP TABLE users CASCADE")
conn.execute("""CREATE TABLE users (
    userid      bigint      DEFAULT '0' NOT NULL,
    name        varchar(100)        DEFAULT ''  NOT NULL,
    PRIMARY KEY (userid)
) with OIDS;
""")

conn.execute("DROP TABLE media")
conn.execute("""CREATE TABLE media (
    mediaid     bigint      DEFAULT '0' NOT NULL,
    userid      bigint      DEFAULT '0' NOT NULL,
    sendto      varchar(100)        DEFAULT ''  NOT NULL,
    PRIMARY KEY (mediaid)
) with OIDS;
""")

# NOTE: With this statement the model below works, however, without it,
# generates : Exception: Couldn't find a foreign key constraint in table 'media' using the following columns: userid.

#conn.execute("""ALTER TABLE media ADD CONSTRAINT 
#				media_userid_fk FOREIGN KEY(userid) REFERENCES users (userid);""")
conn.close()

class Media(Entity):
	using_options(
		tablename='media',
		autoload=True,
		auto_primarykey='mediaid',
	)
	user = ManyToOne('User', colname='userid', ondelete='cascade') 

class User(Entity):
	using_options(
		tablename='users',
		autoload=True,
		auto_primarykey='userid',
	)
	media = OneToMany('Media', cascade='all, delete-orphan')

setup_all()

print "adding user and media"
u = User(name='test', media=[Media(sendto='[EMAIL PROTECTED]')])
session.flush()

print "ECHO USERS & MEDIA"
for u in User.query.all():
	print "user: ", u.name

for m in Media.query.all():
	print "media:", m.sendto

print "deleting media"
del u.media[0]
session.flush()

print "ECHO USERS"
for u in User.query.all():
	print "user: ", u.name

for m in Media.query.all():
	print "media:", m.sendto

Reply via email to