def create_table(db, s_create_sql): """ Create a table for an AutORM class. """ Query.begin(db=db) Query.raw_sqlscript(s_create_sql, db=db) Query.commit(db=db)
def create_table(self): """ Create a table for an AutORM class. """ if hasattr(self.rclass.Meta, 'create_sql'): s_create_sql = self.rclass.Meta.create_sql else: fields = [] deferred = [] for f in self.rclass._fields: if f.creation_deferred: deferred.append(f.define(self.rclass.Meta.table)) else: fields.append(f.define()) s_create_sql = """CREATE TABLE %s (%s); %s;""" % (self.rclass.Meta.table_safe, ", ".join(fields), ";".join(deferred)) Query.begin(db=self.rclass.db) Query.raw_sqlscript(s_create_sql, db=self.rclass.db) Query.commit(db=self.rclass.db)
def create_table(self): """ Create a table for an AutORM class. """ if hasattr(self.rclass.Meta, 'create_sql'): s_create_sql = self.rclass.Meta.create_sql else: fields = [] deferred = [] for f in self.rclass._fields: if f.creation_deferred: deferred.append(f.define(self.rclass.Meta.table)) else: fields.append(f.define()) s_create_sql = """CREATE TABLE %s (%s); %s;""" % ( self.rclass.Meta.table_safe, ", ".join(fields), ";".join(deferred)) Query.begin(db=self.rclass.db) Query.raw_sqlscript(s_create_sql, db=self.rclass.db) Query.commit(db=self.rclass.db)
CREATE TABLE author ( id INTEGER PRIMARY KEY AUTOINCREMENT, first_name VARCHAR(40) NOT NULL, last_name VARCHAR(40) NOT NULL, bio TEXT ); CREATE TABLE books ( id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(255), author_id INT(11), json_data TEXT, FOREIGN KEY (author_id) REFERENCES author(id) ); """ #autorm_db.conn.connect('sqlite3', ':memory:') Query.raw_sqlscript(sqlite_create) class Author(Model): books = OneToMany('Book') class Meta: defaults = {'bio': 'No bio available'} validations = {'first_name': validators.Length(), 'last_name': (validators.Length(), lambda obj,x: x != 'BadGuy!')} fields = [IdField('id'), Field('first_name'), Field('last_name'), TextField('bio')] class Book(Model): author = ForeignKey(Author)
TextField('first_name', notnull=True), TextField('last_name', notnull=True), TextField('bio'), JSONField('some_json_data')] autorm_db.conn.connect('sqlite3', ':memory:') # let's create a table for the model above: Author.objects.create_table() # This model's fields are derived via introspecting the database. So, you need # a connection and a table. Query.raw_sqlscript(""" DROP TABLE IF EXISTS books; CREATE TABLE books ( id INTEGER PRIMARY KEY autoincrement, title VARCHAR(255), other_json_data TEXT, author_id INT(11), FOREIGN KEY (author_id) REFERENCES author(id) ); """) class Book(Model): author = ForeignKey(Author) class Meta: table = 'books' # inspect the database to get field names, # use the default field type (no-op) for all the columns, except this one: field_overrides = [JSONField('other_json_data')] # Now we can create, retrieve, update and delete entries in our database.
JSONField('some_json_data') ] autorm_db.conn.connect('sqlite3', ':memory:') # let's create a table for the model above: Author.objects.create_table() # This model's fields are derived via introspecting the database. So, you need # a connection and a table. Query.raw_sqlscript(""" DROP TABLE IF EXISTS books; CREATE TABLE books ( id INTEGER PRIMARY KEY autoincrement, title VARCHAR(255), other_json_data TEXT, author_id INT(11), FOREIGN KEY (author_id) REFERENCES author(id) ); """) class Book(Model): author = ForeignKey(Author) class Meta: table = 'books' # inspect the database to get field names, # use the default field type (no-op) for all the columns, except this one: field_overrides = [JSONField('other_json_data')]
CREATE TABLE author ( id INTEGER PRIMARY KEY AUTOINCREMENT, first_name VARCHAR(40) NOT NULL, last_name VARCHAR(40) NOT NULL, bio TEXT ); CREATE TABLE books ( id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(255), author_id INT(11), json_data TEXT, FOREIGN KEY (author_id) REFERENCES author(id) ); """ #autorm_db.conn.connect('sqlite3', ':memory:') Query.raw_sqlscript(sqlite_create) class Author(Model): books = OneToMany('Book') class Meta: defaults = {'bio': 'No bio available'} validations = { 'first_name': validators.Length(), 'last_name': (validators.Length(), lambda obj, x: x != 'BadGuy!') } fields = [ IdField('id'), Field('first_name'),