def setup_module(): global users_table, test_table, multi_table, fresh_multi_table config.update({ 'sqlalchemy.dburi': 'sqlite:///:memory:', 'fresh.dburi': 'sqlite:///freshtest.db', }) if os.path.exists('freshtest.db'): os.unlink('freshtest.db') fresh_metadata = get_metadata('fresh') if metadata.is_bound(): metadata.bind = None bind_metadata() metadata.bind.echo = True fresh_metadata.bind.echo = True users_table = Table("users", metadata, Column("user_id", Integer, primary_key=True), Column("user_name", String(40)), Column("password", String(10))) persons_table = Table("persons", metadata, Column("id", Integer, primary_key=True), Column("name", String(40))) addresses_table = Table("addresses", metadata, Column("id", Integer, primary_key=True), Column("address", String(40)), Column("city", String(40)), Column("person_id", Integer, ForeignKey(persons_table.c.id))) multi_table = Table('multi', metadata, Column('id', Integer, primary_key=True)) metadata.create_all() test_table = Table("test", fresh_metadata, Column("id", Integer, primary_key=True), Column("val", String(40))) fresh_multi_table = Table('multi', fresh_metadata, Column('id', Integer, primary_key=True)) fresh_metadata.create_all() mapper(User, users_table) mapper(Person, persons_table) mapper(Address, addresses_table, properties=dict( person=relation(Person, backref='addresses'))) mapper(Test, test_table) start_server()
def create_tables(): """Create tables filled with test data.""" occupations_table = Table('occupations', metadata, Column('id', Integer, primary_key=True), Column('name', String(20))) users_table = Table( 'users', metadata, Column('id', Integer, primary_key=True), Column('name', String(20)), Column('occupation_id', Integer, ForeignKey("occupations.id"))) addresses_table = Table('addresses', metadata, Column('id', Integer, primary_key=True), Column('user_id', Integer, ForeignKey("users.id")), Column('street', String(50)), Column('city', String(40))) mapper(Occupation, occupations_table) mapper(User, users_table, properties={ 'occupation': relation(Occupation, lazy=False), 'addresses': relation(Address, backref='user', lazy=False) }) mapper(Address, addresses_table) metadata.create_all() occupations = ({ 'id': 1, 'name': 'Doctor' }, { 'id': 2, 'name': 'Actor' }, { 'id': 3, 'name': 'Programmer' }) users = ({ 'id': 1, 'name': 'Smith', 'occupation_id': 1 }, { 'id': 2, 'name': 'John', 'occupation_id': 2 }, { 'id': 3, 'name': 'Fred', 'occupation_id': 2 }, { 'id': 4, 'name': 'Albert', 'occupation_id': 3 }, { 'id': 5, 'name': 'Nicole', 'occupation_id': None }, { 'id': 6, 'name': 'Sarah', 'occupation_id': None }, { 'id': 7, 'name': 'Walter', 'occupation_id': 1 }, { 'id': 8, 'name': 'Bush', 'occupation_id': None }) addresses = ({ 'id': 1, 'user_id': None, 'street': 'street P', 'city': 'city X' }, { 'id': 2, 'user_id': 2, 'street': 'street B', 'city': 'city Z' }, { 'id': 3, 'user_id': 4, 'street': 'street C', 'city': 'city X' }, { 'id': 4, 'user_id': 1, 'street': 'street D', 'city': 'city Y' }, { 'id': 5, 'user_id': None, 'street': 'street E', 'city': 'city Z' }, { 'id': 6, 'user_id': 1, 'street': 'street F', 'city': 'city Z' }, { 'id': 7, 'user_id': 3, 'street': 'street G', 'city': 'city Y' }, { 'id': 8, 'user_id': 7, 'street': 'street H', 'city': 'city Y' }, { 'id': 9, 'user_id': 1, 'street': 'street I', 'city': 'city X' }, { 'id': 10, 'user_id': 6, 'street': 'street J', 'city': 'city X' }, { 'id': 11, 'user_id': 3, 'street': 'street K', 'city': 'city Z' }, { 'id': 12, 'user_id': 2, 'street': 'street L', 'city': 'city X' }, { 'id': 13, 'user_id': 8, 'street': 'street M', 'city': 'city X' }, { 'id': 14, 'user_id': 5, 'street': 'street N', 'city': 'city Y' }, { 'id': 15, 'user_id': 1, 'street': 'street O', 'city': 'city Y' }, { 'id': 16, 'user_id': 3, 'street': 'street A', 'city': 'city X' }) metadata.bind.execute(occupations_table.insert(), occupations) metadata.bind.execute(users_table.insert(), users) metadata.bind.execute(addresses_table.insert(), addresses) SOAddress.createTable() # SQLObject tests will need only "addresses" for kw in addresses: print SOAddress(**kw)
def create(command, args): print "Creating tables at %s" % (config.get("sqlalchemy.dburi")) bind_metadata() get_model() metadata.create_all()
def create_tables(): """Create tables filled with test data.""" occupations_table = Table('occupations', metadata, Column('id', Integer, primary_key=True), Column('name', String(20))) users_table = Table('users', metadata, Column('id', Integer, primary_key=True), Column('name', String(20)), Column('occupation_id', Integer, ForeignKey("occupations.id"))) addresses_table = Table('addresses', metadata, Column('id', Integer, primary_key=True), Column('user_id', Integer, ForeignKey("users.id")), Column('street', String(50)), Column('city', String(40))) mapper(Occupation, occupations_table) mapper(User, users_table, properties={ 'occupation' : relation(Occupation, lazy=False), 'addresses': relation(Address, backref='user', lazy=False)}) mapper(Address, addresses_table) metadata.create_all() occupations = ( {'id': 1, 'name': 'Doctor'}, {'id': 2, 'name': 'Actor'}, {'id': 3, 'name': 'Programmer'}) users = ( {'id': 1, 'name': 'Smith', 'occupation_id': 1}, {'id': 2, 'name': 'John', 'occupation_id': 2}, {'id': 3, 'name': 'Fred', 'occupation_id': 2}, {'id': 4, 'name': 'Albert', 'occupation_id': 3}, {'id': 5, 'name': 'Nicole', 'occupation_id': None}, {'id': 6, 'name': 'Sarah', 'occupation_id': None}, {'id': 7, 'name': 'Walter', 'occupation_id': 1}, {'id': 8, 'name': 'Bush', 'occupation_id': None}); addresses = ( {'id': 1, 'user_id': None, 'street': 'street P', 'city': 'city X'}, {'id': 2, 'user_id': 2, 'street': 'street B', 'city': 'city Z'}, {'id': 3, 'user_id': 4, 'street': 'street C', 'city': 'city X'}, {'id': 4, 'user_id': 1, 'street': 'street D', 'city': 'city Y'}, {'id': 5, 'user_id': None, 'street': 'street E', 'city': 'city Z'}, {'id': 6, 'user_id': 1, 'street': 'street F', 'city': 'city Z'}, {'id': 7, 'user_id': 3, 'street': 'street G', 'city': 'city Y'}, {'id': 8, 'user_id': 7, 'street': 'street H', 'city': 'city Y'}, {'id': 9, 'user_id': 1, 'street': 'street I', 'city': 'city X'}, {'id': 10, 'user_id': 6, 'street': 'street J', 'city': 'city X'}, {'id': 11, 'user_id': 3, 'street': 'street K', 'city': 'city Z'}, {'id': 12, 'user_id': 2, 'street': 'street L', 'city': 'city X'}, {'id': 13, 'user_id': 8, 'street': 'street M', 'city': 'city X'}, {'id': 14, 'user_id': 5, 'street': 'street N', 'city': 'city Y'}, {'id': 15, 'user_id': 1, 'street': 'street O', 'city': 'city Y'}, {'id': 16, 'user_id': 3, 'street': 'street A', 'city': 'city X'}); metadata.bind.execute(occupations_table.insert(), occupations) metadata.bind.execute(users_table.insert(), users) metadata.bind.execute(addresses_table.insert(), addresses) SOAddress.createTable() # SQLObject tests will need only "addresses" for kw in addresses: print SOAddress(**kw)