Is it possible to manually define a single attribute for a Hanami/ROM entity? - sequel

we're building a Hanami app and one of my entities has an attribute of tsvector. I've added this via a migration which ran ok:
Hanami::Model.migration do
change do
alter_table :segments do
add_column :ts_content, :tsvector, generated_always_as: [to_tsvector: [:simple, :content]]
end
end
end
When I start the server, or run a rake task for example, this is what I get:
ROM::Schema::MissingAttributesError: missing attributes in ROM::Relation::Name(segments) schema: :ts_content
I gather that tsvector is not a supported type. So the schema can't be inferred. Discussion in one of the issues related to this suggests I need to define the attribute manually. Where and how do I do this for a single attribute?
If I do it in the entity I suppose I have to do it for all the attributes, right?
Best, Seba

Related

How to create associations between tables in migrations properly?

I am trying to comprehend associations between tables in Sequelize (postgre sql). I know that migrations have two ways to manage associations - adding a reference property to the table field declaration in the queryInterface.CreateTable() function. You can also add a queryInterface.addConstraints() function where you can set constraints. It seems to me that the queryInterface.CreateTable() function is enough for Sequelize to make the constraints itself, then I don’t understand *whether it’s necessary to add the queryInterface.addConstraints() function in addition to the references field.
In model you also have to call two functions for each association, for example, BelongsTo() and HasMany(). But I can't figure out what these functions do because they don't change the database. Please help me understand difference
You can create foreign keys while using the createTable() function simply by adding a 'references' object to the column definition. For example:
TeamId: {
type: Sequelize.INTEGER,
references: {
model: 'Team',
key: 'id'
}
}
The caveat to this is that if you are doing a large batch of migrations simultaneously, and in something like the above example, the 'Team' table has not yet been built, this code will fail. For this reason, I tend to write migrations that build out all of the tables first, then do a separate migration afterward with all the foreign keys. In that case, you would use the queryInterface.addColumn(table, columnName, optionsObj) method, using the same syntax as the example above for the options object.
If you are using migrations instead of sequelize.sync(), the association methods such as belongsTo() and hasMany() only exist for when the model interacts with the database, so that you can retrieve associated models.

How to create relationships between entities with existing database that does not contain foreign keys

Using Entity Framework Core 2.0
Stuck with company's production database which has primary keys defined for each table but no foreign keys defined for any relationships.
Dependent records in the database have id fields which are intended to relate to the primary key fields of the parent record like you would normally find with a foreign key relationship/constraint. But these fields were all created as INT NOT NULL and are using a SQL default of '0'.
As a result dependent records have been inserted over time without requiring that a related parent record be specified.
Initially I defined my models in EF with integers and used a fluent configuration to specify "IsRequired". This was done so I could run migrations to create a test database for comparison against the production database to verify that my code first was correctly coded.
This then lead to the problem while using "Include" in my Linq queries which performs an inner join that results in dropping the records that contain the 0's in the id fields of the dependent record.
The only way that I have found to make this work is to model all of the id fields in the dependent entity as nullable integers and remove the "IsRequired" from the fluent configuration.
When using the "Include" it performs a left outer join keeping all of the dependent entities. This also means that any reference properties on the included entities are set to null instead of an empty string. This part can probably be fixed fairly easily.
The downside is if I wanted to use migrations to create a database now, all id fields in the dependent records would be created as NULL.
Is there anyone who has run up against this type of situation? Does anyone have any suggestions to try other than the approach I am using?
I haven't dealt with this scenario before but I wonder if you can solve it by defining the FK property as Nullable and then in the migrations, after the migration is created, edit it to add a HasDefaultValue property to ensure that it's 0? (doc for that migration method: https://learn.microsoft.com/en-us/ef/core/modeling/relational/default-values)

Rails 5.1.2 - Single Table Inheritance: No migration is getting generated

I am trying to generate scaffolding for STI implementation. I issue the following.
rails g scaffold user1 type name email
rails g scaffold member company subscription --parent user1
Every thing gets generated file except for the migration file my 'member' model.
When I try to create a member record like this
Member.create(name: "My Name", email: "myname#example.com",
company: 'Example LLC', subscription: 'Monthly Gold' )
I get this error:
ActiveModel::UnknownAttributeError: unknown attribute 'company' for Member. from (irb):1
Any ideas on what is going on?
I use rails 5 and db is postgres
The --parent option assumes that you are already all setup for single table inheritance, i.e. the parent class has a table with a type column (or whatever column you are using for this).
Since the model will be stored in the parent's table, there is no need to create a new table for the subclass, hence no migration
I got this answer similar to this question asked by someone.
To my understanding, you are on the wrong track. In single table inheritance, all the attributes must be present in the parent model table with an additional column name 'type' to indicate the type of inherited model. The column name 'type' can be changed with appropriate settings but ActiveRecord by default looks for 'type' column. You are getting 'UnknownAttributeError' error cause the parent model does not have the following column in its table. You need to write a migration to add the new columns. Hope you understand the concept of STI. For further exploration, I am providing you the link of the official guide. Hope your problem will be solved.
http://edgeguides.rubyonrails.org/association_basics.html#single-table-inheritance

Entity Framework: How can I assign permissions on new code first tables?

I think the question speaks for itself. I have a fairly typical case where I've created a new entity class on which I've specified the [Table] attribute. The Add-Migration command has generated the corresponding DbMigration.CreateTable, etc.
However, I would like to ensure that the table is create in SQL Server with the select permission assigned to group... let's call it ABCGRP.
Can this be done via attributes, the Fluent API or will I need to simply create a SQL script with a GRANT operation?

Plural table names with Entity Frameworks Model First

I'm giving EF Model first a go. I'm using EF 4.1
Pretty much followed this article
I've set PluraliseNewObjects to False on the Model and also in Options->Database Tools ->O/R Designer set Pluralization of names to false.
Neither have any effect - when I generate a new schema from the model the table names are always pluralised - is it possible to disable this?
OK - I've found one way to achieve what I want - but it's a pretty daft route.
Generated db with the plural names (interesting that it only pluralised the tables mapping to types - not the auto-generated linking tables for many to many joins).
Manually renamed the tables in the database
Deleted Model from the project and recreated based on existing database schema (the one I've just renamed).
Model is now correctly mapped to singularly names tables.
I'll wait and see if anyone comes up with a more sensible way of achieving this....
The names of the tables in the generated DDL seem to match the "Entity Set Name" values (different than the "Entity Name"). If you singularize the Entity Set Names, the table names in the DDL are singularized as well.
This will have the possibly undesired effect of singularizing the EntitySet property names in your code, though. Instead of:
myDatabase
.Products
.Where...
.Select...
your code will look like:
myDatabase
.Product
.Where...
.Select...
may or may not be an issue