[英]ActiveRecord to SQLalchemy schema
I try to convert this ActiveRecord schema to sqlalchemy 我尝试将此ActiveRecord模式转换为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? 这个SQLalchemy是否等效于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. 关闭,但似乎缺少id列。 I don't know exactly how ActiveRecord behaves regarding nullable/default, so I'm just guessing good defaults for some of this. 我不确切知道ActiveRecord在可为空/默认值方面的表现,因此我只是猜测其中的一些默认值很好。
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. 另外,MySQL和其他一些数据库期望String的长度,并且将String与Text区别对待。 At least PostgreSQL and SQLite don't care though. 至少PostgreSQL和SQLite不在乎。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.