Zend_Db_Table_Abstract $_name not working - zend-db-table

I encountered a problem regarding changing default table name
class Application_Model_DbTable_Game extends Zend_Db_Table_Abstract
{
protected $_name = 'games';
Error:
Message: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'gamenomad_dev.game' doesn't exist
Help me... it's supposed to be simple!
*EDIT
The problem here is that Zend Framework is supposed to detect the changed table name from the default 'game' into 'games'.

In ZF you have to hardcode your database table to model. It doesn't scan for database changes. You have two ways:
Create class with table name
class Game extends Zend_Db_Table_Abstract
{
// default table name: game
}
If you want to use ZF's default paths, you should put DBTable model into application/models/dbtable directory and name your class like Application_Model_DbTable_Game - then ZF knows it has to look for game table
Create class with any name
e.g. ExtraGameTable and set its parameters to show table name:
class ExtraGameTable extends Zend_Db_Table_Abstract
{
protected $_name = 'game';
}
As stated in documentation: http://framework.zend.com/manual/en/zend.db.table.html
If you don't specify the table name, it defaults to the name of the
class. If you rely on this default, the class name must match the
spelling of the table name as it appears in the database.
You may try to combine it with some configuration file and load table names from there, but still - ZF won't know anything about underlying database changes.

Show the actual line and stacktrace to your problem, maybe you are generating your query in a way it doesn't read the actual table name.

Related

How to rename generated table references?

I have two schemas in my database and both share tables with the same names.
In order to not have naming conflicts in generated code, I want to prefix all names with the appropriate schema name.
I made it work for class names by overriding getJavaClassName of DefaultGeneratorStrategy however I could not find a fitting override that is responsible for the table references in tables.references.Tables.kt.
I managed to solve my problem by overriding getJavaIdentifier of DefaultGeneratorStrategy the following way:
override fun getJavaIdentifier(definition: Definition): String {
if (definition is PostgresTableDefinition) {
return "${definition.schema.name.toUppercaseSnakeCase()}_${super.getJavaIdentifier(definition)}"
}
return super.getJavaIdentifier(definition)
}

Remove double-quotes from generated query from ServiceStack.Ormlite

Our DBA don't want us to use double quoted fields and tables in our queries (don't ask me the reason)... the problem is that ServiceStack.OrmLite double quote them all, and I don't have any idea on how disable this behaviour. We are using ServiceStack.OrmLite Version 4.5.4.0.
For example:
public class ClassA {
public int ID {get;set;}
public string Name {get;set;}
}
If we make a simple query like:
using (IDbConnection db = dbFactory.Open())
{
return db.LoadSingleById<ClassA>(id);
}
would generate:
select "ID", "Name" from "ClassA" where "ID" = #0
And this is what our dba want:
select ID, Name from ClassA where ID = #0
If anybody could help, I would apreciate a lot
PS I know I can write myself all queries, but there are too much code to change, so I'm trying to avoid this solution because it's too much time consuming at the moment.
Based on my inspection of the source code, it appears that this cannot be changed out of the box.
When ORMLite builds its query, it grabs the column name and wraps it in quotation marks. See here: https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs#L384
An alternative would be to create a new OrmLiteDialectProvider that inherits whichever provider you are using (e.g., SQL Server, Oracle, etc), and override one of the following methods:
GetQuotedColumnName(string columnName)
GetQuotedName(string name)
Overriding either of those to exclude the quotation marks would get you what you're looking for.

django model add default columns

I have to following model:
class publication_type(models.Model):
name = models.CharField(max_length=200)
description = models.TextField(blank=True,null=True,default=None)
And I would like to add a few "default" entrys to this model, that I can always refer to without assuming whether they exist yet or not, for example:
publication_type.objects.create(id=1,name="article",description="...")
Where would be the best position in the django code to position to put this code?

Why my app is searching for table that doesn't / shouldn't exist?

I have created an MVC app in which I renamed a model class from "Diplomata" to "Diplomas" and now I can't make the migrations to create a table with name "Diplomas", because they still use the old name for some reason. (using .NET Framework 4.6 and EntityFramework 6.1.2)
things I have tried so far:
dropping the db tables completely (from Visual Studio's SQL Server Object Explorer and deleting the files manually)
deleting the migration folder and re-enabling migrations
deleting the model and re-creating it (after deleting migrations and dropping the tables completely)
After enabling migrations again and using command "add-migration Initial" I get a script that generates a table with name "dbo.Diplomata"
this is the model
namespace DDS.Data.Models
{
using System.Collections.Generic;
using DDS.Data.Common.Models;
public class Diploma : BaseModel<int>
{
public string Title { get; set; }
public string Description { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
}
this is the ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
...
public IDbSet<Diploma> Diplomas { get; set; }
...
}
and this is the part of the migration script that is automatically generated
public partial class Initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Diplomata",
c => new
{
Id = c.Int(nullable: false, identity: true),
Title = c.String(),
Description = c.String(),
...
}
Also running a search in VS2015 for "Diplomata" in the entire solution doesn't find anything.
Adding a migration that is renaming the table makes the app crash after the update, because it is searching for a table with the old name. (Invalid object name 'dbo.Diplomata')
I have been debugging this all day with no result so any ideas or suggestions for where and what to look for are appreciated.
PS: This is my first question here so if I missed something or something is hard to understand please tell me, thank you
Did you try cleaning the migrations table? When migrations are enabled, a system table called "__MigrationHistory" is generated. You can locate such table from within SQL Server Management Studio. Go to YourDatabase->Tables->System Tables and it's going to be there.
The way migrations work is as follows:
An initial migration is generated with a specified name.
The initial migration is executed.
When the initial migration and subsequent migrations are run, the changes are applied to the database and a snapshot of the structure of the database it's saved in the __MigrationHIstory table as a new row.
When the application initializaes, EF will compare the latest record in the migrations table and compare that snapshot with the latest migration available, if they don't match, an exception will be thrown. This is how EF determines whether or not the Database changed.
When you are making changes to the original model, you are supposed to create subsequent migration files so you can revert or apply database changes. If you already deleted the initial migration file, probably your best option will be to clean the __MigrationHistory table. EF generates unique entries in the table (I believe that the default behavior is the name of the migration file + a timestamp when the migration was generated). You can always rename the initial migration file to match with the name in the __MigrationHistory table and create a new migration to apply the rename of the table in question. This will only work if the name of the files and model match the snapshots in the DB, otherwise an exception will be thrown
Take a look to this article for more information about migrations:
http://tech.trailmax.info/2014/03/inside_of_ef_migrations/
As a side note, you can also modify the default behavior oh how the migration history table is generated. This may be helpful in the event you need to support specific needs, such as renaming it, not generate it as a system table, add additional columns, etc. This will be helpful in certain scenarios. Check the following link (Particularly useful for cloud-based databases):
How do I add an additional column to the __MigrationHistory table?
NOTE: It is important to mention that tables that are not contained in the initial model or subsequent models will be excluded from the model validation. This is specially useful if you want to create tables that shouldn't be monitored by EF. I.E: The membership provider tables.
I hope this helps clarifying the problems you are having.
After few more trials and errors I gave up on fixing the problem. Instead I forced the app to created the model in a table with the name I wanted by adding Table attribute on the model. So now the model looks like this:
[Table("Diplomas")]
public class Diploma : BaseModel<int>
{
public string Title { get; set; }
public string Description { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}

Kohana 3 Auto loading Models

I'm attempting to use a Model but I get a fatal error so I assume it doesn't autoload properly.
ErrorException [ Fatal Error ]: Class
'Properties_Model' not found
The offending controller line:
$properties = new Properties_Model;
The model:
class Properties_Model extends Model
{
public function __construct()
{
parent::__construct();
}
}
I also put the class in three different locations hoping one would work, all there failed.
They are:
application/classes/model
application/model
application/models
What am I missing?
Ah, I got this question emailed directly to me (via my website's contact form)!
Here is what I responded with (for the benefit of other people which may run into this problem).
The correct location of a model named
properties is
application/classes/model/properties.php
and the class definition would be as
follows
class Model_Properties extends Model { }
Think of the underscore above as the
directory separator. That is, if you
replaced the underscore with a / you
would have: 'model/properties', which
will be your file under application/classes.
To load the model from a controller,
you can use PHP's standard new
operator or do what I prefer, which is
$propertiesModel = Model::factory('Properties');
I'm not 100% why I prefer this way...
but it works for me :)
First off, The Kohana 3 fileyestem does not work like Kohana 2's!
In K2 the autoloader looks at the class name searches for the class in different folders, based on the suffix of the class.
In K3, class names are "converted" to file paths by replacing underscores with slashes.
i.e. Class Properties_Model becomes classes/properties/model.php
As you can see, using a Model suffix in this new system won't really help to group your models, so basically you prepend "Model" to the class name instead of suffixing it:
Model_Property is located in classes/model/property.php
For more information see the Kohana 3 userguide

Resources