简体   繁体   中英

ActiveRecord to SQLalchemy schema

I try to convert this ActiveRecord schema to sqlalchemy

def db_schema
  ActiveRecord::Schema.define do
unless table_exists? :strains
  create_table :strains do |t|
    t.column :name, :string
    t.column :description, :string
  end
end

# indices
unless index_exists? :strains, :id
  add_index :strains, :id
end
  end
end

Is this SQLalchemy the equivalent to ActiveRecord?

class Strain(db.Model):
    __tablename__ = 'strains'
  name = db.Column(db.String(), index=True, unique=True)
  description = db.Column(db.String())

Close, but seems to be missing id column. I don't know exactly how ActiveRecord behaves regarding nullable/default, so I'm just guessing good defaults for some of this.

class Strain(db.Model):
    __tablename__ = 'strains'
    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False, unique=True, index=True)
    description = Column(String, nullable=False, default='')

Also, MySQL and some other dbs expect a length for String, and treat String different than Text. At least PostgreSQL and SQLite don't care though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM